♻️ Improve the main store-generation logic

This commit is contained in:
Philipp Stracker 2024-11-18 16:23:54 +01:00
parent b921a54e9f
commit 4dd31965ea
No known key found for this signature in database

View file

@ -1,20 +1,23 @@
import { createReduxStore, register, combineReducers } from '@wordpress/data'; import { createReduxStore, register, combineReducers } from '@wordpress/data';
import { controls } from '@wordpress/data-controls'; import { controls } from '@wordpress/data-controls';
import { STORE_NAME } from './constants'; import { STORE_NAME } from './constants';
import * as onboarding from './onboarding';
// Import Redux modules.
import * as Onboarding from './onboarding';
const actions = {}; const actions = {};
const selectors = {}; const selectors = {};
const resolvers = {}; const resolvers = {};
[ onboarding ].forEach( ( item ) => { [ Onboarding ].forEach( ( item ) => {
Object.assign( actions, { ...item.actions } ); Object.assign( actions, { ...( item.actions || {} ) } );
Object.assign( selectors, { ...item.selectors } ); Object.assign( selectors, { ...( item.selectors || {} ) } );
Object.assign( resolvers, { ...item.resolvers } ); Object.assign( resolvers, { ...( item.resolvers || {} ) } );
Object.assign( controls, { ...( item.controls || {} ) } );
} ); } );
const reducer = combineReducers( { const reducer = combineReducers( {
onboarding: onboarding.reducer, [ Onboarding.STORE_KEY ]: Onboarding.reducer,
} ); } );
export const initStore = () => { export const initStore = () => {
@ -28,31 +31,53 @@ export const initStore = () => {
register( store ); register( store );
/* eslint-disable no-console */ addDebugTools();
// Provide a debug tool to inspect the Redux store via the JS console. };
if ( window.ppcpSettings?.debug && console?.groupCollapsed ) {
window.ppcpSettings.dumpStore = () => { const addDebugTools = () => {
const storeSelector = `wp.data.select('${ STORE_NAME }')`; const context = window.ppcpSettings;
console.group( `[STORE] ${ storeSelector }` );
// Provide a debug tool to inspect the Redux store via the JS console.
const storeState = wp.data.select( STORE_NAME ); if ( ! context?.debug ) {
Object.keys( selectors ).forEach( ( selector ) => { return;
console.groupCollapsed( `[SELECTOR] .${ selector }()` ); }
console.table( storeState[ selector ]() );
console.groupEnd(); const getSelectors = () => wp.data.select( STORE_NAME );
} ); const getActions = () => wp.data.dispatch( STORE_NAME );
console.groupEnd(); context.dumpStore = () => {
}; /* eslint-disable no-console */
window.ppcpSettings.resetStore = () => { if ( ! console?.groupCollapsed ) {
wp.data.dispatch( STORE_NAME ).resetOnboarding(); console.error( 'console.groupCollapsed is not supported.' );
wp.data.dispatch( STORE_NAME ).persist(); return;
}; }
window.ppcpSettings.startOnboarding = () => {
wp.data.dispatch( STORE_NAME ).setCompleted( false ); const storeSelector = `wp.data.select('${ STORE_NAME }')`;
wp.data.dispatch( STORE_NAME ).setOnboardingStep( 0 ); console.group( `[STORE] ${ storeSelector }` );
wp.data.dispatch( STORE_NAME ).persist();
}; const select = getSelectors();
} Object.keys( selectors ).forEach( ( selector ) => {
/* eslint-enable no-console */ 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();
};
}; };