Chatwoot/app/javascript/dashboard/store/modules/helpCenterArticles/actions.js
Sivin Varghese 9bc75225fe
feat: Category store integration (#5218)
* Add more actions

* Complete sidebar store integration

* Complete portal list store integration

* Fixed the specs

* Added missing specs

* Add comment

* Code cleanup

* Fixed all the spec issues

* Add portal and article API specs

* Add category name in article list

* Add more locales

* Code beautification

* Exclude locale from codeclimate ci

* feat: Category store integration

* chore: Minor fixes

* chore: API call fixes

* chore: Minor fixes

* chore: Minor fixes

* chore: Adds the ability for get articles based on categories

* chore: minor fixes

* chore: Minor fixes

* chore: fixes specs and minor improvements

* chore: Review fixes

* chore: Minor fixes

* chore: Review fixes

* chore: Review fixes

* chore: Spacing fixes

* Code cleanup

Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
2022-08-10 10:48:41 +05:30

114 lines
3 KiB
JavaScript

import articlesAPI from 'dashboard/api/helpCenter/articles';
import { throwErrorMessage } from 'dashboard/store/utils/api';
import types from '../../mutation-types';
export const actions = {
index: async (
{ commit },
{ pageNumber, portalSlug, locale, status, author_id, category_slug }
) => {
try {
commit(types.SET_UI_FLAG, { isFetching: true });
const {
data: { payload, meta },
} = await articlesAPI.getArticles({
pageNumber,
portalSlug,
locale,
status,
author_id,
category_slug,
});
const articleIds = payload.map(article => article.id);
commit(types.CLEAR_ARTICLES);
commit(types.ADD_MANY_ARTICLES, payload);
commit(types.SET_ARTICLES_META, meta);
commit(types.ADD_MANY_ARTICLES_ID, articleIds);
return articleIds;
} catch (error) {
return throwErrorMessage(error);
} finally {
commit(types.SET_UI_FLAG, { isFetching: false });
}
},
create: async ({ commit }, params) => {
commit(types.SET_UI_FLAG, { isCreating: true });
try {
const { data } = await articlesAPI.create(params);
const { id: articleId } = data;
commit(types.ADD_ARTICLE, data);
commit(types.ADD_ARTICLE_ID, articleId);
return articleId;
} catch (error) {
return throwErrorMessage(error);
} finally {
commit(types.SET_UI_FLAG, { isCreating: false });
}
},
show: async ({ commit }, { id, portalSlug }) => {
commit(types.SET_UI_FLAG, { isFetching: true });
try {
const response = await articlesAPI.getArticle({ id, portalSlug });
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 });
}
},
update: async ({ commit }, params) => {
const articleId = params.id;
commit(types.ADD_ARTICLE_FLAG, {
uiFlags: {
isUpdating: true,
},
articleId,
});
try {
const { data } = await articlesAPI.update(params);
commit(types.UPDATE_ARTICLE, data);
return articleId;
} catch (error) {
return throwErrorMessage(error);
} finally {
commit(types.ADD_ARTICLE_FLAG, {
uiFlags: {
isUpdating: false,
},
articleId,
});
}
},
delete: async ({ commit }, articleId) => {
commit(types.ADD_ARTICLE_FLAG, {
uiFlags: {
isDeleting: true,
},
articleId,
});
try {
await articlesAPI.delete(articleId);
commit(types.REMOVE_ARTICLE, articleId);
commit(types.REMOVE_ARTICLE_ID, articleId);
return articleId;
} catch (error) {
return throwErrorMessage(error);
} finally {
commit(types.ADD_ARTICLE_FLAG, {
uiFlags: {
isDeleting: false,
},
articleId,
});
}
},
};