2025-01-28 11:52:56 +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
|
|
|
|
*/
|
|
|
|
|
2025-02-03 23:06:33 +01:00
|
|
|
import { select } from '@wordpress/data';
|
2025-01-28 11:52:56 +01:00
|
|
|
import ACTION_TYPES from './action-types';
|
2025-02-03 23:06:33 +01:00
|
|
|
import { STORE_NAME } from './constants';
|
2025-01-28 11:52:56 +01:00
|
|
|
|
|
|
|
export const setIsReady = ( isReady ) => ( {
|
|
|
|
type: ACTION_TYPES.SET_TRANSIENT,
|
|
|
|
payload: { isReady },
|
|
|
|
} );
|
|
|
|
|
|
|
|
export const setTodos = ( todos ) => ( {
|
|
|
|
type: ACTION_TYPES.SET_TODOS,
|
|
|
|
payload: todos,
|
|
|
|
} );
|
|
|
|
|
2025-01-30 12:54:05 +01:00
|
|
|
export const setDismissedTodos = ( dismissedTodos ) => ( {
|
|
|
|
type: ACTION_TYPES.SET_DISMISSED_TODOS,
|
|
|
|
payload: dismissedTodos,
|
|
|
|
} );
|
|
|
|
|
2025-01-28 11:52:56 +01:00
|
|
|
export const fetchTodos = function* () {
|
|
|
|
yield { type: ACTION_TYPES.DO_FETCH_TODOS };
|
|
|
|
};
|
2025-02-03 23:06:33 +01:00
|
|
|
|
|
|
|
export const persist = function* () {
|
|
|
|
const data = yield select( STORE_NAME ).persistentData();
|
|
|
|
yield { type: ACTION_TYPES.DO_PERSIST_DATA, data };
|
|
|
|
};
|
|
|
|
|
|
|
|
export const resetDismissedTodos = function* () {
|
|
|
|
const result = yield { type: ACTION_TYPES.DO_RESET_DISMISSED_TODOS };
|
|
|
|
|
|
|
|
if ( result && result.success ) {
|
2025-02-05 11:19:44 +01:00
|
|
|
yield setDismissedTodos( [] );
|
2025-02-03 23:06:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const setCompletedTodos = ( completedTodos ) => ( {
|
|
|
|
type: ACTION_TYPES.SET_COMPLETED_TODOS,
|
|
|
|
payload: completedTodos,
|
|
|
|
} );
|
2025-02-05 11:19:44 +01:00
|
|
|
|
|
|
|
export const completeOnClick = function* ( todoId ) {
|
|
|
|
const result = yield {
|
|
|
|
type: ACTION_TYPES.DO_COMPLETE_ONCLICK,
|
|
|
|
todoId,
|
|
|
|
};
|
|
|
|
|
|
|
|
if ( result && result.success ) {
|
|
|
|
// Set transient completed state for visual feedback
|
|
|
|
const currentTransientCompleted =
|
|
|
|
yield select( STORE_NAME ).getCompletedTodos();
|
|
|
|
yield setCompletedTodos( [ ...currentTransientCompleted, todoId ] );
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|