From 6b7a4445a61106365eb7c79008c3c748c5691796 Mon Sep 17 00:00:00 2001 From: josh Date: Fri, 21 Apr 2023 10:06:38 -0400 Subject: [PATCH] #6898 make new validation state work with reducer/init state --- ui/js/dfv/src/pods-dfv.js | 6 +++++- ui/js/dfv/src/store/actions.js | 15 +++++++-------- ui/js/dfv/src/store/constants.js | 2 +- ui/js/dfv/src/store/reducer.js | 12 ++++++++++++ ui/js/dfv/src/store/store.js | 20 ++++++++------------ 5 files changed, 33 insertions(+), 22 deletions(-) diff --git a/ui/js/dfv/src/pods-dfv.js b/ui/js/dfv/src/pods-dfv.js index 2474f09b36..abc3dbff61 100755 --- a/ui/js/dfv/src/pods-dfv.js +++ b/ui/js/dfv/src/pods-dfv.js @@ -796,7 +796,11 @@ window.PodsDFV = { } else if (window.podsDFVConfig) { return initPodStore( window.podsDFVConfig, - initialStoresWithValues[storeKey], + { + ...initialStoresWithValues[ storeKey ], + validationMessages: [], + needsValidation: false, + }, storeKey, ); } diff --git a/ui/js/dfv/src/store/actions.js b/ui/js/dfv/src/store/actions.js index 67764c27a9..fe5f24b8c2 100644 --- a/ui/js/dfv/src/store/actions.js +++ b/ui/js/dfv/src/store/actions.js @@ -335,17 +335,16 @@ export const deleteField = ( fieldID, name ) => { }; //Set the validation messages -export const setValidationMessages = (messages) => { +export const setValidationMessages = ( validationMessages ) => { return { type: CURRENT_POD_ACTIONS.SET_VALIDATION_MESSAGES, - messages, + validationMessages, }; -} +}; -//Triggers validation of the pod -export const setNeedsValidating = () => { +//Toggle if needs validation of the pod +export const toggleNeedsValidating = () => { return { - type: CURRENT_POD_ACTIONS.SET_NEEDS_VALIDATING, - needsValidating: true, + type: CURRENT_POD_ACTIONS.TOGGLE_NEEDS_VALIDATING, }; -} +}; diff --git a/ui/js/dfv/src/store/constants.js b/ui/js/dfv/src/store/constants.js index 99bf4e7952..0e56ccee15 100644 --- a/ui/js/dfv/src/store/constants.js +++ b/ui/js/dfv/src/store/constants.js @@ -55,7 +55,7 @@ export const CURRENT_POD_ACTIONS = { //Validation //@see https://github.com/pods-framework/pods/pull/7064 SET_VALIDATION_MESSAGES: 'CURRENT_POD/SET_VALIDATION_MESSAGES', - SET_NEEDS_VALIDATION: 'CURRENT_POD/SET_NEEDS_VALIDATION', + TOGGLE_NEEDS_VALIDATING: 'CURRENT_POD/TOGGLE_NEEDS_VALIDATING', }; export const INITIAL_UI_STATE = { diff --git a/ui/js/dfv/src/store/reducer.js b/ui/js/dfv/src/store/reducer.js index 5e95281bf3..e6d069d34a 100644 --- a/ui/js/dfv/src/store/reducer.js +++ b/ui/js/dfv/src/store/reducer.js @@ -351,6 +351,18 @@ export const currentPod = ( state = {}, action = {} ) => { groups, }; } + case CURRENT_POD_ACTIONS.SET_VALIDATION_MESSAGES: { + return { + ...state, + validationMessages: action.validationMessages, + }; + } + case CURRENT_POD_ACTIONS.TOGGLE_NEEDS_VALIDATING: { + return { + ...state, + needsValidating: ! state.needsValidating, + }; + } default: { return state; diff --git a/ui/js/dfv/src/store/store.js b/ui/js/dfv/src/store/store.js index b8fb37802e..b12ac30726 100644 --- a/ui/js/dfv/src/store/store.js +++ b/ui/js/dfv/src/store/store.js @@ -27,22 +27,18 @@ import apiMiddleware from './api-middleware'; * @param {int} formCounter Form index. (Optional.) * @param {string} prefix Prefix. (Optional.) */ - export const createStoreKey = ( pod, itemId, formCounter = 0, prefix = '' ) => { - return prefix.length ? - `${ prefix }-${ pod }-${ itemId }-${ formCounter }` +export const createStoreKey = ( pod, itemId, formCounter = 0, prefix = '' ) => { + return prefix.length + ? `${ prefix }-${ pod }-${ itemId }-${ formCounter }` : `${ pod }-${ itemId }-${ formCounter }`; }; -const initStore = (initialState, storeKey) => { - const reduxStore = configureStore({ +const initStore = ( initialState, storeKey ) => { + const reduxStore = configureStore( { reducer, - middleware: [apiMiddleware], - preloadedState: { - validationMessages: [], - needsValidating: false, - ...initialState - }, - }); + middleware: [ apiMiddleware ], + preloadedState: initialState, + } ); const mappedSelectors = Object.keys( selectors ).reduce( ( acc, selectorKey ) => { acc[ selectorKey ] = ( ...args ) =>