-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathselectors.js
237 lines (206 loc) · 7.03 KB
/
selectors.js
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/**
* External dependencies
*/
import { createRegistrySelector } from '@wordpress/data';
import createSelector from 'rememo';
/**
* Internal dependencies
*/
import { STORE_KEY } from './constants';
import { getReportQuery, getReportKey, getPerformanceQuery } from './utils';
export const getShippingRates = ( state ) => {
return state.mc.shipping.rates;
};
export const getShippingTimes = ( state ) => {
return state.mc.shipping.times;
};
export const getSettings = ( state ) => {
return state.mc.settings;
};
/**
* @typedef {Object} JetpackAccount
* @property {'yes'|'no'} active Whether jetpack is connected.
* @property {'yes'|'no'} owner Whether the current admin user is the jetpack owner.
* @property {string|''} email Owner email. Available for jetpack owner.
* @property {string|''} displayName Owner name. Available for jetpack owner.
*/
/**
* Select jetpack connection state.
*
* @param {Object} state The current store state will be injected by `wp.data`.
* @return {JetpackAccount|null} The jetpack connection state. It would return `null` before the data is fetched.
*/
export const getJetpackAccount = ( state ) => {
return state.mc.accounts.jetpack;
};
export const getGoogleAccount = ( state ) => {
return state.mc.accounts.google;
};
export const getGoogleAccountAccess = ( state ) => {
return state.mc.accounts.google_access;
};
export const getGoogleMCAccount = ( state ) => {
return state.mc.accounts.mc;
};
export const getExistingGoogleMCAccounts = ( state ) => {
return state.mc.accounts.existing_mc;
};
export const getGoogleAdsAccount = ( state ) => {
return state.mc.accounts.ads;
};
export const getGoogleAdsAccountBillingStatus = ( state ) => {
return state.mc.accounts.ads_billing_status;
};
export const getExistingGoogleAdsAccounts = ( state ) => {
return state.mc.accounts.existing_ads;
};
const mockPhoneNumber = Math.random() > 0.5 ? '+12133734253' : '';
export const getGoogleMCPhoneNumber = () => {
// TODO: [lite-contact-info] integrate with API
return {
id: '123456789',
phone_number: mockPhoneNumber,
};
};
export const getCountries = ( state ) => {
return state.mc.countries;
};
export const getTargetAudience = ( state ) => {
return state.mc.target_audience;
};
export const getAdsCampaigns = ( state ) => {
return state.ads_campaigns;
};
export const getMCSetup = ( state ) => {
return state.mc_setup;
};
export const getMCProductStatistics = ( state ) => {
return state.mc_product_statistics;
};
// note: we use rememo createSelector here to cache the sliced issues array,
// to prevent returning new array to the consumer every time,
// which might cause rendering performance problem.
export const getMCIssues = createSelector(
( state, query ) => {
if ( ! state.mc_issues ) {
return state.mc_issues;
}
const start = ( query.page - 1 ) * query.per_page;
const end = start + query.per_page;
return {
issues: state.mc_issues.issues.slice( start, end ),
total: state.mc_issues.total,
};
},
( state ) => [
state.mc_issues,
state.mc_issues?.issues,
state.mc_issues?.total,
]
);
export const getMCProductFeed = ( state, query ) => {
if ( ! state.mc_product_feed ) {
return state.mc_product_feed;
}
return {
products: state.mc_product_feed.pages[ query.page ],
total: state.mc_product_feed.total,
};
};
/**
* @typedef {Object} ReportQuery
* @property {string} after Start date in 'YYYY-MM-DD' format.
* @property {string} before End date in 'YYYY-MM-DD' format.
* @property {Array<string>} fields An array of performance metrics field to retrieve.
* @property {string} [ids] Filter product or campaign by a comma separated list of IDs.
* @property {string} [orderby] Column to order the results by, this must be one of the fields in requesting.
* @property {string} [order] Results order, 'desc' or 'asc'.
* @property {string} [interval] How to segment the data. Note that the 'free' type data only supports segmenting by day,
* but the 'paid' type report allows any of the following values:
* 'day', 'week', 'month', 'quarter', 'year'
*/
/**
* Select report data according to parameters and report API query.
*
* @param {Object} state The current store state will be injected by `wp.data`.
* @param {string} category Category of report, 'programs' or 'products'.
* @param {string} type Type of report, 'free' or 'paid'.
* @param {ReportQuery} reportQuery Query options of report API.
*
* @return {Object|null} The report data of specified parameters. It would return `null` before the data is fetched.
*/
export const getReportByApiQuery = ( state, category, type, reportQuery ) => {
const reportKey = getReportKey( category, type, reportQuery );
return state.report[ reportKey ] || null;
};
/**
* @typedef {Object} ReportSchema
* @property {boolean} loaded Whether the data have been loaded.
* @property {ReportData} data Fetched report data.
* @property {ReportQuery} reportQuery The actual, resolved query used to request the report. Available synchronously.
* @template ReportData
*/
/**
* Select report data according to parameters and URL query.
*
* @param {Object} state The current store state will be injected by `wp.data`.
* @param {string} category Category of report, 'programs' or 'products'.
* @param {string} type Type of report, 'free' or 'paid'.
* @param {Object} query Query parameters in the URL.
* @param {string} dateReference Which date range to use, 'primary' or 'secondary'.
*
* @return {ReportSchema} Report data.
*/
export const getReport = createRegistrySelector(
( select ) => ( state, category, type, query, dateReference ) => {
const selector = select( STORE_KEY );
const reportQuery = getReportQuery(
category,
type,
query,
dateReference
);
const args = [ category, type, reportQuery ];
return {
reportQuery,
loaded: selector.hasFinishedResolution(
'getReportByApiQuery',
args
),
data: selector.getReportByApiQuery( ...args ),
};
}
);
/**
* @typedef {Object} PerformaceData
* @property {boolean} loaded Whether the data have been loaded.
* @property {Object|null} data The programs performace data of specified parameters. It would return `null` before the data is fetched.
*/
/**
* Select programs performace data according to parameters.
*
* @param {Object} state The current store state will be injected by `wp.data`.
* @param {string} type Type of report, 'free' or 'paid'.
* @param {Object} query Query parameters in the URL.
* @param {string} dateReference Which date range to use, 'primary' or 'secondary'.
*
* @return {PerformaceData} Performace data.
*/
export const getDashboardPerformance = createRegistrySelector(
( select ) => ( state, type, query, dateReference ) => {
const selector = select( STORE_KEY );
const args = [
'programs',
type,
getPerformanceQuery( type, query, dateReference ),
];
const report = selector.getReportByApiQuery( ...args );
return {
data: report ? report.totals : null,
loaded: selector.hasFinishedResolution(
'getReportByApiQuery',
args
),
};
}
);