Skip to content

Instantly share code, notes, and snippets.

@phpbits
Created October 3, 2018 06:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phpbits/8723cb3b8c0bf52df89c68c9dfb34b2e to your computer and use it in GitHub Desktop.
Save phpbits/8723cb3b8c0bf52df89c68c9dfb34b2e to your computer and use it in GitHub Desktop.
Use withSelect instead of withAPIData
const { apiFetch } = wp;
const { registerStore, withSelect } = wp.data;
const actions = {
setUserRoles( userRoles ) {
return {
type: 'SET_USER_ROLES',
userRoles,
};
},
receiveUserRoles( path ) {
return {
type: 'RECEIVE_USER_ROLES',
path,
};
},
};
const store = registerStore( 'phpbits/test-block', {
reducer( state = { userRoles: {} }, action ) {
switch ( action.type ) {
case 'SET_USER_ROLES':
return {
...state,
userRoles: action.userRoles,
};
case 'RECEIVE_USER_ROLES':
return action.userRoles;
}
return state;
},
actions,
selectors: {
receiveUserRoles( state ) {
const { userRoles } = state;
return userRoles;
},
},
resolvers: {
* receiveUserRoles( state ) {
const userRoles = apiFetch( { path: '/phpbits/test-blocks/v1/user-roles/' } )
.then( userRoles => {
return actions.setUserRoles( userRoles );
} )
yield userRoles;
},
},
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment