@@ -17,6 +17,8 @@ import {
17
17
ToggleControl ,
18
18
Toolbar ,
19
19
} from '@wordpress/components' ;
20
+ import apiFetch from '@wordpress/api-fetch' ;
21
+ import { addQueryArgs } from '@wordpress/url' ;
20
22
import { __ } from '@wordpress/i18n' ;
21
23
import { dateI18n , format , __experimentalGetSettings } from '@wordpress/date' ;
22
24
import { decodeEntities } from '@wordpress/html-entities' ;
@@ -27,15 +29,46 @@ import {
27
29
} from '@wordpress/editor' ;
28
30
import { withSelect } from '@wordpress/data' ;
29
31
32
+ /**
33
+ * Module Constants
34
+ */
35
+ const CATEGORIES_LIST_QUERY = {
36
+ per_page : 100 ,
37
+ } ;
30
38
const MAX_POSTS_COLUMNS = 6 ;
31
39
32
40
class LatestPostsEdit extends Component {
33
41
constructor ( ) {
34
42
super ( ...arguments ) ;
35
-
43
+ this . state = {
44
+ categoriesList : [ ] ,
45
+ } ;
36
46
this . toggleDisplayPostDate = this . toggleDisplayPostDate . bind ( this ) ;
37
47
}
38
48
49
+ componentWillMount ( ) {
50
+ this . isStillMounted = true ;
51
+ this . fetchRequest = apiFetch ( {
52
+ path : addQueryArgs ( `/wp/v2/categories` , CATEGORIES_LIST_QUERY ) ,
53
+ } ) . then (
54
+ ( categoriesList ) => {
55
+ if ( this . isStillMounted ) {
56
+ this . setState ( { categoriesList } ) ;
57
+ }
58
+ }
59
+ ) . catch (
60
+ ( ) => {
61
+ if ( this . isStillMounted ) {
62
+ this . setState ( { categoriesList : [ ] } ) ;
63
+ }
64
+ }
65
+ ) ;
66
+ }
67
+
68
+ componentWillUnmount ( ) {
69
+ this . isStillMounted = false ;
70
+ }
71
+
39
72
toggleDisplayPostDate ( ) {
40
73
const { displayPostDate } = this . props . attributes ;
41
74
const { setAttributes } = this . props ;
@@ -44,7 +77,8 @@ class LatestPostsEdit extends Component {
44
77
}
45
78
46
79
render ( ) {
47
- const { attributes, categoriesList, setAttributes, latestPosts } = this . props ;
80
+ const { attributes, setAttributes, latestPosts } = this . props ;
81
+ const { categoriesList } = this . state ;
48
82
const { displayPostDate, align, postLayout, columns, order, orderBy, categories, postsToShow } = attributes ;
49
83
50
84
const inspectorControls = (
@@ -163,11 +197,7 @@ export default withSelect( ( select, props ) => {
163
197
orderby : orderBy ,
164
198
per_page : postsToShow ,
165
199
} , ( value ) => ! isUndefined ( value ) ) ;
166
- const categoriesListQuery = {
167
- per_page : 100 ,
168
- } ;
169
200
return {
170
201
latestPosts : getEntityRecords ( 'postType' , 'post' , latestPostsQuery ) ,
171
- categoriesList : getEntityRecords ( 'taxonomy' , 'category' , categoriesListQuery ) ,
172
202
} ;
173
203
} ) ( LatestPostsEdit ) ;
0 commit comments