2021-08-31 08:24:34 +00:00
|
|
|
import * as MutationHelpers from 'shared/helpers/vuex/mutationHelpers';
|
|
|
|
import types from '../mutation-types';
|
|
|
|
import AttributeAPI from '../../api/attributes';
|
|
|
|
|
|
|
|
export const state = {
|
|
|
|
records: [],
|
|
|
|
uiFlags: {
|
|
|
|
isFetching: false,
|
|
|
|
isCreating: false,
|
2021-09-13 10:26:18 +00:00
|
|
|
isUpdating: false,
|
|
|
|
isDeleting: false,
|
2021-08-31 08:24:34 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getters = {
|
|
|
|
getUIFlags(_state) {
|
|
|
|
return _state.uiFlags;
|
|
|
|
},
|
2021-09-08 04:07:58 +00:00
|
|
|
getAttributesByModel: _state => attributeModel => {
|
2021-08-31 08:24:34 +00:00
|
|
|
return _state.records.filter(
|
2021-09-08 04:07:58 +00:00
|
|
|
record => record.attribute_model === attributeModel
|
2021-08-31 08:24:34 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export const actions = {
|
2021-11-11 09:53:33 +00:00
|
|
|
get: async function getAttributesByModel({ commit }) {
|
2021-08-31 08:24:34 +00:00
|
|
|
commit(types.SET_CUSTOM_ATTRIBUTE_UI_FLAG, { isFetching: true });
|
|
|
|
try {
|
2021-11-11 09:53:33 +00:00
|
|
|
const response = await AttributeAPI.getAttributesByModel();
|
2021-08-31 08:24:34 +00:00
|
|
|
commit(types.SET_CUSTOM_ATTRIBUTE, response.data);
|
|
|
|
} catch (error) {
|
|
|
|
// Ignore error
|
|
|
|
} finally {
|
|
|
|
commit(types.SET_CUSTOM_ATTRIBUTE_UI_FLAG, { isFetching: false });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
create: async function createAttribute({ commit }, attributeObj) {
|
|
|
|
commit(types.SET_CUSTOM_ATTRIBUTE_UI_FLAG, { isCreating: true });
|
|
|
|
try {
|
|
|
|
const response = await AttributeAPI.create(attributeObj);
|
|
|
|
commit(types.ADD_CUSTOM_ATTRIBUTE, response.data);
|
|
|
|
} catch (error) {
|
2021-11-15 07:15:47 +00:00
|
|
|
const errorMessage = error?.response?.data?.message;
|
|
|
|
throw new Error(errorMessage);
|
2021-08-31 08:24:34 +00:00
|
|
|
} finally {
|
|
|
|
commit(types.SET_CUSTOM_ATTRIBUTE_UI_FLAG, { isCreating: false });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
update: async ({ commit }, { id, ...updateObj }) => {
|
|
|
|
commit(types.SET_CUSTOM_ATTRIBUTE_UI_FLAG, { isUpdating: true });
|
|
|
|
try {
|
|
|
|
const response = await AttributeAPI.update(id, updateObj);
|
|
|
|
commit(types.EDIT_CUSTOM_ATTRIBUTE, response.data);
|
|
|
|
} catch (error) {
|
2021-11-15 07:15:47 +00:00
|
|
|
const errorMessage = error?.response?.data?.message;
|
|
|
|
throw new Error(errorMessage);
|
2021-08-31 08:24:34 +00:00
|
|
|
} finally {
|
|
|
|
commit(types.SET_CUSTOM_ATTRIBUTE_UI_FLAG, { isUpdating: false });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
delete: async ({ commit }, id) => {
|
|
|
|
commit(types.SET_CUSTOM_ATTRIBUTE_UI_FLAG, { isDeleting: true });
|
|
|
|
try {
|
|
|
|
await AttributeAPI.delete(id);
|
|
|
|
commit(types.DELETE_CUSTOM_ATTRIBUTE, id);
|
|
|
|
} catch (error) {
|
|
|
|
throw new Error(error);
|
|
|
|
} finally {
|
|
|
|
commit(types.SET_CUSTOM_ATTRIBUTE_UI_FLAG, { isDeleting: false });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export const mutations = {
|
|
|
|
[types.SET_CUSTOM_ATTRIBUTE_UI_FLAG](_state, data) {
|
|
|
|
_state.uiFlags = {
|
|
|
|
..._state.uiFlags,
|
|
|
|
...data,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
[types.ADD_CUSTOM_ATTRIBUTE]: MutationHelpers.create,
|
|
|
|
[types.SET_CUSTOM_ATTRIBUTE]: MutationHelpers.set,
|
|
|
|
[types.EDIT_CUSTOM_ATTRIBUTE]: MutationHelpers.update,
|
|
|
|
[types.DELETE_CUSTOM_ATTRIBUTE]: MutationHelpers.destroy,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default {
|
|
|
|
namespaced: true,
|
|
|
|
actions,
|
|
|
|
state,
|
|
|
|
getters,
|
|
|
|
mutations,
|
|
|
|
};
|