mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
♻️ Provide cleaner sample for the store’s hooks
This commit is contained in:
parent
60d747ea44
commit
de24a30fac
1 changed files with 29 additions and 22 deletions
|
@ -7,44 +7,51 @@
|
|||
* @file
|
||||
*/
|
||||
|
||||
import { useDispatch } from '@wordpress/data';
|
||||
import { useMemo } from '@wordpress/element';
|
||||
import { useDispatch, useSelect } from '@wordpress/data';
|
||||
|
||||
import { createHooksForStore } from '../utils';
|
||||
import { STORE_NAME } from './constants';
|
||||
|
||||
const useHooks = () => {
|
||||
/**
|
||||
* Single source of truth for access Redux details.
|
||||
*
|
||||
* This hook returns a stable API to access actions, selectors and special hooks to generate
|
||||
* getter- and setters for transient or persistent properties.
|
||||
*
|
||||
* @return {{select, dispatch, useTransient, usePersistent}} Store data API.
|
||||
*/
|
||||
const useStoreData = () => {
|
||||
const select = useSelect( ( selectors ) => selectors( STORE_NAME ), [] );
|
||||
const dispatch = useDispatch( STORE_NAME );
|
||||
const { useTransient, usePersistent } = createHooksForStore( STORE_NAME );
|
||||
const { persist } = useDispatch( STORE_NAME );
|
||||
|
||||
// Read-only flags and derived state.
|
||||
// Nothing here yet.
|
||||
|
||||
// Transient accessors.
|
||||
const [ isReady ] = useTransient( 'isReady' );
|
||||
|
||||
// Persistent accessors.
|
||||
// TODO: Replace with real property.
|
||||
const [ sampleValue, setSampleValue ] = usePersistent( 'sampleValue' );
|
||||
|
||||
return {
|
||||
persist,
|
||||
isReady,
|
||||
sampleValue,
|
||||
setSampleValue,
|
||||
};
|
||||
return useMemo(
|
||||
() => ( {
|
||||
select,
|
||||
dispatch,
|
||||
useTransient,
|
||||
usePersistent,
|
||||
} ),
|
||||
[ select, dispatch, useTransient, usePersistent ]
|
||||
);
|
||||
};
|
||||
|
||||
export const useStore = () => {
|
||||
const { persist, isReady } = useHooks();
|
||||
return { persist, isReady };
|
||||
const { dispatch, useTransient } = useStoreData();
|
||||
const [ isReady ] = useTransient( 'isReady' );
|
||||
|
||||
return { persist: dispatch.persist, isReady };
|
||||
};
|
||||
|
||||
// TODO: Replace with real hook.
|
||||
export const useSampleValue = () => {
|
||||
const { sampleValue, setSampleValue } = useHooks();
|
||||
const { usePersistent, select } = useStoreData();
|
||||
const [ sampleValue, setSampleValue ] = usePersistent( 'sampleValue' );
|
||||
|
||||
return {
|
||||
sampleValue,
|
||||
setSampleValue,
|
||||
flags: select.flags(),
|
||||
};
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue