Move styling options to Redux store!

This commit is contained in:
Philipp Stracker 2025-01-16 20:01:22 +01:00
parent 34cc8f8fab
commit 033b6e5b74
No known key found for this signature in database

View file

@ -8,8 +8,8 @@
*/ */
import { __ } from '@wordpress/i18n'; import { __ } from '@wordpress/i18n';
import { useCallback, useState } from '@wordpress/element'; // Temporary import { useCallback } from '@wordpress/element'; // Temporary
import { useDispatch } from '@wordpress/data'; import { useDispatch, useSelect } from '@wordpress/data';
import { createHooksForStore } from '../utils'; import { createHooksForStore } from '../utils';
import { STORE_NAME } from './constants'; import { STORE_NAME } from './constants';
@ -33,43 +33,29 @@ const useHooks = () => {
const [ location, setLocation ] = useTransient( 'location' ); const [ location, setLocation ] = useTransient( 'location' );
// Persistent accessors. // Persistent accessors.
// TODO - this is a dummy implementation. const persistentData = useSelect(
const defaultStyle = { ( select ) => select( STORE_NAME ).persistentData(),
paymentMethods: [], []
color: 'gold', );
shape: 'rect',
label: 'paypal',
layout: 'vertical',
tagline: false,
};
// This data-struct is already present in the Redux store via persistentData, e.g. "persistentData.cart.label" const [ locationStyles, setLocationStyles ] = usePersistent( location );
const [ styles, setStyles ] = useState( {
cart: { ...defaultStyle, label: 'checkout' },
'classic-checkout': { ...defaultStyle },
'express-checkout': { ...defaultStyle, label: 'pay' },
'mini-cart': { ...defaultStyle, label: 'pay' },
'product-page': { ...defaultStyle },
} );
const getLocationProp = useCallback( const getLocationProp = useCallback(
( prop ) => styles[ location ]?.[ prop ], ( prop ) => {
[ location, styles ] return persistentData[ location ]?.[ prop ];
},
[ location, persistentData ]
); );
const setLocationProp = useCallback( const setLocationProp = useCallback(
( prop, value ) => { ( prop, value ) => {
setStyles( ( prevState ) => ( { setLocationStyles( {
...prevState, ...locationStyles,
[ location ]: { [ prop ]: value,
...prevState[ location ], } );
[ prop ]: value,
},
} ) );
}, },
[ location ] [ locationStyles, setLocationStyles ]
); );
// end of dummy implementation
return { return {
persist, persist,