feat: Adds the ability to edit article (#5232)
This commit is contained in:
@@ -62,18 +62,24 @@ export const actions = {
|
||||
commit(types.SET_UI_FLAG, { isFetching: false });
|
||||
}
|
||||
},
|
||||
update: async ({ commit }, params) => {
|
||||
const articleId = params.id;
|
||||
update: async ({ commit }, { portalSlug, articleId, ...articleObj }) => {
|
||||
commit(types.ADD_ARTICLE_FLAG, {
|
||||
uiFlags: {
|
||||
isUpdating: true,
|
||||
},
|
||||
articleId,
|
||||
});
|
||||
try {
|
||||
const { data } = await articlesAPI.update(params);
|
||||
|
||||
commit(types.UPDATE_ARTICLE, data);
|
||||
try {
|
||||
const {
|
||||
data: { payload },
|
||||
} = await articlesAPI.updateArticle({
|
||||
portalSlug,
|
||||
articleId,
|
||||
articleObj,
|
||||
});
|
||||
|
||||
commit(types.UPDATE_ARTICLE, payload);
|
||||
|
||||
return articleId;
|
||||
} catch (error) {
|
||||
|
||||
@@ -19,7 +19,7 @@ export const mutations = {
|
||||
[types.CLEAR_ARTICLES]: $state => {
|
||||
Vue.set($state.articles, 'byId', {});
|
||||
Vue.set($state.articles, 'allIds', []);
|
||||
Vue.set($state.articles, 'uiFlags', {});
|
||||
Vue.set($state.articles, 'uiFlags.byId', {});
|
||||
},
|
||||
[types.ADD_MANY_ARTICLES]($state, articles) {
|
||||
const allArticles = { ...$state.articles.byId };
|
||||
@@ -55,7 +55,6 @@ export const mutations = {
|
||||
},
|
||||
[types.UPDATE_ARTICLE]($state, article) {
|
||||
const articleId = article.id;
|
||||
|
||||
if (!$state.articles.allIds.includes(articleId)) return;
|
||||
|
||||
Vue.set($state.articles.byId, articleId, {
|
||||
|
||||
@@ -89,8 +89,15 @@ describe('#actions', () => {
|
||||
|
||||
describe('#update', () => {
|
||||
it('sends correct actions if API is success', async () => {
|
||||
axios.patch.mockResolvedValue({ data: articleList[0] });
|
||||
await actions.update({ commit }, articleList[0]);
|
||||
axios.patch.mockResolvedValue({ data: { payload: articleList[0] } });
|
||||
await actions.update(
|
||||
{ commit },
|
||||
{
|
||||
portalSlug: 'room-rental',
|
||||
articleId: 1,
|
||||
title: 'Documents are required to complete KYC',
|
||||
}
|
||||
);
|
||||
expect(commit.mock.calls).toEqual([
|
||||
[
|
||||
types.default.ADD_ARTICLE_FLAG,
|
||||
@@ -105,9 +112,17 @@ describe('#actions', () => {
|
||||
});
|
||||
it('sends correct actions if API is error', async () => {
|
||||
axios.patch.mockRejectedValue({ message: 'Incorrect header' });
|
||||
await expect(actions.update({ commit }, articleList[0])).rejects.toThrow(
|
||||
Error
|
||||
);
|
||||
await expect(
|
||||
actions.update(
|
||||
{ commit },
|
||||
{
|
||||
portalSlug: 'room-rental',
|
||||
articleId: 1,
|
||||
title: 'Documents are required to complete KYC',
|
||||
}
|
||||
)
|
||||
).rejects.toThrow(Error);
|
||||
|
||||
expect(commit.mock.calls).toEqual([
|
||||
[
|
||||
types.default.ADD_ARTICLE_FLAG,
|
||||
|
||||
@@ -106,7 +106,11 @@ describe('#mutations', () => {
|
||||
mutations[types.CLEAR_ARTICLES](state);
|
||||
expect(state.articles.allIds).toEqual([]);
|
||||
expect(state.articles.byId).toEqual({});
|
||||
expect(state.articles.uiFlags).toEqual({});
|
||||
expect(state.articles.uiFlags).toEqual({
|
||||
byId: {
|
||||
'1': { isFetching: false, isUpdating: true, isDeleting: false },
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user