From 480d15a7fb127ba794e8c1fe5be3b56fb1c6551b Mon Sep 17 00:00:00 2001 From: Philipp Stracker Date: Mon, 17 Feb 2025 14:17:37 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Implement=20a=20full-store-refresh?= =?UTF-8?q?=20hook?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../resources/js/hooks/useSaveSettings.js | 68 ++++++++++--------- 1 file changed, 35 insertions(+), 33 deletions(-) diff --git a/modules/ppcp-settings/resources/js/hooks/useSaveSettings.js b/modules/ppcp-settings/resources/js/hooks/useSaveSettings.js index 7237aa4df..5c51866e6 100644 --- a/modules/ppcp-settings/resources/js/hooks/useSaveSettings.js +++ b/modules/ppcp-settings/resources/js/hooks/useSaveSettings.js @@ -12,48 +12,41 @@ import { export const useSaveSettings = () => { const { withActivity } = CommonHooks.useBusyState(); - const { persist: persistPayment } = PaymentHooks.useStore(); - const { persist: persistSettings } = SettingsHooks.useStore(); - const { persist: persistStyling } = StylingHooks.useStore(); - const { persist: persistTodos } = TodosHooks.useStore(); - const { persist: persistPayLaterMessaging } = - PayLaterMessagingHooks.useStore(); + const paymentStore = PaymentHooks.useStore(); + const settingsStore = SettingsHooks.useStore(); + const stylingStore = StylingHooks.useStore(); + const todosStore = TodosHooks.useStore(); + const payLaterStore = PayLaterMessagingHooks.useStore(); - const persistActions = useMemo( + const storeActions = useMemo( () => [ { - key: 'persist-methods', - message: 'Save payment methods', - action: persistPayment, + key: 'methods', + message: 'Process payment methods', + store: paymentStore, }, { - key: 'persist-settings', - message: 'Save the settings', - action: persistSettings, + key: 'settings', + message: 'Process the settings', + store: settingsStore, }, { - key: 'persist-styling', - message: 'Save styling details', - action: persistStyling, + key: 'styling', + message: 'Process styling details', + store: stylingStore, }, { - key: 'persist-todos', - message: 'Save todos state', - action: persistTodos, + key: 'todos', + message: 'Process todos state', + store: todosStore, }, { - key: 'persist-pay-later-messaging', - message: 'Save pay later messaging details', - action: persistPayLaterMessaging, + key: 'pay-later-messaging', + message: 'Process pay later messaging details', + store: payLaterStore, }, ], - [ - persistPayLaterMessaging, - persistPayment, - persistSettings, - persistStyling, - persistTodos, - ] + [ payLaterStore, paymentStore, settingsStore, stylingStore, todosStore ] ); const persistAll = useCallback( () => { @@ -65,10 +58,19 @@ export const useSaveSettings = () => { */ document.getElementById( 'configurator-publishButton' )?.click(); - persistActions.forEach( ( { key, message, action } ) => { - withActivity( key, message, action ); + storeActions.forEach( ( { key, message, store } ) => { + withActivity( `persist-${ key }`, message, store.persist ); } ); - }, [ persistActions, withActivity ] ); + }, [ storeActions, withActivity ] ); - return { persistAll }; + const refreshAll = useCallback( () => { + storeActions.forEach( ( { key, message, store } ) => { + withActivity( `refresh-${ key }`, message, store.refresh ); + } ); + }, [ storeActions, withActivity ] ); + + return { + persistAll, + refreshAll, + }; };