chore: Execute campaigns based on matching URL (#2254)
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
import Vue from 'vue';
|
||||
import { getCampaigns } from 'widget/api/campaign';
|
||||
import { startTimer } from 'widget/helpers/campaignTimer';
|
||||
|
||||
import campaignTimer from 'widget/helpers/campaignTimer';
|
||||
import {
|
||||
formatCampaigns,
|
||||
filterCampaigns,
|
||||
} from 'widget/helpers/campaignHelper';
|
||||
const state = {
|
||||
records: [],
|
||||
uiFlags: {
|
||||
@@ -16,11 +19,14 @@ export const getters = {
|
||||
};
|
||||
|
||||
export const actions = {
|
||||
fetchCampaigns: async ({ commit }, websiteToken) => {
|
||||
fetchCampaigns: async (
|
||||
{ commit, dispatch },
|
||||
{ websiteToken, currentURL }
|
||||
) => {
|
||||
try {
|
||||
const { data } = await getCampaigns(websiteToken);
|
||||
startTimer({ allCampaigns: data });
|
||||
commit('setCampaigns', data);
|
||||
dispatch('startCampaigns', { currentURL, websiteToken });
|
||||
commit('setError', false);
|
||||
commit('setHasFetched', true);
|
||||
} catch (error) {
|
||||
@@ -28,6 +34,24 @@ export const actions = {
|
||||
commit('setHasFetched', true);
|
||||
}
|
||||
},
|
||||
startCampaigns: async (
|
||||
{ getters: { fetchCampaigns: campagins }, dispatch },
|
||||
{ currentURL, websiteToken }
|
||||
) => {
|
||||
if (!campagins.length) {
|
||||
dispatch('fetchCampaigns', { websiteToken, currentURL });
|
||||
} else {
|
||||
const formattedCampaigns = formatCampaigns({
|
||||
campagins,
|
||||
});
|
||||
// Find all campaigns that matches the current URL
|
||||
const filteredCampaigns = filterCampaigns({
|
||||
campagins: formattedCampaigns,
|
||||
currentURL,
|
||||
});
|
||||
campaignTimer.initTimers({ campagins: filteredCampaigns });
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
export const mutations = {
|
||||
|
||||
Reference in New Issue
Block a user