♻️ Improve the code pattern of persistAll

This commit is contained in:
Philipp Stracker 2025-02-07 11:57:03 +01:00
parent e3dfb23d2d
commit 4b75622ac7
No known key found for this signature in database

View file

@ -1,4 +1,4 @@
import { useCallback } from '@wordpress/element';
import { useCallback, useMemo } from '@wordpress/element';
import {
CommonHooks,
@ -19,39 +19,51 @@ export const useSaveSettings = () => {
const { persist: persistPayLaterMessaging } =
PayLaterMessagingHooks.useStore();
const persistActions = useMemo(
() => [
{
key: 'persist-methods',
message: 'Save payment methods',
action: persistPayment,
},
{
key: 'persist-settings',
message: 'Save the settings',
action: persistSettings,
},
{
key: 'persist-styling',
message: 'Save styling details',
action: persistStyling,
},
{
key: 'persist-todos',
message: 'Save todos state',
action: persistTodos,
},
{
key: 'persist-pay-later-messaging',
message: 'Save pay later messaging details',
action: persistPayLaterMessaging,
},
],
[
persistPayLaterMessaging,
persistPayment,
persistSettings,
persistStyling,
persistTodos,
]
);
const persistAll = useCallback( () => {
// Executes onSave on TabPayLaterMessaging component.
document.getElementById( 'configurator-publishButton' )?.click();
withActivity(
'persist-methods',
'Save payment methods',
persistPayment
);
withActivity(
'persist-settings',
'Save the settings',
persistSettings
);
withActivity(
'persist-styling',
'Save styling details',
persistStyling
);
withActivity( 'persist-todos', 'Save todos state', persistTodos );
withActivity(
'persist-pay-later-messaging',
'Save pay later messaging details',
persistPayLaterMessaging
);
}, [
persistPayment,
persistSettings,
persistStyling,
persistTodos,
persistPayLaterMessaging,
withActivity,
] );
persistActions.forEach( ( { key, message, action } ) => {
withActivity( key, message, action );
} );
}, [ persistActions, withActivity ] );
return { persistAll };
};