2025-01-13 16:16:03 +01:00
|
|
|
/**
|
|
|
|
* Action Creators: Define functions to create action objects.
|
|
|
|
*
|
|
|
|
* These functions update state or trigger side effects (e.g., async operations).
|
|
|
|
* Actions are categorized as Transient, Persistent, or Side effect.
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { select } from '@wordpress/data';
|
|
|
|
|
|
|
|
import ACTION_TYPES from './action-types';
|
|
|
|
import { STORE_NAME } from './constants';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object} Action An action object that is handled by a reducer or control.
|
|
|
|
* @property {string} type - The action type.
|
|
|
|
* @property {Object?} payload - Optional payload for the action.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Special. Resets all values in the store to initial defaults.
|
|
|
|
*
|
|
|
|
* @return {Action} The action.
|
|
|
|
*/
|
|
|
|
export const reset = () => ( { type: ACTION_TYPES.RESET } );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Persistent. Set the full store details during app initialization.
|
|
|
|
*
|
|
|
|
* @param {{data: {}, flags?: {}}} payload
|
|
|
|
* @return {Action} The action.
|
|
|
|
*/
|
|
|
|
export const hydrate = ( payload ) => ( {
|
|
|
|
type: ACTION_TYPES.HYDRATE,
|
|
|
|
payload,
|
|
|
|
} );
|
|
|
|
|
|
|
|
/**
|
2025-01-17 19:25:10 +01:00
|
|
|
* Generic transient-data updater.
|
2025-01-13 16:16:03 +01:00
|
|
|
*
|
2025-01-17 19:25:10 +01:00
|
|
|
* @param {string} prop Name of the property to update.
|
|
|
|
* @param {any} value The new value of the property.
|
2025-01-13 16:16:03 +01:00
|
|
|
* @return {Action} The action.
|
|
|
|
*/
|
2025-01-17 19:25:10 +01:00
|
|
|
export const setTransient = ( prop, value ) => ( {
|
2025-01-13 16:16:03 +01:00
|
|
|
type: ACTION_TYPES.SET_TRANSIENT,
|
2025-01-17 19:25:10 +01:00
|
|
|
payload: { [ prop ]: value },
|
2025-01-13 16:16:03 +01:00
|
|
|
} );
|
|
|
|
|
|
|
|
/**
|
2025-01-17 19:25:10 +01:00
|
|
|
* Generic persistent-data updater.
|
2025-01-13 16:16:03 +01:00
|
|
|
*
|
2025-01-17 19:25:10 +01:00
|
|
|
* @param {string} prop Name of the property to update.
|
|
|
|
* @param {any} value The new value of the property.
|
2025-01-13 16:16:03 +01:00
|
|
|
* @return {Action} The action.
|
|
|
|
*/
|
2025-01-17 19:25:10 +01:00
|
|
|
export const setPersistent = ( prop, value ) => ( {
|
2025-01-13 16:16:03 +01:00
|
|
|
type: ACTION_TYPES.SET_PERSISTENT,
|
2025-01-17 19:25:10 +01:00
|
|
|
payload: { [ prop ]: value },
|
2025-01-13 16:16:03 +01:00
|
|
|
} );
|
|
|
|
|
2025-01-17 19:25:10 +01:00
|
|
|
/**
|
|
|
|
* Transient. Marks the store as "ready", i.e., fully initialized.
|
|
|
|
*
|
|
|
|
* @param {boolean} isReady
|
|
|
|
* @return {Action} The action.
|
|
|
|
*/
|
|
|
|
export const setIsReady = ( isReady ) => setTransient( 'isReady', isReady );
|
|
|
|
|
2025-01-13 16:16:03 +01:00
|
|
|
/**
|
|
|
|
* Side effect. Triggers the persistence of store data to the server.
|
|
|
|
*
|
|
|
|
* @return {Action} The action.
|
|
|
|
*/
|
|
|
|
export const persist = function* () {
|
|
|
|
const data = yield select( STORE_NAME ).persistentData();
|
|
|
|
|
|
|
|
yield { type: ACTION_TYPES.DO_PERSIST_DATA, data };
|
|
|
|
};
|