Skip to content

Commit

Permalink
Improve reducer signature
Browse files Browse the repository at this point in the history
  • Loading branch information
manzoorwanijk committed Oct 25, 2024
1 parent 1b44b04 commit f5b1ebf
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 131 deletions.
15 changes: 12 additions & 3 deletions projects/js-packages/wp-data-sync/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

Sync data from client to server and vice versa using @wordpress/data stores.

This package exports a function named `createWpDataSync` which lets you avoid creating all the boilerplate code to handle API calls, which means that you don't need to create any reducers, selectors or actions to handle the data sync.
Often, when working on settings page UIs, we need to fetch the settings from and sync back to the server. This results in a lot of boiler plate code to define actions, action type constants, thinks, selectors, resolvers and reducers, which are often repeated for different sections.

This package exports a function named `createWpDataSync`, which abstracts that boiler plate away and adds a way to simply define what data you need and where to get from and then it gives you the required selectors, actions, resolvers, and a reducer which you can pass to `@wordpress/data` store.

## How to install wp-data-sync

Expand Down Expand Up @@ -37,7 +39,9 @@ const initialState: PluginSettings = {
// Create the data sync
const myPluginSettings = createWpDataSync( 'myPluginSettings', {
endpoint: '/wp/v2/settings',
initialState, // Optional
getSliceFromState( state: SocialStoreState ) {
return state.settings.myPluginSettings;
},
extractFetchResponse: response => response.my_plugin_settings, // Optional
prepareUpdateRequest: data => ( { my_plugin_settings: data } ), // Optional
} );
Expand All @@ -50,7 +54,7 @@ const myPluginSettings = createWpDataSync( 'myPluginSettings', {
// For example, if you only need the selectors, you can pass only the selectors.
export const store = createReduxStore( 'some-store-id', {
reducer: combineReducers( {
...myPluginSettings.reducers,
myPluginSettings: myPluginSettings.reducer,
// Other reducers
} ),
actions: {
Expand All @@ -65,6 +69,11 @@ export const store = createReduxStore( 'some-store-id', {
...myPluginSettings.resolvers,
// Other resolvers
},
initialState: {
settings: {
myPluginSettings: initialState,
},
},
} );

register( store );
Expand Down
Loading

0 comments on commit f5b1ebf

Please sign in to comment.