mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 12:25:15 +08:00
♻️ Move debug logic to separate file
This commit is contained in:
parent
4ae61dfb58
commit
9e4d0c0819
3 changed files with 54 additions and 50 deletions
51
modules/ppcp-settings/resources/js/data/debug.js
Normal file
51
modules/ppcp-settings/resources/js/data/debug.js
Normal file
|
@ -0,0 +1,51 @@
|
|||
import { OnboardingStoreName } from './index';
|
||||
import * as selectors from './onboarding/selectors';
|
||||
|
||||
export const addDebugTools = ( context ) => {
|
||||
if ( ! context || ! context?.debug ) {
|
||||
return;
|
||||
}
|
||||
|
||||
context.dumpStore = async () => {
|
||||
/* eslint-disable no-console */
|
||||
if ( ! console?.groupCollapsed ) {
|
||||
console.error( 'console.groupCollapsed is not supported.' );
|
||||
return;
|
||||
}
|
||||
|
||||
[ OnboardingStoreName ].forEach( ( storeName ) => {
|
||||
const storeSelector = `wp.data.select('${ storeName }')`;
|
||||
console.group( `[STORE] ${ storeSelector }` );
|
||||
|
||||
const dumpStore = async ( selector ) => {
|
||||
const contents = await wp.data
|
||||
.resolveSelect( storeName )
|
||||
[ selector ]();
|
||||
|
||||
console.groupCollapsed( `[SELECTOR] .${ selector }()` );
|
||||
console.table( contents );
|
||||
console.groupEnd();
|
||||
};
|
||||
|
||||
Object.keys( selectors ).forEach( async ( selector ) => {
|
||||
await dumpStore( selector );
|
||||
} );
|
||||
|
||||
console.groupEnd();
|
||||
} );
|
||||
/* eslint-enable no-console */
|
||||
};
|
||||
|
||||
context.resetStore = () => {
|
||||
const onboarding = wp.data.dispatch( OnboardingStoreName );
|
||||
onboarding.reset();
|
||||
onboarding.persist();
|
||||
};
|
||||
|
||||
context.startOnboarding = () => {
|
||||
const onboarding = wp.data.dispatch( OnboardingStoreName );
|
||||
onboarding.setCompleted( false );
|
||||
onboarding.setStep( 0 );
|
||||
onboarding.persist();
|
||||
};
|
||||
};
|
|
@ -1,7 +1,10 @@
|
|||
import { STORE_NAME } from './constants';
|
||||
import { initStore } from './store';
|
||||
import { addDebugTools } from './debug';
|
||||
|
||||
initStore();
|
||||
|
||||
export const WC_PAYPAL_STORE_NAME = STORE_NAME;
|
||||
export * from './onboarding/hooks';
|
||||
|
||||
addDebugTools( window.ppcpSettings );
|
||||
|
|
|
@ -30,54 +30,4 @@ export const initStore = () => {
|
|||
} );
|
||||
|
||||
register( store );
|
||||
|
||||
addDebugTools();
|
||||
};
|
||||
|
||||
const addDebugTools = () => {
|
||||
const context = window.ppcpSettings;
|
||||
|
||||
// Provide a debug tool to inspect the Redux store via the JS console.
|
||||
if ( ! context?.debug ) {
|
||||
return;
|
||||
}
|
||||
|
||||
const getSelectors = () => wp.data.select( STORE_NAME );
|
||||
const getActions = () => wp.data.dispatch( STORE_NAME );
|
||||
|
||||
context.dumpStore = () => {
|
||||
/* eslint-disable no-console */
|
||||
if ( ! console?.groupCollapsed ) {
|
||||
console.error( 'console.groupCollapsed is not supported.' );
|
||||
return;
|
||||
}
|
||||
|
||||
const storeSelector = `wp.data.select('${ STORE_NAME }')`;
|
||||
console.group( `[STORE] ${ storeSelector }` );
|
||||
|
||||
const select = getSelectors();
|
||||
Object.keys( selectors ).forEach( ( selector ) => {
|
||||
console.groupCollapsed( `[SELECTOR] .${ selector }()` );
|
||||
console.table( select[ selector ]() );
|
||||
console.groupEnd();
|
||||
} );
|
||||
|
||||
console.groupEnd();
|
||||
/* eslint-enable no-console */
|
||||
};
|
||||
|
||||
context.resetStore = () => {
|
||||
const dispatch = getActions();
|
||||
|
||||
dispatch.resetOnboarding();
|
||||
dispatch.persist();
|
||||
};
|
||||
|
||||
context.startOnboarding = () => {
|
||||
const dispatch = getActions();
|
||||
|
||||
dispatch.setCompleted( false );
|
||||
dispatch.setOnboardingStep( 0 );
|
||||
dispatch.persist();
|
||||
};
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue