Skip to content

Commit

Permalink
#6898 make new validation state work with reducer/init state
Browse files Browse the repository at this point in the history
  • Loading branch information
Shelob9 committed Apr 21, 2023
1 parent 26fa0da commit b39c5b4
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 22 deletions.
6 changes: 5 additions & 1 deletion ui/js/dfv/src/pods-dfv.js
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,11 @@ window.PodsDFV = {
} else if (window.podsDFVConfig) {
return initPodStore(
window.podsDFVConfig,
initialStoresWithValues[storeKey],
{
...initialStoresWithValues[ storeKey ],
validationMessages: [],
needsValidation: false,
},
storeKey,
);
}
Expand Down
15 changes: 7 additions & 8 deletions ui/js/dfv/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}
};
2 changes: 1 addition & 1 deletion ui/js/dfv/src/store/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
12 changes: 12 additions & 0 deletions ui/js/dfv/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
20 changes: 8 additions & 12 deletions ui/js/dfv/src/store/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) =>
Expand Down

0 comments on commit b39c5b4

Please sign in to comment.