feat: Adds the ability to edit/delete category (#5385)

This commit is contained in:
Sivin Varghese
2022-09-02 22:34:07 +05:30
committed by GitHub
parent a9801a3c76
commit d47a0ae461
17 changed files with 667 additions and 37 deletions

View File

@@ -9,6 +9,7 @@ export const actions = {
const {
data: { payload },
} = await categoriesAPI.get({ portalSlug });
commit(types.CLEAR_CATEGORIES);
const categoryIds = payload.map(category => category.id);
commit(types.ADD_MANY_CATEGORIES, payload);
commit(types.ADD_MANY_CATEGORIES_ID, categoryIds);
@@ -39,8 +40,7 @@ export const actions = {
}
},
update: async ({ commit }, portalSlug, categoryObj) => {
const categoryId = categoryObj.id;
update: async ({ commit }, { portalSlug, categoryId, categoryObj }) => {
commit(types.ADD_CATEGORY_FLAG, {
uiFlags: {
isUpdating: true,
@@ -48,8 +48,14 @@ export const actions = {
categoryId,
});
try {
const { data } = await categoriesAPI.update({ portalSlug, categoryObj });
commit(types.UPDATE_CATEGORY, data);
const {
data: { payload },
} = await categoriesAPI.update({
portalSlug,
categoryId,
categoryObj,
});
commit(types.UPDATE_CATEGORY, payload);
return categoryId;
} catch (error) {
return throwErrorMessage(error);
@@ -63,7 +69,7 @@ export const actions = {
}
},
delete: async ({ commit }, portalSlug, categoryId) => {
delete: async ({ commit }, { portalSlug, categoryId }) => {
commit(types.ADD_CATEGORY_FLAG, {
uiFlags: {
isDeleting: true,

View File

@@ -18,6 +18,13 @@ export const getters = {
});
return categories;
},
categoriesByLocaleCode: (...getterArguments) => localeCode => {
const [state, _getters] = getterArguments;
const categories = state.categories.allIds.map(id => {
return _getters.categoryById(id);
});
return categories.filter(category => category.locale === localeCode);
},
getMeta: state => {
return state.meta;
},

View File

@@ -19,7 +19,7 @@ export const mutations = {
[types.CLEAR_CATEGORIES]: $state => {
Vue.set($state.categories, 'byId', {});
Vue.set($state.categories, 'allIds', []);
Vue.set($state.categories, 'uiFlags', {});
Vue.set($state.categories.uiFlags, 'byId', {});
},
[types.ADD_MANY_CATEGORIES]($state, categories) {
const allCategories = { ...$state.categories.byId };

View File

@@ -13,6 +13,7 @@ describe('#actions', () => {
await actions.index({ commit }, { portalSlug: 'room-rental' });
expect(commit.mock.calls).toEqual([
[types.default.SET_UI_FLAG, { isFetching: true }],
[types.default.CLEAR_CATEGORIES],
[types.default.ADD_MANY_CATEGORIES, categoriesPayload.payload],
[types.default.ADD_MANY_CATEGORIES_ID, [1, 2]],
[types.default.SET_UI_FLAG, { isFetching: false }],
@@ -57,21 +58,34 @@ describe('#actions', () => {
describe('#update', () => {
it('sends correct actions if API is success', async () => {
axios.patch.mockResolvedValue({ data: categoriesPayload.payload[0] });
axios.patch.mockResolvedValue({ data: categoriesPayload });
await actions.update(
{ commit },
'web-docs',
categoriesPayload.payload[0]
{
portalSlug: 'room-rental',
categoryId: 1,
categoryObj: categoriesPayload.payload[0],
}
);
expect(commit.mock.calls).toEqual([
[
types.default.ADD_CATEGORY_FLAG,
{ uiFlags: { isUpdating: true }, categoryId: 1 },
{
uiFlags: {
isUpdating: true,
},
categoryId: 1,
},
],
[types.default.UPDATE_CATEGORY, categoriesPayload.payload[0]],
[types.default.UPDATE_CATEGORY, categoriesPayload.payload],
[
types.default.ADD_CATEGORY_FLAG,
{ uiFlags: { isUpdating: false }, categoryId: 1 },
{
uiFlags: {
isUpdating: false,
},
categoryId: 1,
},
],
]);
});
@@ -79,7 +93,14 @@ describe('#actions', () => {
it('sends correct actions if API is error', async () => {
axios.patch.mockRejectedValue({ message: 'Incorrect header' });
await expect(
actions.update({ commit }, 'web-docs', categoriesPayload.payload[0])
actions.update(
{ commit },
{
portalSlug: 'room-rental',
categoryId: 1,
categoryObj: categoriesPayload.payload[0],
}
)
).rejects.toThrow(Error);
expect(commit.mock.calls).toEqual([
[
@@ -96,11 +117,13 @@ describe('#actions', () => {
describe('#delete', () => {
it('sends correct actions if API is success', async () => {
axios.delete.mockResolvedValue({ data: categoriesPayload.payload[0] });
axios.delete.mockResolvedValue({ data: categoriesPayload });
await actions.delete(
{ commit },
'portal-slug',
categoriesPayload.payload[0].id
{
portalSlug: 'room-rental',
categoryId: categoriesPayload.payload[0].id,
}
);
expect(commit.mock.calls).toEqual([
[
@@ -120,8 +143,10 @@ describe('#actions', () => {
await expect(
actions.delete(
{ commit },
'portal-slug',
categoriesPayload.payload[0].id
{
portalSlug: 'room-rental',
categoryId: categoriesPayload.payload[0].id,
}
)
).rejects.toThrow(Error);
expect(commit.mock.calls).toEqual([

View File

@@ -19,6 +19,10 @@ describe('#getters', () => {
);
});
it('categoriesByLocaleCode', () => {
expect(getters.categoriesByLocaleCode(state, getters)('en_US')).toEqual([]);
});
it('isFetchingCategories', () => {
expect(getters.isFetching(state)).toEqual(true);
});