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;
|
|
|
|
},
|
2021-08-11 14:59:33 +00:00
|
|
|
getCampaigns: _state => campaignType => {
|
|
|
|
return _state.records.filter(
|
|
|
|
record => record.campaign_type === campaignType
|
|
|
|
);
|
2021-05-04 09:38:41 +00:00
|
|
|
},
|
2021-11-18 13:45:02 +00:00
|
|
|
getAllCampaigns: _state => {
|
|
|
|
return _state.records;
|
|
|
|
},
|
2021-05-04 09:38:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const actions = {
|
2021-08-11 14:59:33 +00:00
|
|
|
get: async function getCampaigns({ commit }) {
|
2021-05-04 09:38:41 +00:00
|
|
|
commit(types.SET_CAMPAIGN_UI_FLAG, { isFetching: true });
|
|
|
|
try {
|
2021-08-11 14:59:33 +00:00
|
|
|
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 });
|
|
|
|
}
|
|
|
|
},
|
2021-05-10 07:10:48 +00:00
|
|
|
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 });
|
|
|
|
}
|
|
|
|
},
|
2021-06-15 08:44:59 +00:00
|
|
|
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,
|
2021-05-10 07:10:48 +00:00
|
|
|
[types.EDIT_CAMPAIGN]: MutationHelpers.update,
|
2021-06-15 08:44:59 +00:00
|
|
|
[types.DELETE_CAMPAIGN]: MutationHelpers.destroy,
|
2021-05-04 09:38:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default {
|
|
|
|
namespaced: true,
|
|
|
|
actions,
|
|
|
|
state,
|
|
|
|
getters,
|
|
|
|
mutations,
|
|
|
|
};
|