/
index.ts
143 lines (126 loc) · 3.56 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { dispatch, select } from '@wordpress/data';
import { getCurrentDates } from '@woocommerce/date';
import { __ } from '@wordpress/i18n';
import { addQueryArgs } from '@wordpress/url';
import { apiFetch } from '@wordpress/data-controls';
import { Hooks } from 'wp-js-hooks';
import { CurrentView, Query, Section, SectionData } from '../types';
import { storeName } from '../constants';
import { getSampleData } from '../samples';
import { storeName as editorStoreName } from '../../../../../editor/store/constants';
export function setQuery(query: Query) {
const sections = select(storeName).getSections();
sections.forEach((section: Section) => {
void dispatch(storeName).updateSection(section, query);
});
return {
type: 'SET_QUERY',
payload: query,
};
}
export function setSectionData(payload: Section) {
return {
type: 'SET_SECTION_DATA',
payload,
};
}
export function resetSectionData(section: Section) {
const payload = {
...section,
data: undefined,
};
return {
type: 'SET_SECTION_DATA',
payload,
};
}
export function updateCurrentView(sectionId: string, currentView: CurrentView) {
const currentSection = select(storeName).getSection(sectionId);
const payload = {
...currentSection,
currentView,
};
return {
type: 'SET_SECTION_DATA',
payload,
};
}
export function* updateSection(
section: Section,
queryParam: Query | undefined = undefined,
) {
void dispatch(storeName).resetSectionData(section);
const sampleData = Hooks.applyFilters(
'mailpoet_analytics_section_sample_data',
getSampleData(section.id),
section.id,
) as SectionData;
if (sampleData) {
return {
type: 'SET_SECTION_DATA',
payload: { ...section, data: sampleData },
};
}
const formatDate = (date: Date, endOfDay = false): string => {
const newDate = new Date(date.getTime());
if (endOfDay) {
newDate.setUTCHours(23, 59, 59, 999);
} else {
newDate.setUTCHours(0, 0, 0, 0);
}
return newDate.toISOString();
};
const query = queryParam ?? select(storeName).getCurrentQuery();
const defaultDateRange = 'period=month&compare=previous_year';
const { primary: primaryDate, secondary: secondaryDate } = getCurrentDates(
query,
defaultDateRange,
);
const dates = {
primary: {
after: formatDate(primaryDate.after.toDate()),
before: formatDate(primaryDate.before.toDate(), true),
},
...(section.withPreviousData
? {
secondary: {
after: formatDate(secondaryDate.after.toDate()),
before: formatDate(secondaryDate.before.toDate(), true),
},
}
: {}),
};
const id = select(editorStoreName).getAutomationData().id;
const customQuery = section.customQuery ?? {};
const args = { id, query: { ...dates, ...customQuery } };
const response: { data: SectionData } = yield apiFetch({
path: addQueryArgs(section.endpoint, args),
method: 'GET',
});
if (section?.updateCallback) {
section.updateCallback(response?.data);
}
return {
type: 'SET_SECTION_DATA',
payload: { ...section, data: response?.data },
};
}
export function openPremiumModal(content: JSX.Element, utmCampaign?: string) {
return {
type: 'OPEN_PREMIUM_MODAL',
content,
utmCampaign,
};
}
export function openPremiumModalForSampleData() {
return {
type: 'OPEN_PREMIUM_MODAL',
content: __("You're viewing sample data.", 'mailpoet'),
utmCampaign: 'automation_analytics_sample_data',
};
}
export function closePremiumModal() {
return {
type: 'CLOSE_PREMIUM_MODAL',
};
}