diff --git a/modules/ppcp-settings/resources/js/data/debug.js b/modules/ppcp-settings/resources/js/data/debug.js index 88bdf40ae..66530d7ec 100644 --- a/modules/ppcp-settings/resources/js/data/debug.js +++ b/modules/ppcp-settings/resources/js/data/debug.js @@ -18,6 +18,11 @@ export const addDebugTools = ( context, modules ) => { if ( ! context.debug ) { return } */ + const describe = ( fnName, fnInfo ) => { + // eslint-disable-next-line no-console + console.log( `\n%c${ fnName }:`, 'font-weight:bold', fnInfo, '\n\n' ); + }; + const debugApi = ( window.ppcpDebugger = window.ppcpDebugger || {} ); // Dump the current state of all our Redux stores. @@ -51,45 +56,89 @@ export const addDebugTools = ( context, modules ) => { // Reset all Redux stores to their initial state. debugApi.resetStore = () => { const stores = []; - const { isConnected } = wp.data.select( CommonStoreName ).merchant(); - if ( isConnected ) { - // Make sure the Onboarding wizard is "completed". - const onboarding = wp.data.dispatch( OnboardingStoreName ); - onboarding.setPersistent( 'completed', true ); - onboarding.persist(); + describe( + 'resetStore', + 'Reset all Redux stores to their DEFAULT state, without changing any server-side data. The default state is defined in the JS code.' + ); - // Reset all stores, except for the onboarding store. - stores.push( CommonStoreName ); - stores.push( PaymentStoreName ); - stores.push( SettingsStoreName ); - stores.push( StylingStoreName ); - stores.push( TodosStoreName ); - } else { - // Only reset the common & onboarding stores to restart the onboarding wizard. - stores.push( CommonStoreName ); + const { completed } = wp.data + .select( OnboardingStoreName ) + .persistentData(); + + // Reset all stores, except for the onboarding store. + stores.push( CommonStoreName ); + stores.push( PaymentStoreName ); + stores.push( SettingsStoreName ); + stores.push( StylingStoreName ); + stores.push( TodosStoreName ); + + // Only reset the onboarding store when the wizard is not completed. + if ( ! completed ) { stores.push( OnboardingStoreName ); } stores.forEach( ( storeName ) => { const store = wp.data.dispatch( storeName ); - // eslint-disable-next-line no-console - console.log( `Reset store: ${ storeName }...` ); - try { store.reset(); - store.persist(); + + // eslint-disable-next-line no-console + console.log( `Done: Store '${ storeName }' reset` ); } catch ( error ) { - console.error( ' ... Reset failed, skipping this store' ); + console.error( + `Failed: Could not reset store '${ storeName }'` + ); } } ); + + // eslint-disable-next-line no-console + console.log( '---- Complete ----\n\n' ); + }; + + debugApi.refreshStore = () => { + const stores = []; + + describe( + 'refreshStore', + 'Refreshes all Redux details with details provided by the server. This has a similar effect as reloading the page without saving' + ); + + stores.push( CommonStoreName ); + stores.push( PaymentStoreName ); + stores.push( SettingsStoreName ); + stores.push( StylingStoreName ); + stores.push( TodosStoreName ); + stores.push( OnboardingStoreName ); + + stores.forEach( ( storeName ) => { + const store = wp.data.dispatch( storeName ); + + try { + store.refresh(); + + // eslint-disable-next-line no-console + console.log( + `Done: Store '${ storeName }' refreshed from REST` + ); + } catch ( error ) { + console.error( + `Failed: Could not refresh store '${ storeName }' from REST` + ); + } + } ); + + // eslint-disable-next-line no-console + console.log( '---- Complete ----\n\n' ); }; // Disconnect the merchant and display the onboarding wizard. debugApi.disconnect = () => { const common = wp.data.dispatch( CommonStoreName ); + describe(); + common.disconnectMerchant(); // eslint-disable-next-line no-console @@ -102,6 +151,11 @@ export const addDebugTools = ( context, modules ) => { debugApi.onboardingMode = ( state ) => { const onboarding = wp.data.dispatch( OnboardingStoreName ); + describe( + 'onboardingMode', + 'Toggle between onboarding wizard and the settings screen.' + ); + onboarding.setPersistent( 'completed', ! state ); onboarding.persist(); };