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,
|
|
|
|
},
|
2021-06-15 14:39:42 +00:00
|
|
|
activeCampaign: {},
|
2021-05-10 07:31:00 +00:00
|
|
|
};
|
|
|
|
|
2021-10-12 12:28:33 +00:00
|
|
|
const resetCampaignTimers = (
|
|
|
|
campaigns,
|
|
|
|
currentURL,
|
|
|
|
websiteToken,
|
|
|
|
isInBusinessHours
|
|
|
|
) => {
|
2021-05-18 06:45:23 +00:00
|
|
|
const formattedCampaigns = formatCampaigns({ campaigns });
|
|
|
|
// Find all campaigns that matches the current URL
|
|
|
|
const filteredCampaigns = filterCampaigns({
|
|
|
|
campaigns: formattedCampaigns,
|
|
|
|
currentURL,
|
2021-10-12 12:28:33 +00:00
|
|
|
isInBusinessHours,
|
2021-05-18 06:45:23 +00:00
|
|
|
});
|
2021-10-11 12:48:11 +00:00
|
|
|
campaignTimer.initTimers({ campaigns: filteredCampaigns }, websiteToken);
|
2021-05-18 06:45:23 +00:00
|
|
|
};
|
|
|
|
|
2021-05-10 07:31:00 +00:00
|
|
|
export const getters = {
|
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-10-12 12:28:33 +00:00
|
|
|
fetchCampaigns: async (
|
|
|
|
{ commit },
|
|
|
|
{ websiteToken, currentURL, isInBusinessHours }
|
|
|
|
) => {
|
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);
|
2021-10-12 12:28:33 +00:00
|
|
|
resetCampaignTimers(
|
|
|
|
campaigns,
|
|
|
|
currentURL,
|
|
|
|
websiteToken,
|
|
|
|
isInBusinessHours
|
|
|
|
);
|
2021-05-10 07:31:00 +00:00
|
|
|
} catch (error) {
|
|
|
|
commit('setError', true);
|
|
|
|
}
|
|
|
|
},
|
2021-06-15 14:39:42 +00:00
|
|
|
initCampaigns: async (
|
2021-05-18 06:45:23 +00:00
|
|
|
{ getters: { getCampaigns: campaigns }, dispatch },
|
2021-10-12 12:28:33 +00:00
|
|
|
{ currentURL, websiteToken, isInBusinessHours }
|
2021-05-17 16:08:35 +00:00
|
|
|
) => {
|
2021-05-18 06:45:23 +00:00
|
|
|
if (!campaigns.length) {
|
2021-10-12 12:28:33 +00:00
|
|
|
dispatch('fetchCampaigns', {
|
|
|
|
websiteToken,
|
|
|
|
currentURL,
|
|
|
|
isInBusinessHours,
|
|
|
|
});
|
2021-05-17 16:08:35 +00:00
|
|
|
} else {
|
2021-10-12 12:28:33 +00:00
|
|
|
resetCampaignTimers(
|
|
|
|
campaigns,
|
|
|
|
currentURL,
|
|
|
|
websiteToken,
|
|
|
|
isInBusinessHours
|
|
|
|
);
|
2021-05-17 16:08:35 +00:00
|
|
|
}
|
|
|
|
},
|
2021-11-11 13:32:16 +00:00
|
|
|
startCampaign: async (
|
|
|
|
{
|
|
|
|
commit,
|
|
|
|
rootState: {
|
2022-01-12 10:55:27 +00:00
|
|
|
appConfig: { isWidgetOpen },
|
2021-11-11 13:32:16 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{ websiteToken, campaignId }
|
|
|
|
) => {
|
|
|
|
// Disable campaign execution if widget is opened
|
2022-01-12 10:55:27 +00:00
|
|
|
if (!isWidgetOpen) {
|
2021-11-11 13:32:16 +00:00
|
|
|
const { data: campaigns } = await getCampaigns(websiteToken);
|
|
|
|
// Check campaign is disabled or not
|
|
|
|
const campaign = campaigns.find(item => item.id === campaignId);
|
|
|
|
if (campaign) {
|
|
|
|
commit('setActiveCampaign', campaign);
|
|
|
|
}
|
2021-10-11 12:48:11 +00:00
|
|
|
}
|
2021-06-15 14:39:42 +00:00
|
|
|
},
|
|
|
|
|
2022-04-20 05:19:52 +00:00
|
|
|
executeCampaign: async (
|
|
|
|
{ commit },
|
|
|
|
{ campaignId, websiteToken, customAttributes }
|
|
|
|
) => {
|
2021-06-15 14:39:42 +00:00
|
|
|
try {
|
2022-01-12 10:55:27 +00:00
|
|
|
commit(
|
|
|
|
'conversation/setConversationUIFlag',
|
|
|
|
{ isCreating: true },
|
|
|
|
{ root: true }
|
|
|
|
);
|
2022-04-20 05:19:52 +00:00
|
|
|
await triggerCampaign({ campaignId, websiteToken, customAttributes });
|
2022-03-15 16:37:30 +00:00
|
|
|
commit('setCampaignExecuted', true);
|
2021-11-11 13:32:16 +00:00
|
|
|
commit('setActiveCampaign', {});
|
|
|
|
} catch (error) {
|
|
|
|
commit('setError', true);
|
2022-01-12 10:55:27 +00:00
|
|
|
} finally {
|
|
|
|
commit(
|
|
|
|
'conversation/setConversationUIFlag',
|
|
|
|
{ isCreating: false },
|
|
|
|
{ root: true }
|
|
|
|
);
|
2021-11-11 13:32:16 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
resetCampaign: async ({ commit }) => {
|
|
|
|
try {
|
2022-03-15 16:37:30 +00:00
|
|
|
commit('setCampaignExecuted', false);
|
2021-06-15 14:39:42 +00:00
|
|
|
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);
|
|
|
|
},
|
2022-03-15 16:37:30 +00:00
|
|
|
setHasFetched($state, value) {
|
|
|
|
Vue.set($state.uiFlags, 'hasFetched', value);
|
|
|
|
},
|
|
|
|
setCampaignExecuted($state, data) {
|
|
|
|
Vue.set($state, 'campaignHasExecuted', data);
|
|
|
|
},
|
2021-05-10 07:31:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default {
|
|
|
|
namespaced: true,
|
|
|
|
state,
|
|
|
|
getters,
|
|
|
|
actions,
|
|
|
|
mutations,
|
|
|
|
};
|