Chatwoot/app/javascript/dashboard/store/modules/campaigns.js

92 lines
2.4 KiB
JavaScript
Raw Normal View History

2021-05-04 09:38:41 +00:00
import * as MutationHelpers from 'shared/helpers/vuex/mutationHelpers';
import types from '../mutation-types';
import CampaignsAPI from '../../api/campaigns';
export const state = {
records: [],
uiFlags: {
isFetching: false,
isCreating: false,
},
};
export const getters = {
getUIFlags(_state) {
return _state.uiFlags;
},
getCampaigns: _state => campaignType => {
return _state.records.filter(
record => record.campaign_type === campaignType
);
2021-05-04 09:38:41 +00:00
},
};
export const actions = {
get: async function getCampaigns({ commit }) {
2021-05-04 09:38:41 +00:00
commit(types.SET_CAMPAIGN_UI_FLAG, { isFetching: true });
try {
const response = await CampaignsAPI.get();
2021-05-04 09:38:41 +00:00
commit(types.SET_CAMPAIGNS, response.data);
} catch (error) {
// Ignore error
} finally {
commit(types.SET_CAMPAIGN_UI_FLAG, { isFetching: false });
}
},
create: async function createCampaign({ commit }, campaignObj) {
commit(types.SET_CAMPAIGN_UI_FLAG, { isCreating: true });
try {
const response = await CampaignsAPI.create(campaignObj);
commit(types.ADD_CAMPAIGN, response.data);
} catch (error) {
throw new Error(error);
} finally {
commit(types.SET_CAMPAIGN_UI_FLAG, { isCreating: false });
}
},
update: async ({ commit }, { id, ...updateObj }) => {
commit(types.SET_CAMPAIGN_UI_FLAG, { isUpdating: true });
try {
const response = await CampaignsAPI.update(id, updateObj);
commit(types.EDIT_CAMPAIGN, response.data);
} catch (error) {
throw new Error(error);
} finally {
commit(types.SET_CAMPAIGN_UI_FLAG, { isUpdating: false });
}
},
delete: async ({ commit }, id) => {
commit(types.SET_CAMPAIGN_UI_FLAG, { isDeleting: true });
try {
await CampaignsAPI.delete(id);
commit(types.DELETE_CAMPAIGN, id);
} catch (error) {
throw new Error(error);
} finally {
commit(types.SET_CAMPAIGN_UI_FLAG, { isDeleting: false });
}
},
2021-05-04 09:38:41 +00:00
};
export const mutations = {
[types.SET_CAMPAIGN_UI_FLAG](_state, data) {
_state.uiFlags = {
..._state.uiFlags,
...data,
};
},
[types.ADD_CAMPAIGN]: MutationHelpers.create,
[types.SET_CAMPAIGNS]: MutationHelpers.set,
[types.EDIT_CAMPAIGN]: MutationHelpers.update,
[types.DELETE_CAMPAIGN]: MutationHelpers.destroy,
2021-05-04 09:38:41 +00:00
};
export default {
namespaced: true,
actions,
state,
getters,
mutations,
};