-
Notifications
You must be signed in to change notification settings - Fork 341
Expand file tree
/
Copy pathpublications.js
More file actions
435 lines (378 loc) · 11.3 KB
/
publications.js
File metadata and controls
435 lines (378 loc) · 11.3 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/**
* `modules/reader-revenue-manager` data store: publications.
*
* Site Kit by Google, Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* External dependencies
*/
import invariant from 'invariant';
import { isPlainObject } from 'lodash';
/**
* Internal dependencies.
*/
import { get, set } from 'googlesitekit-api';
import {
commonActions,
combineStores,
createReducer,
createRegistrySelector,
} from 'googlesitekit-data';
import { createFetchStore } from '@/js/googlesitekit/data/create-fetch-store';
import { createValidatedAction } from '@/js/googlesitekit/data/utils';
import { CORE_USER } from '@/js/googlesitekit/datastore/user/constants';
import {
MODULES_READER_REVENUE_MANAGER,
PUBLICATION_ONBOARDING_STATES,
} from './constants';
import { MODULE_SLUG_READER_REVENUE_MANAGER } from '@/js/modules/reader-revenue-manager/constants';
import { actions as errorStoreActions } from '@/js/googlesitekit/data/create-error-store';
import {
getProductIDs,
getPaymentOption,
} from '@/js/modules/reader-revenue-manager/utils/settings';
const fetchGetPublicationsStore = createFetchStore( {
baseName: 'getPublications',
controlCallback: () =>
get(
'modules',
MODULE_SLUG_READER_REVENUE_MANAGER,
'publications',
{},
{ useCache: false }
),
reducerCallback: createReducer( ( state, publications ) => {
state.publications = publications;
if ( state.settings?.publicationID ) {
const publication = publications?.find(
// eslint-disable-next-line sitekit/acronym-case
( { publicationId: id } ) => id === state.settings.publicationID
);
if ( publication ) {
const newSettings = {
publicationOnboardingState: publication.onboardingState,
productIDs: getProductIDs( publication.products ),
paymentOption: getPaymentOption(
publication.paymentOptions
),
};
if ( publication.contentPolicyStatus ) {
newSettings.contentPolicyState =
publication.contentPolicyStatus.contentPolicyState;
newSettings.policyInfoLink =
publication.contentPolicyStatus.policyInfoLink || '';
}
Object.assign( state.settings, newSettings );
if ( state.savedSettings ) {
Object.assign( state.savedSettings, newSettings );
}
}
}
} ),
} );
const fetchGetSyncPublicationOnboardingStateStore = createFetchStore( {
baseName: 'getSyncPublicationOnboardingState',
controlCallback: ( { publicationID, publicationOnboardingState } ) =>
set(
'modules',
MODULE_SLUG_READER_REVENUE_MANAGER,
'sync-publication-onboarding-state',
{
publicationID,
publicationOnboardingState,
}
),
argsToParams: ( { publicationID, publicationOnboardingState } ) => {
return { publicationID, publicationOnboardingState };
},
validateParams: ( { publicationID, publicationOnboardingState } = {} ) => {
invariant(
typeof publicationID === 'string' && publicationID.length > 0,
'publicationID is required and must be string.'
);
invariant(
typeof publicationOnboardingState === 'string' &&
publicationOnboardingState.length > 0,
'publicationOnboardingState is required and must be string.'
);
},
reducerCallback: createReducer(
( state, { publicationID, publicationOnboardingState } ) => {
if ( ! publicationID ) {
return;
}
// eslint-disable-next-line sitekit/no-direct-date
const publicationOnboardingStateLastSyncedAtMs = Date.now();
if ( state.settings.publicationID === publicationID ) {
state.settings.publicationOnboardingState =
publicationOnboardingState;
state.settings.publicationOnboardingStateLastSyncedAtMs =
publicationOnboardingStateLastSyncedAtMs;
}
if ( state.savedSettings.publicationID === publicationID ) {
state.savedSettings.publicationOnboardingState =
publicationOnboardingState;
state.savedSettings.publicationOnboardingStateLastSyncedAtMs =
publicationOnboardingStateLastSyncedAtMs;
}
const publication = state.publications?.find(
// eslint-disable-next-line sitekit/acronym-case
( { publicationId: id } ) => id === publicationID
);
if ( publication ) {
publication.onboardingState = publicationOnboardingState;
}
}
),
} );
const baseInitialState = {
publications: undefined,
};
const baseActions = {
/**
* Synchronizes the onboarding state of the publication with the API.
* Updates the settings on the server.
*
* @since 1.132.0
*
* @return {void}
*/
*syncPublicationOnboardingState() {
const registry = yield commonActions.getRegistry();
yield commonActions.await(
registry
.resolveSelect( MODULES_READER_REVENUE_MANAGER )
.getSettings()
);
const publicationID = registry
.select( MODULES_READER_REVENUE_MANAGER )
.getPublicationID();
const publicationOnboardingState = registry
.select( MODULES_READER_REVENUE_MANAGER )
.getPublicationOnboardingState();
// If there is no publication ID in state, do not attempt to sync
// the onboarding state.
if (
publicationID === undefined ||
publicationOnboardingState === undefined
) {
return {};
}
return yield fetchGetSyncPublicationOnboardingStateStore.actions.fetchGetSyncPublicationOnboardingState(
{
publicationID,
publicationOnboardingState,
}
);
},
/**
* Finds a matched publication.
*
* @since 1.132.0
*
* @return {Object|null} Matched publication; `null` if none found.
*/
*findMatchedPublication() {
const { resolveSelect } = yield commonActions.getRegistry();
const publications = yield commonActions.await(
resolveSelect( MODULES_READER_REVENUE_MANAGER ).getPublications()
);
if ( publications.length === 0 ) {
return null;
}
if ( publications.length === 1 ) {
return publications[ 0 ];
}
const completedOnboardingPublication = publications.find(
( publication ) =>
publication.onboardingState ===
PUBLICATION_ONBOARDING_STATES.ONBOARDING_COMPLETE
);
return completedOnboardingPublication || publications[ 0 ];
},
/**
* Resets the publications data in the store.
*
* @since 1.133.0
*
* @return {Object} The dispatched action results.
*/
*resetPublications() {
const registry = yield commonActions.getRegistry();
yield {
type: 'RESET_PUBLICATIONS',
};
yield errorStoreActions.clearErrors( 'getPublications' );
return registry
.dispatch( MODULES_READER_REVENUE_MANAGER )
.invalidateResolutionForStoreSelector( 'getPublications' );
},
/**
* Sets the given publication in the store.
*
* @since 1.133.0
*
* @param {Object} publication The publiation object.
* @return {Object} A Generator function.
*/
selectPublication: createValidatedAction(
( publication ) => {
invariant(
isPlainObject( publication ),
'A valid publication object is required.'
);
[ 'publicationId', 'onboardingState' ].forEach( ( key ) => {
invariant(
publication.hasOwnProperty( key ),
`The publication object must contain ${ key }`
);
} );
},
function* ( {
// `publicationId` is the identifier used by the API.
// eslint-disable-next-line sitekit/acronym-case
publicationId: publicationID,
onboardingState,
paymentOptions,
products,
contentPolicyStatus,
} ) {
const registry = yield commonActions.getRegistry();
const settings = {
publicationID,
publicationOnboardingState: onboardingState,
publicationOnboardingStateChanged: false,
productIDs: getProductIDs( products ),
paymentOption: getPaymentOption( paymentOptions ),
productID: 'openaccess',
};
if ( contentPolicyStatus ) {
settings.contentPolicyState =
contentPolicyStatus.contentPolicyState;
settings.policyInfoLink =
contentPolicyStatus.policyInfoLink || '';
}
return registry
.dispatch( MODULES_READER_REVENUE_MANAGER )
.setSettings( settings );
}
),
};
const baseControls = {};
const baseReducer = createReducer( ( state, { type } ) => {
switch ( type ) {
case 'RESET_PUBLICATIONS':
state.publications = baseInitialState.publications;
break;
default:
break;
}
} );
const baseResolvers = {
*getPublications() {
const registry = yield commonActions.getRegistry();
// Only fetch publications if there are none in the store.
const publications = registry
.select( MODULES_READER_REVENUE_MANAGER )
.getPublications();
if ( publications === undefined ) {
yield fetchGetPublicationsStore.actions.fetchGetPublications();
}
},
};
const baseSelectors = {
/**
* Gets list of publications associated with the account.
*
* @since 1.132.0
*
* @param {Object} state Data store's state.
* @return {(Array.<Object>|undefined)} An array of publications; `undefined` if not loaded.
*/
getPublications( state ) {
return state.publications;
},
/**
* Gets the current publication IDs.
*
* @since 1.150.0
*
* @param {Object} state Data store's state.
* @return {(Array.<string> | undefined)} An array of product IDs; `undefined` if publications are not loaded.
*/
getCurrentProductIDs: createRegistrySelector( ( select ) => ( state ) => {
const publications = select(
MODULES_READER_REVENUE_MANAGER
).getPublications();
if ( publications === undefined ) {
return undefined;
}
const publicationID = select(
MODULES_READER_REVENUE_MANAGER
).getPublicationID();
if ( ! publicationID ) {
return [];
}
const selectedPublication = state.publications.find(
// eslint-disable-next-line sitekit/acronym-case
( { publicationId: id } ) => id === publicationID
);
if ( ! selectedPublication || ! selectedPublication.products ) {
return [];
}
return selectedPublication.products.map( ( product ) => product.name );
} ),
/**
* Gets the policy info URL wrapped with the account chooser URL.
*
* @since 1.171.0
*
* @param {Object} state Data store's state.
* @return {(string|null|undefined)} The policy info URL wrapped with the account chooser URL; `null` if `policyInfoLink` is empty; `undefined` if not available.
*/
getPolicyInfoURL: createRegistrySelector( ( select ) => () => {
const settings = select( MODULES_READER_REVENUE_MANAGER ).getSettings();
if ( ! settings ) {
return undefined;
}
const { policyInfoLink } = settings;
if ( policyInfoLink === undefined ) {
return undefined;
}
if ( ! policyInfoLink ) {
return null;
}
return select( CORE_USER ).getAccountChooserURL( policyInfoLink );
} ),
};
const store = combineStores(
fetchGetPublicationsStore,
fetchGetSyncPublicationOnboardingStateStore,
{
initialState: baseInitialState,
actions: baseActions,
controls: baseControls,
reducer: baseReducer,
resolvers: baseResolvers,
selectors: baseSelectors,
}
);
export const initialState = store.initialState;
export const actions = store.actions;
export const controls = store.controls;
export const reducer = store.reducer;
export const resolvers = store.resolvers;
export const selectors = store.selectors;
export default store;