Skip to content

Commit

Permalink
Set field-specific validation messsage in store #6898
Browse files Browse the repository at this point in the history
  • Loading branch information
Shelob9 authored and sc0ttkclark committed Apr 25, 2023
1 parent bb82036 commit 0f090ff
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 9 deletions.
1 change: 1 addition & 0 deletions ui/js/dfv/src/components/connected-field-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const ConnectedFieldWrapper = compose( [
return {
...valueData,
allPodValues,
storeKey,
};
} ),
withDispatch( ( storeDispatch, ownProps ) => {
Expand Down
5 changes: 4 additions & 1 deletion ui/js/dfv/src/components/field-wrapper/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const FieldWrapper = ( props ) => {
values,
setOptionValue,
allPodValues,
storeKey,
} = props;

const {
Expand Down Expand Up @@ -139,7 +140,9 @@ export const FieldWrapper = ( props ) => {
condition: () => true === toBool( required ),
},
],
value
value,
name,
storeKey
);

// Don't render a field that hasn't had its dependencies met.
Expand Down
3 changes: 2 additions & 1 deletion ui/js/dfv/src/components/field-wrapper/subfield-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ const SubfieldWrapper = ( {
// Subfields get their own set of validation rules
const [ validationMessages, addValidationRules ] = useValidation(
[],
value
value,
subfieldConfig.name
);

// Set up useSortable hook
Expand Down
22 changes: 17 additions & 5 deletions ui/js/dfv/src/hooks/useValidation.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
import { useState, useEffect } from 'react';
import {
useSelect,
useDispatch,
} from '@wordpress/data';

const useValidation = ( defaultRules = [], value ) => {
const useValidation = ( defaultRules = [], value, fieldName, strokeKey ) => {
const [ validationRules, setValidationRules ] = useState( defaultRules );
const [ validationMessages, setValidationMessages ] = useState( [] );

const validationMessages = useSelect( ( select ) => {
const currentMessages = select( strokeKey ).getValidationMessages();
//has fieldName ?
if ( currentMessages.hasOwnProperty( fieldName ) ) {
return currentMessages[ fieldName ];
}
return [];
}, [ fieldName ] );

const { setValidationMessages } = useDispatch( strokeKey );

useEffect( () => {
const newMessages = [];
Expand All @@ -20,8 +34,7 @@ const useValidation = ( defaultRules = [], value ) => {
}
}
} );

setValidationMessages( newMessages );
setValidationMessages( fieldName, newMessages );
}, [ value ] );

const addValidationRules = ( rules = [] ) => {
Expand All @@ -34,7 +47,6 @@ const useValidation = ( defaultRules = [], value ) => {
} );
} );
};

return [
validationMessages,
addValidationRules,
Expand Down
3 changes: 2 additions & 1 deletion ui/js/dfv/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,10 +335,11 @@ export const deleteField = ( fieldID, name ) => {
};

//Set the validation messages
export const setValidationMessages = ( validationMessages ) => {
export const setValidationMessages = ( fieldName, validationMessages ) => {
return {
type: CURRENT_POD_ACTIONS.SET_VALIDATION_MESSAGES,
validationMessages,
fieldName,
};
};

Expand Down
5 changes: 4 additions & 1 deletion ui/js/dfv/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,10 @@ export const currentPod = ( state = {}, action = {} ) => {
case CURRENT_POD_ACTIONS.SET_VALIDATION_MESSAGES: {
return {
...state,
validationMessages: action.validationMessages,
validationMessages: {
...state.validationMessages,
[ action.fieldName ]: action.validationMessages,
},
};
}
case CURRENT_POD_ACTIONS.TOGGLE_NEEDS_VALIDATING: {
Expand Down
3 changes: 3 additions & 0 deletions ui/js/dfv/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ export const getPodOptions = ( state ) => CURRENT_POD.getFrom( state );

export const getPodOption = ( state, key ) => CURRENT_POD.getFrom( state )[ key ];

//Get all validation messages
export const getValidationMessages = ( state ) => CURRENT_POD.getFrom( state ).validationMessages;
export const getNeedsValidating = ( state ) => CURRENT_POD.getFrom( state ).needsValidating;
//-- Pod Groups
export const getGroups = ( state ) => GROUPS.getFrom( state );

Expand Down

0 comments on commit 0f090ff

Please sign in to comment.