Chatwoot/app/javascript/dashboard/store/modules/campaigns.js
2021-05-17 21:38:12 +05:30

78 lines
2 KiB
JavaScript

import * as MutationHelpers from 'shared/helpers/vuex/mutationHelpers';
import types from '../mutation-types';
import CampaignsAPI from '../../api/campaigns';
import InboxesAPI from '../../api/inboxes';
export const state = {
records: [],
uiFlags: {
isFetching: false,
isCreating: false,
},
};
export const getters = {
getUIFlags(_state) {
return _state.uiFlags;
},
getCampaigns(_state) {
return _state.records;
},
};
export const actions = {
get: async function getCampaigns({ commit }, { inboxId }) {
commit(types.SET_CAMPAIGN_UI_FLAG, { isFetching: true });
try {
const response = await InboxesAPI.getCampaigns(inboxId);
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 });
}
},
};
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,
};
export default {
namespaced: true,
actions,
state,
getters,
mutations,
};