5
5
*/
6
6
import QueryControls from '../../components/query-controls' ;
7
7
import { STORE_NAMESPACE } from './store' ;
8
- import { isBlogPrivate , isSpecificPostModeActive , queryCriteriaFromAttributes } from './utils' ;
8
+ import {
9
+ getEditorBlocksIds ,
10
+ isBlogPrivate ,
11
+ shouldReflow ,
12
+ queryCriteriaFromAttributes ,
13
+ } from './utils' ;
9
14
import { formatAvatars , formatByline } from '../../shared/js/utils' ;
10
15
11
16
/**
@@ -91,7 +96,7 @@ const coverIcon = (
91
96
92
97
class Edit extends Component {
93
98
renderPost = post => {
94
- const { attributes } = this . props ;
99
+ const { attributes, isUIDisabled } = this . props ;
95
100
const {
96
101
showImage,
97
102
imageShape,
@@ -121,7 +126,10 @@ class Edit extends Component {
121
126
} ;
122
127
123
128
const postClasses = classNames (
124
- { 'post-has-image' : post . newspack_featured_image_src } ,
129
+ {
130
+ 'post-has-image' : post . newspack_featured_image_src ,
131
+ 'homepage-posts-block__post--disabled' : isUIDisabled ,
132
+ } ,
125
133
post . newspack_article_classes
126
134
) ;
127
135
@@ -292,7 +300,6 @@ class Edit extends Component {
292
300
setAttributes ( { tagExclusions : _tagExclusions } )
293
301
}
294
302
/>
295
-
296
303
{ postLayout === 'grid' && (
297
304
< RangeControl
298
305
label = { __ ( 'Columns' , 'newspack-blocks' ) }
@@ -467,6 +474,18 @@ class Edit extends Component {
467
474
) ;
468
475
} ;
469
476
477
+ componentDidMount ( ) {
478
+ this . props . triggerReflow ( ) ;
479
+ }
480
+ componentDidUpdate ( props ) {
481
+ if ( shouldReflow ( props , this . props ) ) {
482
+ this . props . triggerReflow ( ) ;
483
+ }
484
+ }
485
+ componentWillUnmount ( ) {
486
+ this . props . triggerReflow ( ) ;
487
+ }
488
+
470
489
render ( ) {
471
490
/**
472
491
* Constants
@@ -475,12 +494,11 @@ class Edit extends Component {
475
494
const {
476
495
attributes,
477
496
className,
478
- clientId,
479
497
setAttributes,
480
498
isSelected,
481
499
latestPosts,
482
500
textColor,
483
- markPostsAsDisplayed ,
501
+ error ,
484
502
} = this . props ;
485
503
486
504
const {
@@ -584,7 +602,6 @@ class Edit extends Component {
584
602
} ,
585
603
] ;
586
604
587
- markPostsAsDisplayed ( clientId , latestPosts ) ;
588
605
return (
589
606
< Fragment >
590
607
< div
@@ -606,9 +623,15 @@ class Edit extends Component {
606
623
{ latestPosts && ! latestPosts . length && (
607
624
< Placeholder > { __ ( 'Sorry, no posts were found.' , 'newspack-blocks' ) } </ Placeholder >
608
625
) }
609
- { ! latestPosts && (
626
+ { ! latestPosts && ! error && (
610
627
< Placeholder icon = { < Spinner /> } className = "component-placeholder__align-center" />
611
628
) }
629
+ { ! latestPosts && error && (
630
+ < Placeholder className = "component-placeholder__align-center homepage-posts-block--error" >
631
+ { error }
632
+ </ Placeholder >
633
+ ) }
634
+
612
635
{ latestPosts && latestPosts . map ( post => this . renderPost ( post ) ) }
613
636
</ div >
614
637
</ div >
@@ -645,27 +668,39 @@ class Edit extends Component {
645
668
646
669
export default compose ( [
647
670
withColors ( { textColor : 'color' } ) ,
648
- withSelect ( ( select , props ) => {
649
- const { attributes, clientId } = props ;
671
+ withSelect ( ( select , { clientId, attributes } ) => {
672
+ const { getEditorBlocks, getBlocks } = select ( 'core/editor' ) ;
673
+ const editorBlocksIds = getEditorBlocksIds ( getEditorBlocks ( ) ) ;
674
+ // The block might be rendered in the block styles preview, not in the editor.
675
+ const isEditorBlock = editorBlocksIds . indexOf ( clientId ) >= 0 ;
676
+
677
+ const { getPosts, getError, isUIDisabled } = select ( STORE_NAMESPACE ) ;
678
+
679
+ const props = {
680
+ isEditorBlock,
681
+ isUIDisabled : isUIDisabled ( ) ,
682
+ error : getError ( { clientId } ) ,
683
+ topBlocksClientIdsInOrder : getBlocks ( ) . map ( block => block . clientId ) ,
684
+ } ;
650
685
651
- const latestPostsQuery = queryCriteriaFromAttributes ( attributes ) ;
652
- if ( ! isSpecificPostModeActive ( attributes ) ) {
653
- const postIdsToExclude = select ( STORE_NAMESPACE ) . previousPostIds ( clientId ) ;
654
- latestPostsQuery . exclude = postIdsToExclude . join ( ',' ) ;
686
+ if ( isEditorBlock ) {
687
+ props . latestPosts = getPosts ( { clientId } ) ;
688
+ } else {
689
+ // For block preview, display without deduplication. If there would be a way to match the outside-editor's
690
+ // block clientId to the clientId of the block that's being previewed, the correct posts could be shown here.
691
+ props . latestPosts = select ( 'core' ) . getEntityRecords (
692
+ 'postType' ,
693
+ 'post' ,
694
+ queryCriteriaFromAttributes ( attributes )
695
+ ) ;
655
696
}
656
697
657
- return {
658
- latestPosts : select ( 'core' ) . getEntityRecords ( 'postType' , 'post' , latestPostsQuery ) ,
659
- } ;
698
+ return props ;
660
699
} ) ,
661
- withDispatch ( ( dispatch , props ) => {
662
- const { attributes } = props ;
663
- const markPostsAsDisplayed = isSpecificPostModeActive ( attributes )
664
- ? dispatch ( STORE_NAMESPACE ) . markSpecificPostsAsDisplayed
665
- : dispatch ( STORE_NAMESPACE ) . markPostsAsDisplayed ;
666
-
700
+ withDispatch ( ( dispatch , { isEditorBlock } ) => {
667
701
return {
668
- markPostsAsDisplayed,
702
+ // Only editor blocks can trigger reflows.
703
+ triggerReflow : isEditorBlock ? dispatch ( STORE_NAMESPACE ) . reflow : ( ) => { } ,
669
704
} ;
670
705
} ) ,
671
706
] ) ( Edit ) ;
0 commit comments