feat: Add campaign (#2177)
This commit is contained in:
65
app/javascript/dashboard/store/modules/campaigns.js
Normal file
65
app/javascript/dashboard/store/modules/campaigns.js
Normal file
@@ -0,0 +1,65 @@
|
||||
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) {
|
||||
return _state.records;
|
||||
},
|
||||
};
|
||||
|
||||
export const actions = {
|
||||
get: async function getCampaigns({ commit }) {
|
||||
commit(types.SET_CAMPAIGN_UI_FLAG, { isFetching: true });
|
||||
try {
|
||||
const response = await CampaignsAPI.get();
|
||||
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 });
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
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,
|
||||
};
|
||||
|
||||
export default {
|
||||
namespaced: true,
|
||||
actions,
|
||||
state,
|
||||
getters,
|
||||
mutations,
|
||||
};
|
||||
Reference in New Issue
Block a user