-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathselectors.js
559 lines (497 loc) · 16.1 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
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/** @format */
/**
* External dependencies
*/
import { filter, find, has, get, includes, isEqual, omit, some } from 'lodash';
import createSelector from 'lib/create-selector';
import { moment } from 'i18n-calypso';
/**
* Internal dependencies
*/
import {
getNormalizedPostsQuery,
getSerializedPostsQuery,
getDeserializedPostsQueryDetails,
getSerializedPostsQueryWithoutPage,
isAuthorEqual,
isDateEqual,
isDiscussionEqual,
areAllMetadataEditsApplied,
applyPostEdits,
normalizePostForEditing,
normalizePostForDisplay,
} from './utils';
import { decodeURIIfValid } from 'lib/url';
import { getSite } from 'state/sites/selectors';
import { DEFAULT_POST_QUERY, DEFAULT_NEW_POST_VALUES } from './constants';
import { addQueryArgs } from 'lib/route';
import { getFeaturedImageId } from 'lib/posts/utils';
/**
* Returns the PostsQueryManager from the state tree for a given site ID (or
* for queries related to all sites at once).
*
* @param {Object} state Global state tree
* @param {?Number} siteId Site ID, or `null` for all-sites queries
* @return {Object} Posts query manager
*/
function getQueryManager( state, siteId ) {
if ( ! siteId ) {
return state.posts.allSitesQueries;
}
return state.posts.queries[ siteId ] || null;
}
/**
* Returns a post object by its global ID.
*
* @param {Object} state Global state tree
* @param {String} globalId Post global ID
* @return {Object} Post object
*/
export function getPost( state, globalId ) {
const path = state.posts.items[ globalId ];
if ( ! path ) {
return null;
}
const [ siteId, postId ] = path;
const manager = state.posts.queries[ siteId ];
if ( ! manager ) {
return null;
}
return manager.getItem( postId );
}
/**
* Returns a normalized post object by its global ID, or null if the post does
* not exist. A normalized post includes common transformations to prepare the
* post for display.
*
* @param {Object} state Global state tree
* @param {String} globalId Post global ID
* @return {?Object} Post object
*/
export const getNormalizedPost = createSelector(
( state, globalId ) => normalizePostForDisplay( getPost( state, globalId ) ),
state => [ state.posts.items, state.posts.queries ]
);
/**
* Returns an array of post objects by site ID.
*
* @param {Object} state Global state tree
* @param {Number} siteId Site ID
* @return {Array} Site posts
*/
export const getSitePosts = createSelector( ( state, siteId ) => {
if ( ! siteId ) {
return null;
}
const manager = state.posts.queries[ siteId ];
if ( ! manager ) {
return [];
}
return manager.getItems();
}, state => state.posts.queries );
/**
* Returns a post object by site ID, post ID pair.
*
* @param {Object} state Global state tree
* @param {Number} siteId Site ID
* @param {String} postId Post ID
* @return {?Object} Post object
*/
export const getSitePost = createSelector( ( state, siteId, postId ) => {
if ( ! siteId ) {
return null;
}
const manager = state.posts.queries[ siteId ];
if ( ! manager ) {
return null;
}
return manager.getItem( postId );
}, state => state.posts.queries );
/**
* Returns an array of normalized posts for the posts query, or null if no
* posts have been received.
*
* @param {Object} state Global state tree
* @param {?Number} siteId Site ID, or `null` for all-sites queries
* @param {Object} query Post query object
* @return {?Array} Posts for the post query
*/
export const getPostsForQuery = createSelector(
( state, siteId, query ) => {
const manager = getQueryManager( state, siteId );
if ( ! manager ) {
return null;
}
const posts = manager.getItems( query );
if ( ! posts ) {
return null;
}
// PostQueryManager will return an array including undefined entries if
// it knows that a page of results exists for the query (via a previous
// request's `found` value) but the items haven't been received. While
// we could impose this on the developer to accommodate, instead we
// simply return null when any `undefined` entries exist in the set.
//
// TODO this is known to be incorrect behavior in some cases, because
// the WP.com API skips unreadable posts entirely instead of including
// them in the results. See the 'handles items missing from the first
// and last pages' test case for PaginatedQueryManager.
if ( includes( posts, undefined ) ) {
return null;
}
return posts.map( normalizePostForDisplay );
},
state => [ state.posts.queries, state.posts.allSitesQueries ],
( state, siteId, query ) => getSerializedPostsQuery( query, siteId )
);
/**
* Returns true if currently requesting posts for the posts query, or false
* otherwise.
*
* @param {Object} state Global state tree
* @param {?Number} siteId Site ID, or `null` for all-sites queries
* @param {Object} query Post query object
* @return {Boolean} Whether posts are being requested
*/
export function isRequestingPostsForQuery( state, siteId, query ) {
const serializedQuery = getSerializedPostsQuery( query, siteId );
return !! state.posts.queryRequests[ serializedQuery ];
}
/**
* Returns the total number of items reported to be found for the given query,
* or null if the total number of queryable posts is unknown.
*
* @param {Object} state Global state tree
* @param {?Number} siteId Site ID, or `null` for all-sites queries
* @param {Object} query Post query object
* @return {?Number} Total number of found items
*/
export function getPostsFoundForQuery( state, siteId, query ) {
const manager = getQueryManager( state, siteId );
if ( ! manager ) {
return null;
}
return manager.getFound( query );
}
/**
* Returns the last queryable page of posts for the given query, or null if the
* total number of queryable posts if unknown.
*
* @param {Object} state Global state tree
* @param {?Number} siteId Site ID, or `null` for all-sites queries
* @param {Object} query Post query object
* @return {?Number} Last posts page
*/
export function getPostsLastPageForQuery( state, siteId, query ) {
const manager = getQueryManager( state, siteId );
if ( ! manager ) {
return null;
}
const pages = manager.getNumberOfPages( query );
if ( null === pages ) {
return null;
}
return Math.max( pages, 1 );
}
/**
* Returns true if the query has reached the last page of queryable pages, or
* null if the total number of queryable posts if unknown.
*
* @param {Object} state Global state tree
* @param {?Number} siteId Site ID, or `null` for all-sites queries
* @param {Object} query Post query object
* @return {?Boolean} Whether last posts page has been reached
*/
export function isPostsLastPageForQuery( state, siteId, query = {} ) {
const lastPage = getPostsLastPageForQuery( state, siteId, query );
if ( null === lastPage ) {
return lastPage;
}
return lastPage === ( query.page || DEFAULT_POST_QUERY.page );
}
/**
* Returns an array of normalized posts for the posts query, including all
* known queried pages, or null if the posts for the query are not known.
*
* @param {Object} state Global state tree
* @param {?Number} siteId Site ID, or `null` for all-sites queries
* @param {Object} query Post query object
* @return {?Array} Posts for the post query
*/
export const getPostsForQueryIgnoringPage = createSelector(
( state, siteId, query ) => {
const manager = getQueryManager( state, siteId );
if ( ! manager ) {
return null;
}
const itemsIgnoringPage = manager.getItemsIgnoringPage( query );
if ( ! itemsIgnoringPage ) {
return null;
}
return itemsIgnoringPage.map( normalizePostForDisplay );
},
state => [ state.posts.queries, state.posts.allSitesQueries ],
( state, siteId, query ) => getSerializedPostsQueryWithoutPage( query, siteId )
);
/**
* Returns true if currently requesting posts for the posts query, regardless
* of page, or false otherwise.
*
* @param {Object} state Global state tree
* @param {?Number} siteId Site ID, or `null` for all-sites queries
* @param {Object} query Post query object
* @return {Boolean} Whether posts are being requested
*/
export const isRequestingPostsForQueryIgnoringPage = createSelector(
( state, siteId, query ) => {
const normalizedQueryWithoutPage = omit( getNormalizedPostsQuery( query ), 'page' );
return some( state.posts.queryRequests, ( isRequesting, serializedQuery ) => {
if ( ! isRequesting ) {
return false;
}
const queryDetails = getDeserializedPostsQueryDetails( serializedQuery );
// Specific site query
if ( queryDetails.siteId && queryDetails.siteId !== siteId ) {
return false;
}
// All-sites query
if ( ! queryDetails.siteId && siteId ) {
return false;
}
return isEqual( normalizedQueryWithoutPage, omit( queryDetails.query, 'page' ) );
} );
},
state => state.posts.queryRequests,
( state, siteId, query ) => getSerializedPostsQuery( query, siteId )
);
/**
* Returns true if a request is in progress for the specified site post, or
* false otherwise.
*
* @param {Object} state Global state tree
* @param {Number} siteId Site ID
* @param {Number} postId Post ID
* @return {Boolean} Whether request is in progress
*/
export function isRequestingSitePost( state, siteId, postId ) {
if ( ! siteId ) {
return null;
}
if ( ! state.posts.siteRequests[ siteId ] ) {
return false;
}
return !! state.posts.siteRequests[ siteId ][ postId ];
}
/**
* Returns a post object by site ID post ID pairing, with editor revisions.
*
* @param {Object} state Global state tree
* @param {Number} siteId Site ID
* @param {Number} postId Post ID
* @return {Object} Post object with revisions
*/
export const getEditedPost = createSelector(
( state, siteId, postId ) => {
const post = getSitePost( state, siteId, postId );
const edits = getPostEdits( state, siteId, postId );
if ( ! edits ) {
return post;
}
return applyPostEdits( post, edits );
},
state => [ state.posts.queries, state.posts.edits ]
);
/**
* Returns an object of edited post attributes for the site ID post ID pairing.
*
* @param {Object} state Global state tree
* @param {Number} siteId Site ID
* @param {Number} postId Post ID
* @return {Object} Post revisions
*/
export function getPostEdits( state, siteId, postId ) {
const { edits } = state.posts;
return normalizePostForEditing( get( edits, [ siteId, postId || '' ], null ) );
}
/**
* Returns the assigned value for the edited post by field key.
*
* @param {Object} state Global state tree
* @param {Number} siteId Site ID
* @param {Number} postId Post ID
* @param {String} field Field value to retrieve
* @return {*} Field value
*/
export function getEditedPostValue( state, siteId, postId, field ) {
return get( getEditedPost( state, siteId, postId ), field );
}
/**
* Returns true if the edited post is password protected.
*
* @param {Object} state Global state tree
* @param {Number} siteId Site ID
* @param {Number} postId Post ID
* @return {Boolean} Result of the check
*/
export function isEditedPostPasswordProtected( state, siteId, postId ) {
const password = getEditedPostValue( state, siteId, postId, 'password' );
return !! ( password && password.length > 0 );
}
/**
* Returns true if the edited post is password protected and has a valid password set
*
* @param {Object} state Global state tree
* @param {Number} siteId Site ID
* @param {Number} postId Post ID
* @return {Boolean} Result of the check
*/
export function isEditedPostPasswordProtectedWithValidPassword( state, siteId, postId ) {
const password = getEditedPostValue( state, siteId, postId, 'password' );
return !! ( password && password.trim().length > 0 );
}
/**
* Returns true if there are "dirty" edited fields to be saved for the post
* corresponding with the site ID post ID pair, or false otherwise.
*
* @param {Object} state Global state tree
* @param {Number} siteId Site ID
* @param {Number} postId Post ID
* @return {Boolean} Whether dirty fields exist
*/
export const isEditedPostDirty = createSelector(
( state, siteId, postId ) => {
const post = getSitePost( state, siteId, postId );
const edits = getPostEdits( state, siteId, postId );
const editsDirty = some( edits, ( value, key ) => {
if ( key === 'type' ) {
return false;
}
if ( post ) {
switch ( key ) {
case 'author': {
return ! isAuthorEqual( value, post.author );
}
case 'date': {
return ! isDateEqual( value, post.date );
}
case 'discussion': {
return ! isDiscussionEqual( value, post.discussion );
}
case 'featured_image': {
return value !== getFeaturedImageId( post );
}
case 'metadata': {
return ! areAllMetadataEditsApplied( value, post.metadata );
}
case 'parent': {
return get( post, 'parent.ID', 0 ) !== value;
}
}
return post[ key ] !== value;
}
return (
! DEFAULT_NEW_POST_VALUES.hasOwnProperty( key ) || value !== DEFAULT_NEW_POST_VALUES[ key ]
);
} );
const { initial, current } = state.ui.editor.rawContent;
const rawContentDirty = initial !== current;
return editsDirty || rawContentDirty;
},
state => [ state.posts.queries, state.posts.edits, state.ui.editor.rawContent ]
);
/**
* Returns true if the post status is publish, private, or future
* and the date is in the past
*
* @param {Object} state Global state tree
* @param {Number} siteId Site ID
* @param {Number} postId Post ID
* @return {Boolean} Whether post is published
*/
export const isPostPublished = createSelector( ( state, siteId, postId ) => {
const post = getSitePost( state, siteId, postId );
if ( ! post ) {
return null;
}
return (
includes( [ 'publish', 'private' ], post.status ) ||
( post.status === 'future' && moment( post.date ).isBefore( moment() ) )
);
}, state => state.posts.queries );
/**
* Returns the slug, or suggested_slug, for the edited post
*
* @param {Object} state Global state tree
* @param {Number} siteId Site ID
* @param {Number} postId Post ID
* @return {String} Slug value
*/
export function getEditedPostSlug( state, siteId, postId ) {
// if local edits exists, return them regardless of post status
const postEdits = getPostEdits( state, siteId, postId );
if ( has( postEdits, 'slug' ) ) {
return postEdits.slug;
}
const post = getSitePost( state, siteId, postId );
const postSlug = get( post, 'slug' );
// when post is published, return the slug
if ( isPostPublished( state, siteId, postId ) ) {
return decodeURIIfValid( postSlug );
}
// only return suggested_slug if slug has not been edited
const suggestedSlug = get( post, [ 'other_URLs', 'suggested_slug' ] );
if ( suggestedSlug && ! postSlug ) {
return suggestedSlug;
}
return postSlug;
}
/**
* Returns the most reliable preview URL for the post by site ID, post ID pair,
* or null if a preview URL cannot be determined.
*
* @param {Object} state Global state tree
* @param {Number} siteId Site ID
* @param {Number} postId Post ID
* @param {Object} [options] Special options. See wp-calypso#14456
* @return {?String} Post preview URL
*/
export function getPostPreviewUrl( state, siteId, postId, options = false ) {
const rawPost = options.__forceUseRawPost;
const shouldUseRawPost = !! rawPost;
const post = shouldUseRawPost ? rawPost : getSitePost( state, siteId, postId );
if ( ! post ) {
return null;
}
const { URL: url, status } = post;
if ( ! url || status === 'trash' ) {
return null;
}
if ( post.preview_URL ) {
return post.preview_URL;
}
let previewUrl = url;
if ( 'publish' !== status ) {
previewUrl = addQueryArgs(
{
preview: true,
},
previewUrl
);
}
// Support mapped domains https
const site = getSite( state, siteId );
if ( site && site.options ) {
const { is_mapped_domain, unmapped_url } = site.options;
previewUrl = is_mapped_domain ? previewUrl.replace( site.URL, unmapped_url ) : previewUrl;
}
return previewUrl;
}
export function getSitePostsByTerm( state, siteId, taxonomy, termId ) {
return filter( getSitePosts( state, siteId ), post => {
return (
post.terms &&
post.terms[ taxonomy ] &&
find( post.terms[ taxonomy ], postTerm => postTerm.ID === termId )
);
} );
}