mirror of
https://github.com/lingble/chatwoot.git
synced 2025-11-02 12:08:01 +00:00
<img width="1439" alt="Screenshot 2024-10-30 at 8 58 12 PM" src="https://github.com/user-attachments/assets/26231270-5e73-40fb-9efa-c661585ebe7c"> Fixes https://linear.app/chatwoot/project/campaign-redesign-f82bede26ca7/overview --------- Co-authored-by: Pranav <pranavrajs@gmail.com> Co-authored-by: Shivam Mishra <scm.mymail@gmail.com>
99 lines
2.7 KiB
JavaScript
99 lines
2.7 KiB
JavaScript
import * as MutationHelpers from 'shared/helpers/vuex/mutationHelpers';
|
|
import types from '../mutation-types';
|
|
import CampaignsAPI from '../../api/campaigns';
|
|
import AnalyticsHelper from '../../helper/AnalyticsHelper';
|
|
import { CAMPAIGNS_EVENTS } from '../../helper/AnalyticsHelper/events';
|
|
|
|
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)
|
|
.sort((a1, a2) => a1.id - a2.id);
|
|
},
|
|
getAllCampaigns: _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 });
|
|
}
|
|
},
|
|
update: async ({ commit }, { id, ...updateObj }) => {
|
|
commit(types.SET_CAMPAIGN_UI_FLAG, { isUpdating: true });
|
|
try {
|
|
const response = await CampaignsAPI.update(id, updateObj);
|
|
AnalyticsHelper.track(CAMPAIGNS_EVENTS.UPDATE_CAMPAIGN);
|
|
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);
|
|
AnalyticsHelper.track(CAMPAIGNS_EVENTS.DELETE_CAMPAIGN);
|
|
commit(types.DELETE_CAMPAIGN, id);
|
|
} catch (error) {
|
|
throw new Error(error);
|
|
} finally {
|
|
commit(types.SET_CAMPAIGN_UI_FLAG, { isDeleting: 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,
|
|
[types.DELETE_CAMPAIGN]: MutationHelpers.destroy,
|
|
};
|
|
|
|
export default {
|
|
namespaced: true,
|
|
actions,
|
|
state,
|
|
getters,
|
|
mutations,
|
|
};
|