2021-05-10 07:31:00 +00:00
|
|
|
import Vue from 'vue';
|
2021-06-15 14:39:42 +00:00
|
|
|
import { getCampaigns, triggerCampaign } from 'widget/api/campaign';
|
2021-05-17 16:08:35 +00:00
|
|
|
import campaignTimer from 'widget/helpers/campaignTimer';
|
|
|
|
import {
|
|
|
|
formatCampaigns,
|
|
|
|
filterCampaigns,
|
|
|
|
} from 'widget/helpers/campaignHelper';
|
2021-05-10 07:31:00 +00:00
|
|
|
const state = {
|
|
|
|
records: [],
|
|
|
|
uiFlags: {
|
|
|
|
isError: false,
|
|
|
|
hasFetched: false,
|
|
|
|
},
|
2021-06-15 14:39:42 +00:00
|
|
|
activeCampaign: {},
|
2021-05-10 07:31:00 +00:00
|
|
|
};
|
|
|
|
|
2021-05-18 06:45:23 +00:00
|
|
|
const resetCampaignTimers = (campaigns, currentURL) => {
|
|
|
|
const formattedCampaigns = formatCampaigns({ campaigns });
|
|
|
|
// Find all campaigns that matches the current URL
|
|
|
|
const filteredCampaigns = filterCampaigns({
|
|
|
|
campaigns: formattedCampaigns,
|
|
|
|
currentURL,
|
|
|
|
});
|
|
|
|
campaignTimer.initTimers({ campaigns: filteredCampaigns });
|
|
|
|
};
|
|
|
|
|
2021-05-10 07:31:00 +00:00
|
|
|
export const getters = {
|
|
|
|
getHasFetched: $state => $state.uiFlags.hasFetched,
|
2021-05-18 06:45:23 +00:00
|
|
|
getCampaigns: $state => $state.records,
|
2021-06-15 14:39:42 +00:00
|
|
|
getActiveCampaign: $state => $state.activeCampaign,
|
2021-05-10 07:31:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const actions = {
|
2021-05-18 06:45:23 +00:00
|
|
|
fetchCampaigns: async ({ commit }, { websiteToken, currentURL }) => {
|
2021-05-10 07:31:00 +00:00
|
|
|
try {
|
2021-05-18 06:45:23 +00:00
|
|
|
const { data: campaigns } = await getCampaigns(websiteToken);
|
|
|
|
commit('setCampaigns', campaigns);
|
2021-05-10 07:31:00 +00:00
|
|
|
commit('setError', false);
|
|
|
|
commit('setHasFetched', true);
|
2021-05-18 06:45:23 +00:00
|
|
|
resetCampaignTimers(campaigns, currentURL);
|
2021-05-10 07:31:00 +00:00
|
|
|
} catch (error) {
|
|
|
|
commit('setError', true);
|
|
|
|
commit('setHasFetched', true);
|
|
|
|
}
|
|
|
|
},
|
2021-06-15 14:39:42 +00:00
|
|
|
initCampaigns: async (
|
2021-05-18 06:45:23 +00:00
|
|
|
{ getters: { getCampaigns: campaigns }, dispatch },
|
2021-05-17 16:08:35 +00:00
|
|
|
{ currentURL, websiteToken }
|
|
|
|
) => {
|
2021-05-18 06:45:23 +00:00
|
|
|
if (!campaigns.length) {
|
2021-05-17 16:08:35 +00:00
|
|
|
dispatch('fetchCampaigns', { websiteToken, currentURL });
|
|
|
|
} else {
|
2021-05-18 06:45:23 +00:00
|
|
|
resetCampaignTimers(campaigns, currentURL);
|
2021-05-17 16:08:35 +00:00
|
|
|
}
|
|
|
|
},
|
2021-06-15 14:39:42 +00:00
|
|
|
startCampaign: async (
|
|
|
|
{ getters: { getCampaigns: campaigns }, commit },
|
|
|
|
{ campaignId }
|
|
|
|
) => {
|
|
|
|
const campaign = campaigns.find(item => item.id === campaignId);
|
|
|
|
commit('setActiveCampaign', campaign);
|
|
|
|
},
|
|
|
|
|
|
|
|
executeCampaign: async ({ commit }, { campaignId, websiteToken }) => {
|
|
|
|
try {
|
|
|
|
await triggerCampaign({ campaignId, websiteToken });
|
|
|
|
commit('setActiveCampaign', {});
|
|
|
|
} catch (error) {
|
|
|
|
commit('setError', true);
|
|
|
|
}
|
|
|
|
},
|
2021-05-10 07:31:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const mutations = {
|
|
|
|
setCampaigns($state, data) {
|
|
|
|
Vue.set($state, 'records', data);
|
|
|
|
},
|
2021-06-15 14:39:42 +00:00
|
|
|
setActiveCampaign($state, data) {
|
|
|
|
Vue.set($state, 'activeCampaign', data);
|
|
|
|
},
|
2021-05-10 07:31:00 +00:00
|
|
|
setError($state, value) {
|
|
|
|
Vue.set($state.uiFlags, 'isError', value);
|
|
|
|
},
|
|
|
|
setHasFetched($state, value) {
|
|
|
|
Vue.set($state.uiFlags, 'hasFetched', value);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export default {
|
|
|
|
namespaced: true,
|
|
|
|
state,
|
|
|
|
getters,
|
|
|
|
actions,
|
|
|
|
mutations,
|
|
|
|
};
|