2025-01-13 17:06:05 +01:00
|
|
|
/**
|
|
|
|
* Hooks: Provide the main API for components to interact with the store.
|
|
|
|
*
|
|
|
|
* These encapsulate store interactions, offering a consistent interface.
|
|
|
|
* Hooks simplify data access and manipulation for components.
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
*/
|
|
|
|
|
2025-01-16 17:16:37 +01:00
|
|
|
import { __ } from '@wordpress/i18n';
|
2025-01-16 20:01:22 +01:00
|
|
|
import { useCallback } from '@wordpress/element'; // Temporary
|
|
|
|
import { useDispatch, useSelect } from '@wordpress/data';
|
2025-01-13 17:06:05 +01:00
|
|
|
|
2025-01-16 18:20:03 +01:00
|
|
|
import { createHooksForStore } from '../utils';
|
2025-01-13 17:06:05 +01:00
|
|
|
import { STORE_NAME } from './constants';
|
2025-01-16 17:16:37 +01:00
|
|
|
import {
|
|
|
|
STYLING_COLORS,
|
|
|
|
STYLING_LABELS,
|
|
|
|
STYLING_LAYOUTS,
|
|
|
|
STYLING_LOCATIONS,
|
|
|
|
STYLING_PAYMENT_METHODS,
|
|
|
|
STYLING_SHAPES,
|
|
|
|
} from './configuration';
|
2025-01-13 17:06:05 +01:00
|
|
|
|
|
|
|
const useHooks = () => {
|
2025-01-17 13:04:21 +01:00
|
|
|
const { useTransient } = createHooksForStore( STORE_NAME );
|
|
|
|
const { persist, setPersistent } = useDispatch( STORE_NAME );
|
2025-01-13 17:06:05 +01:00
|
|
|
|
|
|
|
// Read-only flags and derived state.
|
|
|
|
|
|
|
|
// Transient accessors.
|
2025-01-16 18:20:03 +01:00
|
|
|
const [ isReady ] = useTransient( 'isReady' );
|
2025-01-16 18:28:11 +01:00
|
|
|
const [ location, setLocation ] = useTransient( 'location' );
|
2025-01-13 17:06:05 +01:00
|
|
|
|
|
|
|
// Persistent accessors.
|
2025-01-16 20:01:22 +01:00
|
|
|
const persistentData = useSelect(
|
|
|
|
( select ) => select( STORE_NAME ).persistentData(),
|
|
|
|
[]
|
|
|
|
);
|
2025-01-16 17:16:37 +01:00
|
|
|
|
2025-01-16 19:30:54 +01:00
|
|
|
const getLocationProp = useCallback(
|
2025-01-17 13:04:21 +01:00
|
|
|
( locatonId, prop ) => {
|
|
|
|
if ( undefined === persistentData[ locatonId ]?.[ prop ] ) {
|
2025-01-16 20:02:37 +01:00
|
|
|
console.error(
|
2025-01-17 13:04:21 +01:00
|
|
|
`Trying to access non-existent style property: ${ locatonId }.${ prop }. Possibly wrong style name - review the reducer.`
|
2025-01-16 20:02:37 +01:00
|
|
|
);
|
|
|
|
}
|
2025-01-17 13:04:21 +01:00
|
|
|
return persistentData[ locatonId ]?.[ prop ];
|
2025-01-16 20:01:22 +01:00
|
|
|
},
|
2025-01-17 13:04:21 +01:00
|
|
|
[ persistentData ]
|
2025-01-16 17:16:37 +01:00
|
|
|
);
|
|
|
|
|
2025-01-16 19:30:54 +01:00
|
|
|
const setLocationProp = useCallback(
|
2025-01-17 13:04:21 +01:00
|
|
|
( locationId, prop, value ) => {
|
|
|
|
const updatedStyles = {
|
|
|
|
...persistentData[ locationId ],
|
2025-01-16 20:01:22 +01:00
|
|
|
[ prop ]: value,
|
2025-01-17 13:04:21 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
setPersistent( locationId, updatedStyles );
|
2025-01-16 17:16:37 +01:00
|
|
|
},
|
2025-01-17 13:04:21 +01:00
|
|
|
[ persistentData, setPersistent ]
|
2025-01-16 17:16:37 +01:00
|
|
|
);
|
2025-01-16 19:30:54 +01:00
|
|
|
|
|
|
|
return {
|
|
|
|
persist,
|
|
|
|
isReady,
|
|
|
|
location,
|
|
|
|
setLocation,
|
|
|
|
getLocationProp,
|
|
|
|
setLocationProp,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useStore = () => {
|
|
|
|
const { persist, isReady } = useHooks();
|
|
|
|
return { persist, isReady };
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useStylingLocation = () => {
|
|
|
|
const { location, setLocation } = useHooks();
|
|
|
|
return { location, setLocation };
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useStylingProps = ( location ) => {
|
|
|
|
const { getLocationProp, setLocationProp } = useHooks();
|
2025-01-16 17:16:37 +01:00
|
|
|
|
|
|
|
return {
|
|
|
|
// Location (drop down).
|
|
|
|
locationChoices: Object.values( STYLING_LOCATIONS ),
|
|
|
|
locationDetails: STYLING_LOCATIONS[ location ],
|
|
|
|
|
|
|
|
// Payment methods (checkboxes).
|
|
|
|
paymentMethodChoices: Object.values( STYLING_PAYMENT_METHODS ),
|
2025-01-16 20:04:47 +01:00
|
|
|
paymentMethods: getLocationProp( 'methods' ),
|
|
|
|
setPaymentMethods: ( methods ) => setLocationProp( 'methods', methods ),
|
2025-01-16 17:16:37 +01:00
|
|
|
|
|
|
|
// Color (dropdown).
|
|
|
|
colorChoices: Object.values( STYLING_COLORS ),
|
2025-01-16 19:30:54 +01:00
|
|
|
color: getLocationProp( 'color' ),
|
|
|
|
setColor: ( color ) => setLocationProp( 'color', color ),
|
2025-01-16 17:16:37 +01:00
|
|
|
|
|
|
|
// Shape (radio).
|
|
|
|
shapeChoices: Object.values( STYLING_SHAPES ),
|
2025-01-16 19:30:54 +01:00
|
|
|
shape: getLocationProp( 'shape' ),
|
|
|
|
setShape: ( shape ) => setLocationProp( 'shape', shape ),
|
2025-01-16 17:16:37 +01:00
|
|
|
|
|
|
|
// Label (dropdown).
|
|
|
|
labelChoices: Object.values( STYLING_LABELS ),
|
2025-01-16 19:30:54 +01:00
|
|
|
label: getLocationProp( 'label' ),
|
|
|
|
setLabel: ( label ) => setLocationProp( 'label', label ),
|
2025-01-16 17:16:37 +01:00
|
|
|
|
|
|
|
// Layout (radio).
|
|
|
|
layoutChoices: Object.values( STYLING_LAYOUTS ),
|
|
|
|
supportsLayout: true,
|
2025-01-16 19:30:54 +01:00
|
|
|
layout: getLocationProp( 'layout' ),
|
|
|
|
setLayout: ( layout ) => setLocationProp( 'layout', layout ),
|
2025-01-16 17:16:37 +01:00
|
|
|
|
|
|
|
// Tagline (checkbox).
|
|
|
|
taglineChoices: [
|
|
|
|
{
|
|
|
|
value: 'tagline',
|
|
|
|
label: __( 'Enable Tagline', 'woocommerce-paypal-payments' ),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
supportsTagline: true,
|
2025-01-16 19:30:54 +01:00
|
|
|
tagline: getLocationProp( 'tagline' ),
|
|
|
|
setTagline: ( tagline ) => setLocationProp( 'tagline', tagline ),
|
2025-01-16 17:16:37 +01:00
|
|
|
};
|
|
|
|
};
|