Repository navigation
Try Selector Side Effects and withData HoC #5219
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
91c666c
Try Selector Side Effects and withData HoC
youknowriad 91da014
Implement the resolver API
youknowriad 542483a
Data Module: Rely on the memoized call to avoid isFulfilled API
youknowriad b924374
Core Data: Register store instead
youknowriad 8cf3918
Style tweaks per review
youknowriad 5942eae
Core Data: Refactor, add comments and avoiding returning new instance…
youknowriad 53784b2
Data Module: Optimize module registration
youknowriad 574b4af
Documentation: Remove useless module list
youknowriad e9220ec
Data: Avoid modifying selector if no resolver
aduth ec4d27c
Data: Change resolver signature to fulfill as assigned value
aduth 157c6f5
Documentation: Resolve error in data example
aduth 1371ee1
Data: Pass state as argument to resolver
aduth 5de52ac
Data: Enable promise resolution to action
aduth 4255bea
Data: Support async generator resolver functions
aduth 0796356
Docs: Add data resolver example
aduth b6e3256
Testing: Use asyncIterator Symbol shim from core-js
aduth 73c28eb
Docs: Add core-data README.md
aduth f4f13f4
Data: Refactor categories as operating on terms data
aduth 0032097
Data: Add resolver tests
aduth f1261b6
Data: Improve yielding flexibility by normalizing to async iterator
aduth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,8 @@ | |
| * WordPress dependencies | ||
| */ | ||
| import { Component } from '@wordpress/element'; | ||
| import { Placeholder, Spinner, ToggleControl, withAPIData } from '@wordpress/components'; | ||
| import { Placeholder, Spinner, ToggleControl } from '@wordpress/components'; | ||
| import { withSelect } from '@wordpress/data'; | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { times, unescape } from 'lodash'; | ||
|
|
||
|
|
@@ -46,7 +47,7 @@ class CategoriesBlock extends Component { | |
| } | ||
|
|
||
| getCategories( parentId = null ) { | ||
| const categories = this.props.categories.data; | ||
| const categories = this.props.categories; | ||
| if ( ! categories || ! categories.length ) { | ||
| return []; | ||
| } | ||
|
|
@@ -142,9 +143,8 @@ class CategoriesBlock extends Component { | |
| } | ||
|
|
||
| render() { | ||
| const { attributes, focus, setAttributes } = this.props; | ||
| const { attributes, focus, setAttributes, isRequesting } = this.props; | ||
| const { align, displayAsDropdown, showHierarchy, showPostCounts } = attributes; | ||
| const categories = this.getCategories(); | ||
|
|
||
| const inspectorControls = focus && ( | ||
| <InspectorControls key="inspector"> | ||
|
|
@@ -167,7 +167,7 @@ class CategoriesBlock extends Component { | |
| </InspectorControls> | ||
| ); | ||
|
|
||
| if ( ! categories.length ) { | ||
| if ( isRequesting ) { | ||
| return [ | ||
| inspectorControls, | ||
| <Placeholder | ||
|
|
@@ -204,8 +204,11 @@ class CategoriesBlock extends Component { | |
| } | ||
| } | ||
|
|
||
| export default withAPIData( () => { | ||
| export default withSelect( ( select ) => { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❤️ |
||
| const { getCategories, isRequestingCategories } = select( 'core' ); | ||
|
|
||
| return { | ||
| categories: '/wp/v2/categories', | ||
| categories: getCategories(), | ||
| isRequesting: isRequestingCategories(), | ||
| }; | ||
| } )( CategoriesBlock ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| Core Data | ||
| ========= | ||
|
|
||
| Core Data is a [data module](../data) intended to simplify access to and manipulation of core WordPress entities. It registers its own store and provides a number of selectors which resolve data from the WordPress REST API automatically, along with dispatching action creators to manipulate data. | ||
|
|
||
| Used in combination with features of the data module such as [`subscribe`](https://github.com/WordPress/gutenberg/tree/master/data#subscribe-function) or [higher-order components](https://github.com/WordPress/gutenberg/tree/master/data#higher-order-components), it enables a developer to easily add data into the logic and display of their plugin. | ||
|
|
||
| ## Example | ||
|
|
||
| Below is an example of a component which simply renders a list of categories: | ||
|
|
||
| ```jsx | ||
| const { withSelect } = wp.data; | ||
|
|
||
| function MyCategoriesList( { categories, isRequesting } ) { | ||
| if ( isRequesting ) { | ||
| return 'Loading…'; | ||
| } | ||
|
|
||
| return ( | ||
| <ul> | ||
| { categories.map( ( category ) => ( | ||
| <li key={ category.id }>{ category.name }</li> | ||
| ) ) } | ||
| </ul> | ||
| ); | ||
| } | ||
|
|
||
| MyCategoriesList = withSelect( ( select ) => { | ||
| const { getCategories, isRequestingCategories } = select( 'core' ); | ||
|
|
||
| return { | ||
| categories: getCategories(), | ||
| isRequesting: isRequestingCategories(), | ||
| }; | ||
| } ); | ||
| ``` | ||
|
|
||
| ## Actions | ||
|
|
||
| The following set of dispatching action creators are available on the object returned by `wp.data.dispatch( 'core' )`: | ||
|
|
||
| _Refer to `actions.js` for the full set of dispatching action creators. In the future, this documentation will be automatically generated to detail all available dispatching action creators._ | ||
|
|
||
| ## Selectors | ||
|
|
||
| The following selectors are available on the object returned by `wp.data.select( 'core' )`: | ||
|
|
||
| _Refer to `selectors.js` for the full set of selectors. In the future, this documentation will be automatically generated to detail all available selectors._ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /** | ||
| * Returns an action object used in signalling that the request for a given | ||
| * data type has been made. | ||
| * | ||
| * @param {string} dataType Data type requested. | ||
| * @param {?string} subType Optional data sub-type. | ||
| * | ||
| * @return {Object} Action object. | ||
| */ | ||
| export function setRequested( dataType, subType ) { | ||
| return { | ||
| type: 'SET_REQUESTED', | ||
| dataType, | ||
| subType, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Returns an action object used in signalling that terms have been received | ||
| * for a given taxonomy. | ||
| * | ||
| * @param {string} taxonomy Taxonomy name. | ||
| * @param {Object[]} terms Terms received. | ||
| * | ||
| * @return {Object} Action object. | ||
| */ | ||
| export function receiveTerms( taxonomy, terms ) { | ||
| return { | ||
| type: 'RECEIVE_TERMS', | ||
| taxonomy, | ||
| terms, | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| /** | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| * WordPress Dependencies | ||
| */ | ||
| import { registerStore } from '@wordpress/data'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import reducer from './reducer'; | ||
| import * as selectors from './selectors'; | ||
| import * as actions from './actions'; | ||
| import * as resolvers from './resolvers'; | ||
|
|
||
| const store = registerStore( 'core', { | ||
| reducer, | ||
| actions, | ||
| selectors, | ||
| resolvers, | ||
| } ); | ||
|
|
||
| export default store; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import { combineReducers } from 'redux'; | ||
|
|
||
| /** | ||
| * Reducer managing terms state. Keyed by taxonomy slug, the value is either | ||
| * undefined (if no request has been made for given taxonomy), null (if a | ||
| * request is in-flight for given taxonomy), or the array of terms for the | ||
| * taxonomy. | ||
| * | ||
| * @param {Object} state Current state. | ||
| * @param {Object} action Dispatched action. | ||
| * | ||
| * @return {string} Updated state. | ||
| */ | ||
| export function terms( state = {}, action ) { | ||
| switch ( action.type ) { | ||
| case 'RECEIVE_TERMS': | ||
| return { | ||
| ...state, | ||
| [ action.taxonomy ]: action.terms, | ||
| }; | ||
|
|
||
| case 'SET_REQUESTED': | ||
| const { dataType, subType: taxonomy } = action; | ||
| if ( dataType !== 'terms' || state.hasOwnProperty( taxonomy ) ) { | ||
| return state; | ||
| } | ||
|
|
||
| return { | ||
| ...state, | ||
| [ taxonomy ]: null, | ||
| }; | ||
| } | ||
|
|
||
| return state; | ||
| } | ||
|
|
||
| export default combineReducers( { | ||
| terms, | ||
| } ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import apiRequest from '@wordpress/api-request'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { setRequested, receiveTerms } from './actions'; | ||
|
|
||
| /** | ||
| * Requests categories from the REST API, yielding action objects on request | ||
| * progress. | ||
| */ | ||
| export async function* getCategories() { | ||
| yield setRequested( 'terms', 'categories' ); | ||
| const categories = await apiRequest( { path: '/wp/v2/categories' } ); | ||
| yield receiveTerms( 'categories', categories ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /** | ||
| * Returns all the available terms for the given taxonomy. | ||
| * | ||
| * @param {Object} state Data state. | ||
| * @param {string} taxonomy Taxonomy name. | ||
| * | ||
| * @return {Array} Categories list. | ||
| */ | ||
| export function getTerms( state, taxonomy ) { | ||
| return state.terms[ taxonomy ]; | ||
| } | ||
|
|
||
| /** | ||
| * Returns all the available categories. | ||
| * | ||
| * @param {Object} state Data state. | ||
| * | ||
| * @return {Array} Categories list. | ||
| */ | ||
| export function getCategories( state ) { | ||
| return getTerms( state, 'categories' ); | ||
| } | ||
|
|
||
| /** | ||
| * Returns true if a request is in progress for terms data of a given taxonomy, | ||
| * or false otherwise. | ||
| * | ||
| * @param {Object} state Data state. | ||
| * @param {string} taxonomy Taxonomy name. | ||
| * | ||
| * @return {boolean} Whether a request is in progress for taxonomy's terms. | ||
| */ | ||
| export function isRequestingTerms( state, taxonomy ) { | ||
| return state.terms[ taxonomy ] === null; | ||
| } | ||
|
|
||
| /** | ||
| * Returns true if a request is in progress for categories data, or false | ||
| * otherwise. | ||
| * | ||
| * @param {Object} state Data state. | ||
| * | ||
| * @return {boolean} Whether a request is in progress for categories. | ||
| */ | ||
| export function isRequestingCategories( state ) { | ||
| return isRequestingTerms( state, 'categories' ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| module.exports = jest.fn(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import deepFreeze from 'deep-freeze'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { terms } from '../reducer'; | ||
|
|
||
| describe( 'terms()', () => { | ||
| it( 'returns an empty object by default', () => { | ||
| const state = terms( undefined, {} ); | ||
|
|
||
| expect( state ).toEqual( {} ); | ||
| } ); | ||
|
|
||
| it( 'returns with received terms', () => { | ||
| const originalState = deepFreeze( {} ); | ||
| const state = terms( originalState, { | ||
| type: 'RECEIVE_TERMS', | ||
| taxonomy: 'categories', | ||
| terms: [ { id: 1 } ], | ||
| } ); | ||
|
|
||
| expect( state ).toEqual( { | ||
| categories: [ { id: 1 } ], | ||
| } ); | ||
| } ); | ||
|
|
||
| it( 'assigns requested taxonomy to null', () => { | ||
| const originalState = deepFreeze( {} ); | ||
| const state = terms( originalState, { | ||
| type: 'SET_REQUESTED', | ||
| dataType: 'terms', | ||
| subType: 'categories', | ||
| } ); | ||
|
|
||
| expect( state ).toEqual( { | ||
| categories: null, | ||
| } ); | ||
| } ); | ||
|
|
||
| it( 'does not assign requested taxonomy to null if received', () => { | ||
| const originalState = deepFreeze( { | ||
| categories: [ { id: 1 } ], | ||
| } ); | ||
| const state = terms( originalState, { | ||
| type: 'SET_REQUESTED', | ||
| dataType: 'terms', | ||
| subType: 'categories', | ||
| } ); | ||
|
|
||
| expect( state ).toEqual( { | ||
| categories: [ { id: 1 } ], | ||
| } ); | ||
| } ); | ||
|
|
||
| it( 'does not assign requested taxonomy if not terms data type', () => { | ||
| const originalState = deepFreeze( {} ); | ||
| const state = terms( originalState, { | ||
| type: 'SET_REQUESTED', | ||
| dataType: 'foo', | ||
| subType: 'categories', | ||
| } ); | ||
|
|
||
| expect( state ).toEqual( {} ); | ||
| } ); | ||
| } ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import apiRequest from '@wordpress/api-request'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { getCategories } from '../resolvers'; | ||
| import { setRequested, receiveTerms } from '../actions'; | ||
|
|
||
| jest.mock( '@wordpress/api-request' ); | ||
|
|
||
| describe( 'getCategories', () => { | ||
| const CATEGORIES = [ { id: 1 } ]; | ||
|
|
||
| beforeAll( () => { | ||
| apiRequest.mockImplementation( ( options ) => { | ||
| if ( options.path === '/wp/v2/categories' ) { | ||
| return Promise.resolve( CATEGORIES ); | ||
| } | ||
| } ); | ||
| } ); | ||
|
|
||
| it( 'yields with requested terms', async () => { | ||
| const fulfillment = getCategories(); | ||
| const requested = ( await fulfillment.next() ).value; | ||
| expect( requested.type ).toBe( setRequested().type ); | ||
| const received = ( await fulfillment.next() ).value; | ||
| expect( received ).toEqual( receiveTerms( 'categories', CATEGORIES ) ); | ||
| } ); | ||
| } ); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I love how this file has been refactored 👍