From 2477e4d357a102858228eb45ca96ab21a8834004 Mon Sep 17 00:00:00 2001
From: Philipp Stracker
Date: Mon, 17 Feb 2025 14:09:12 +0100
Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20missing=20reset=20action=20to?=
=?UTF-8?q?=20todo=20store?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../resources/js/data/todos/action-types.js | 6 +++
.../resources/js/data/todos/actions.js | 42 +++++++++++++++++--
.../resources/js/data/todos/reducer.js | 16 +++++++
3 files changed, 61 insertions(+), 3 deletions(-)
diff --git a/modules/ppcp-settings/resources/js/data/todos/action-types.js b/modules/ppcp-settings/resources/js/data/todos/action-types.js
index 66d92218a..17bbe8a0f 100644
--- a/modules/ppcp-settings/resources/js/data/todos/action-types.js
+++ b/modules/ppcp-settings/resources/js/data/todos/action-types.js
@@ -5,6 +5,12 @@
*/
export default {
+ /**
+ * Resets the store state to its initial values.
+ * Used when needing to clear all store data.
+ */
+ RESET: 'ppcp/todos/RESET',
+
// Transient data
SET_TRANSIENT: 'ppcp/todos/SET_TRANSIENT',
SET_COMPLETED_TODOS: 'ppcp/todos/SET_COMPLETED_TODOS',
diff --git a/modules/ppcp-settings/resources/js/data/todos/actions.js b/modules/ppcp-settings/resources/js/data/todos/actions.js
index 864fe4173..25b6d7647 100644
--- a/modules/ppcp-settings/resources/js/data/todos/actions.js
+++ b/modules/ppcp-settings/resources/js/data/todos/actions.js
@@ -17,11 +17,47 @@ import {
REST_RESET_DISMISSED_TODOS_PATH,
} from './constants';
-export const setIsReady = ( isReady ) => ( {
- type: ACTION_TYPES.SET_TRANSIENT,
- payload: { isReady },
+/**
+ * Special. Resets all values in the store to initial defaults.
+ *
+ * @return {Object} The action.
+ */
+export const reset = () => ( {
+ type: ACTION_TYPES.RESET,
} );
+/**
+ * Generic transient-data updater.
+ *
+ * @param {string} prop Name of the property to update.
+ * @param {any} value The new value of the property.
+ * @return {Object} 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 {Object} 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.
+ *
+ * @param {boolean} isReady Whether the store is ready
+ * @return {Object} The action.
+ */
+export const setIsReady = ( isReady ) => setTransient( 'isReady', isReady );
+
export const setTodos = ( todos ) => ( {
type: ACTION_TYPES.SET_TODOS,
payload: todos,
diff --git a/modules/ppcp-settings/resources/js/data/todos/reducer.js b/modules/ppcp-settings/resources/js/data/todos/reducer.js
index 6f4c74d46..f3cc412ac 100644
--- a/modules/ppcp-settings/resources/js/data/todos/reducer.js
+++ b/modules/ppcp-settings/resources/js/data/todos/reducer.js
@@ -52,6 +52,21 @@ const reducer = createReducer( defaultTransient, defaultPersistent, {
[ ACTION_TYPES.SET_TRANSIENT ]: ( state, payload ) =>
changeTransient( state, payload ),
+ /**
+ * Resets state to defaults while maintaining initialization status
+ *
+ * @param {Object} state Current state
+ * @return {Object} Reset state
+ */
+ [ ACTION_TYPES.RESET ]: ( state ) => {
+ const cleanState = changeTransient(
+ changePersistent( state, defaultPersistent ),
+ defaultTransient
+ );
+ cleanState.isReady = true; // Keep initialization flag
+ return cleanState;
+ },
+
/**
* Updates todos list
*
@@ -99,6 +114,7 @@ const reducer = createReducer( defaultTransient, defaultPersistent, {
},
/**
+ * TODO: This is not used anywhere. Remove "SET_TODOS" and use this resolver instead.
* Initializes persistent state with data from the server
*
* @param {Object} state Current state