From 7fd385fdecb0a6e50c49580d552e64481348fa75 Mon Sep 17 00:00:00 2001 From: Manzoor Wani Date: Fri, 25 Oct 2024 21:43:03 +0530 Subject: [PATCH] Add more options --- .../wp-data-sync/src/create-wp-data-sync.ts | 43 +++++++++++++++++-- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/projects/js-packages/wp-data-sync/src/create-wp-data-sync.ts b/projects/js-packages/wp-data-sync/src/create-wp-data-sync.ts index b7503d0cd55f7..cfa3652835861 100644 --- a/projects/js-packages/wp-data-sync/src/create-wp-data-sync.ts +++ b/projects/js-packages/wp-data-sync/src/create-wp-data-sync.ts @@ -143,16 +143,19 @@ export type WpDataSyncOptions< Shape extends object > = { * The endpoint to sync the data with. */ endpoint: string; + /** * The initial state of the data */ initialState?: Shape; + /** * A function to extract the data from the fetch response. * * If not provided, the response will be used as is. */ extractFetchResponse?: ( response: unknown ) => Partial< Shape >; + /** * A function to prepare the request for updating the data. * @@ -167,6 +170,29 @@ export type WpDataSyncOptions< Shape extends object > = { * or if you use a reducer key different from the name. */ getSliceFromState?: ( state: object ) => WpDataSyncState< Shape >; + + /** + * Whether to fetch the data on initialization. + * + * You may disable it if you set the initial state. + * + * @default true + */ + fetchOnInit?: boolean; + + /** + * Whether to fetch the data on update. + * + * @default false + */ + fetchAfterUpdate?: boolean; + + /** + * Whether to do optimistic updates. + * + * @default true + */ + doOptimisticUpdates?: boolean; }; /** @@ -180,8 +206,11 @@ export type WpDataSyncOptions< Shape extends object > = { export function createWpDataSync< Shape extends object, Name extends string >( name: Name, { + doOptimisticUpdates = true, endpoint, extractFetchResponse, + fetchAfterUpdate = false, + fetchOnInit = true, getSliceFromState = defaultGetSliceFromState( name ), initialState, prepareUpdateRequest, @@ -193,7 +222,9 @@ export function createWpDataSync< Shape extends object, Name extends string >( resolvers: { [ `get${ capitalizedName }` as const ]: () => { return async function ( { dispatch } ) { - await dispatch[ `fetch${ capitalizedName }` ](); + if ( fetchOnInit ) { + await dispatch[ `fetch${ capitalizedName }` ](); + } }; }, }, @@ -266,8 +297,10 @@ export function createWpDataSync< Shape extends object, Name extends string >( const setStatus = dispatch[ `setStatusFor${ capitalizedName }` ]; try { - // Optimistically update the data. - dispatch[ `set${ capitalizedName }` ]( payload ); + if ( doOptimisticUpdates ) { + // Optimistically update the data. + dispatch[ `set${ capitalizedName }` ]( payload ); + } setStatus( 'updating' ); @@ -275,6 +308,10 @@ export function createWpDataSync< Shape extends object, Name extends string >( await apiFetch( { method: 'POST', path: endpoint, data } ); + if ( fetchAfterUpdate ) { + await dispatch[ `fetch${ capitalizedName }` ](); + } + setStatus( 'idle' ); } catch ( error ) { // Revert the value to its previous state.