2022-08-02 11:44:10 +00:00
|
|
|
import articlesAPI from 'dashboard/api/helpCenter/articles';
|
2022-07-27 06:59:11 +00:00
|
|
|
import { throwErrorMessage } from 'dashboard/store/utils/api';
|
2022-08-08 10:17:32 +00:00
|
|
|
|
2022-07-27 06:59:11 +00:00
|
|
|
import types from '../../mutation-types';
|
|
|
|
export const actions = {
|
2022-08-08 10:17:32 +00:00
|
|
|
index: async (
|
|
|
|
{ commit },
|
2022-08-10 05:18:41 +00:00
|
|
|
{ pageNumber, portalSlug, locale, status, author_id, category_slug }
|
2022-08-08 10:17:32 +00:00
|
|
|
) => {
|
2022-07-27 06:59:11 +00:00
|
|
|
try {
|
|
|
|
commit(types.SET_UI_FLAG, { isFetching: true });
|
2022-08-02 11:44:10 +00:00
|
|
|
const {
|
|
|
|
data: { payload, meta },
|
2022-08-08 10:17:32 +00:00
|
|
|
} = await articlesAPI.getArticles({
|
2022-08-02 11:44:10 +00:00
|
|
|
pageNumber,
|
|
|
|
portalSlug,
|
|
|
|
locale,
|
2022-08-08 10:17:32 +00:00
|
|
|
status,
|
|
|
|
author_id,
|
2022-08-10 05:18:41 +00:00
|
|
|
category_slug,
|
2022-08-02 11:44:10 +00:00
|
|
|
});
|
|
|
|
const articleIds = payload.map(article => article.id);
|
|
|
|
commit(types.CLEAR_ARTICLES);
|
|
|
|
commit(types.ADD_MANY_ARTICLES, payload);
|
|
|
|
commit(types.SET_ARTICLES_META, meta);
|
2022-07-27 06:59:11 +00:00
|
|
|
commit(types.ADD_MANY_ARTICLES_ID, articleIds);
|
|
|
|
return articleIds;
|
|
|
|
} catch (error) {
|
|
|
|
return throwErrorMessage(error);
|
|
|
|
} finally {
|
|
|
|
commit(types.SET_UI_FLAG, { isFetching: false });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2022-08-18 06:15:08 +00:00
|
|
|
create: async ({ commit, dispatch }, { portalSlug, ...articleObj }) => {
|
2022-07-27 06:59:11 +00:00
|
|
|
commit(types.SET_UI_FLAG, { isCreating: true });
|
|
|
|
try {
|
2022-08-18 06:15:08 +00:00
|
|
|
const {
|
|
|
|
data: { payload },
|
|
|
|
} = await articlesAPI.createArticle({
|
|
|
|
portalSlug,
|
|
|
|
articleObj,
|
|
|
|
});
|
|
|
|
const { id: articleId, portal } = payload;
|
|
|
|
commit(types.ADD_ARTICLE, payload);
|
2022-07-27 06:59:11 +00:00
|
|
|
commit(types.ADD_ARTICLE_ID, articleId);
|
2022-08-18 06:15:08 +00:00
|
|
|
commit(types.ADD_ARTICLE_FLAG, articleId);
|
|
|
|
dispatch('portals/updatePortal', portal, { root: true });
|
2022-07-27 06:59:11 +00:00
|
|
|
return articleId;
|
|
|
|
} catch (error) {
|
|
|
|
return throwErrorMessage(error);
|
|
|
|
} finally {
|
|
|
|
commit(types.SET_UI_FLAG, { isCreating: false });
|
|
|
|
}
|
|
|
|
},
|
2022-08-02 11:44:10 +00:00
|
|
|
|
|
|
|
show: async ({ commit }, { id, portalSlug }) => {
|
|
|
|
commit(types.SET_UI_FLAG, { isFetching: true });
|
|
|
|
try {
|
2022-08-04 09:41:29 +00:00
|
|
|
const response = await articlesAPI.getArticle({ id, portalSlug });
|
2022-08-02 11:44:10 +00:00
|
|
|
const {
|
|
|
|
data: { payload },
|
|
|
|
} = response;
|
|
|
|
const { id: articleId } = payload;
|
|
|
|
commit(types.ADD_ARTICLE, payload);
|
|
|
|
commit(types.ADD_ARTICLE_ID, articleId);
|
|
|
|
commit(types.SET_UI_FLAG, { isFetching: false });
|
|
|
|
} catch (error) {
|
|
|
|
commit(types.SET_UI_FLAG, { isFetching: false });
|
|
|
|
}
|
|
|
|
},
|
2022-08-16 12:25:34 +00:00
|
|
|
update: async ({ commit }, { portalSlug, articleId, ...articleObj }) => {
|
2022-08-18 06:15:08 +00:00
|
|
|
commit(types.UPDATE_ARTICLE_FLAG, {
|
2022-07-27 06:59:11 +00:00
|
|
|
uiFlags: {
|
|
|
|
isUpdating: true,
|
|
|
|
},
|
|
|
|
articleId,
|
|
|
|
});
|
2022-08-16 12:25:34 +00:00
|
|
|
|
2022-07-27 06:59:11 +00:00
|
|
|
try {
|
2022-08-16 12:25:34 +00:00
|
|
|
const {
|
|
|
|
data: { payload },
|
|
|
|
} = await articlesAPI.updateArticle({
|
|
|
|
portalSlug,
|
|
|
|
articleId,
|
|
|
|
articleObj,
|
|
|
|
});
|
2022-07-27 06:59:11 +00:00
|
|
|
|
2022-08-16 12:25:34 +00:00
|
|
|
commit(types.UPDATE_ARTICLE, payload);
|
2022-07-27 06:59:11 +00:00
|
|
|
|
|
|
|
return articleId;
|
|
|
|
} catch (error) {
|
|
|
|
return throwErrorMessage(error);
|
|
|
|
} finally {
|
2022-08-18 06:15:08 +00:00
|
|
|
commit(types.UPDATE_ARTICLE_FLAG, {
|
2022-07-27 06:59:11 +00:00
|
|
|
uiFlags: {
|
|
|
|
isUpdating: false,
|
|
|
|
},
|
|
|
|
articleId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2022-09-01 05:25:59 +00:00
|
|
|
delete: async ({ commit }, { portalSlug, articleId }) => {
|
2022-08-18 06:15:08 +00:00
|
|
|
commit(types.UPDATE_ARTICLE_FLAG, {
|
2022-07-27 06:59:11 +00:00
|
|
|
uiFlags: {
|
|
|
|
isDeleting: true,
|
|
|
|
},
|
|
|
|
articleId,
|
|
|
|
});
|
|
|
|
try {
|
2022-09-01 05:25:59 +00:00
|
|
|
await articlesAPI.deleteArticle({ portalSlug, articleId });
|
2022-07-27 06:59:11 +00:00
|
|
|
commit(types.REMOVE_ARTICLE, articleId);
|
|
|
|
commit(types.REMOVE_ARTICLE_ID, articleId);
|
|
|
|
return articleId;
|
|
|
|
} catch (error) {
|
|
|
|
return throwErrorMessage(error);
|
|
|
|
} finally {
|
2022-08-18 06:15:08 +00:00
|
|
|
commit(types.UPDATE_ARTICLE_FLAG, {
|
2022-07-27 06:59:11 +00:00
|
|
|
uiFlags: {
|
|
|
|
isDeleting: false,
|
|
|
|
},
|
|
|
|
articleId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|