forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (55 loc) · 1.3 KB
/
index.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
import React from 'react';
import { compose } from '@wordpress/compose';
import { render } from 'react-dom';
import '@wordpress/core-data';
import { withSelect, withDispatch } from '@wordpress/data';
const Books = function( { books, toggleIsFavourite } ) {
return books.map( ( b ) => (
<div key={ b.id }>
Is Favourite:
<input
type="checkbox"
checked={ b.json.isFavouriteBook }
onChange={ () => {
toggleIsFavourite( b );
} }
/>
Title: { b.title.raw }
</div>
) );
};
const BookContainer = function( { books, toggleIsFavourite } ) {
return (
<>
<h2>Books</h2>
<Books books={ books } toggleIsFavourite={ toggleIsFavourite } />
</>
);
};
const App = compose( [
withSelect( ( select ) => {
const args = [ 'postType', 'book' ];
const books = select( 'core' ).getEntityRecords( ...args ) || [];
return {
books,
};
} ),
withDispatch( ( dispatch ) => {
const { saveEntityRecord } = dispatch( 'core' );
return {
toggleIsFavourite: ( book ) => {
saveEntityRecord( 'postType', 'book', {
id: book.id,
json: {
...book.json,
isFavouriteBook: ! book.json.isFavouriteBook,
},
} );
},
};
} ),
] )( BookContainer );
const renderApp = () => {
render( <App />, document.getElementById( 'core-data' ) );
};
setTimeout( renderApp, 0 );