Created
October 3, 2018 06:35
-
-
Save phpbits/8723cb3b8c0bf52df89c68c9dfb34b2e to your computer and use it in GitHub Desktop.
Use withSelect instead of withAPIData
This file contains 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
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