♻️ Apply new code style to example store

This commit is contained in:
Philipp Stracker 2025-01-17 19:25:10 +01:00
parent cd63b18608
commit 10129d49b5
No known key found for this signature in database
2 changed files with 31 additions and 37 deletions

View file

@ -36,28 +36,37 @@ export const hydrate = ( payload ) => ( {
payload, payload,
} ); } );
/**
* Generic transient-data updater.
*
* @param {string} prop Name of the property to update.
* @param {any} value The new value of the property.
* @return {Action} The action.
*/
export const setTransient = ( prop, value ) => ( {
type: ACTION_TYPES.SET_TRANSIENT,
payload: { [ prop ]: value },
} );
/**
* Generic persistent-data updater.
*
* @param {string} prop Name of the property to update.
* @param {any} value The new value of the property.
* @return {Action} The action.
*/
export const setPersistent = ( prop, value ) => ( {
type: ACTION_TYPES.SET_PERSISTENT,
payload: { [ prop ]: value },
} );
/** /**
* Transient. Marks the store as "ready", i.e., fully initialized. * Transient. Marks the store as "ready", i.e., fully initialized.
* *
* @param {boolean} isReady * @param {boolean} isReady
* @return {Action} The action. * @return {Action} The action.
*/ */
export const setIsReady = ( isReady ) => ( { export const setIsReady = ( isReady ) => setTransient( 'isReady', isReady );
type: ACTION_TYPES.SET_TRANSIENT,
payload: { isReady },
} );
/**
* Persistent. Sets a sample value.
* TODO: Replace with a real action/property.
*
* @param {string} value
* @return {Action} The action.
*/
export const setSampleValue = ( value ) => ( {
type: ACTION_TYPES.SET_PERSISTENT,
payload: { sampleValue: value },
} );
/** /**
* Side effect. Triggers the persistence of store data to the server. * Side effect. Triggers the persistence of store data to the server.

View file

@ -7,39 +7,24 @@
* @file * @file
*/ */
import { useSelect, useDispatch } from '@wordpress/data'; import { useDispatch } from '@wordpress/data';
import { createHooksForStore } from '../utils';
import { STORE_NAME } from './constants'; import { STORE_NAME } from './constants';
const useTransient = ( key ) =>
useSelect(
( select ) => select( STORE_NAME ).transientData()?.[ key ],
[ key ]
);
const usePersistent = ( key ) =>
useSelect(
( select ) => select( STORE_NAME ).persistentData()?.[ key ],
[ key ]
);
const useHooks = () => { const useHooks = () => {
const { const { useTransient, usePersistent } = createHooksForStore( STORE_NAME );
persist, const { persist } = useDispatch( STORE_NAME );
// TODO: Replace with real property.
setSampleValue,
} = useDispatch( STORE_NAME );
// Read-only flags and derived state. // Read-only flags and derived state.
// Nothing here yet. // Nothing here yet.
// Transient accessors. // Transient accessors.
const isReady = useTransient( 'isReady' ); const [ isReady ] = useTransient( 'isReady' );
// Persistent accessors. // Persistent accessors.
// TODO: Replace with real property. // TODO: Replace with real property.
const sampleValue = usePersistent( 'sampleValue' ); const [ sampleValue, setSampleValue ] = usePersistent( 'sampleValue' );
return { return {
persist, persist,