Skip to content

Commit

Permalink
#689 start adding validation messages to store
Browse files Browse the repository at this point in the history
  • Loading branch information
Shelob9 authored and sc0ttkclark committed Apr 25, 2023
1 parent 4397eb3 commit bc64340
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 15 deletions.
16 changes: 16 additions & 0 deletions ui/js/dfv/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,19 @@ export const deleteField = ( fieldID, name ) => {
},
};
};

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

//Triggers validation of the pod
export const setNeedsValidating = () => {
return {
type: CURRENT_POD_ACTIONS.SET_NEEDS_VALIDATING,
needsValidating: true,
};
}
5 changes: 5 additions & 0 deletions ui/js/dfv/src/store/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ export const CURRENT_POD_ACTIONS = {
SET_GROUP_FIELD_DATA: 'CURRENT_POD/SET_GROUP_FIELD_DATA',

API_REQUEST: 'CURRENT_POD/API_REQUEST',

//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',
};

export const INITIAL_UI_STATE = {
Expand Down
14 changes: 9 additions & 5 deletions ui/js/dfv/src/store/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,16 @@ import apiMiddleware from './api-middleware';
: `${ pod }-${ itemId }-${ formCounter }`;
};

const initStore = ( initialState, storeKey ) => {
const reduxStore = configureStore( {
const initStore = (initialState, storeKey) => {
const reduxStore = configureStore({
reducer,
middleware: [ apiMiddleware ],
preloadedState: initialState,
} );
middleware: [apiMiddleware],
preloadedState: {
validationMessages: [],
needsValidating: false,
...initialState
},
});

const mappedSelectors = Object.keys( selectors ).reduce( ( acc, selectorKey ) => {
acc[ selectorKey ] = ( ...args ) =>
Expand Down
46 changes: 36 additions & 10 deletions ui/js/dfv/src/store/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ import {
saveGroup,
deleteGroup,

//Validation related
setValidationMessages,
setNeedsValidating,

// setGroupFields,
// addGroupField,
// saveField,
Expand Down Expand Up @@ -322,15 +326,37 @@ describe( 'actions', () => {
expect( result.payload.onStart().type ).toEqual( UI_ACTIONS.SET_GROUP_SAVE_STATUS );
} );

test( 'deleteGroup() returns an action to delete a group by its ID', () => {
test('deleteGroup() returns an action to delete a group by its ID', () => {
const action = CURRENT_POD_ACTIONS.API_REQUEST;

const result = deleteGroup( 123 );

expect( result.type ).toEqual( action );
expect( result.payload.onSuccess().type ).toEqual( UI_ACTIONS.SET_GROUP_DELETE_STATUS );
expect( result.payload.onFailure().type ).toEqual( UI_ACTIONS.SET_GROUP_DELETE_STATUS );
expect( result.payload.onStart().type ).toEqual( UI_ACTIONS.SET_GROUP_DELETE_STATUS );
} );
} );
} );
const result = deleteGroup(123);

expect(result.type).toEqual(action);
expect(result.payload.onSuccess().type).toEqual(UI_ACTIONS.SET_GROUP_DELETE_STATUS);
expect(result.payload.onFailure().type).toEqual(UI_ACTIONS.SET_GROUP_DELETE_STATUS);
expect(result.payload.onStart().type).toEqual(UI_ACTIONS.SET_GROUP_DELETE_STATUS);
});

test('setValidationMessages sets messages', () => {
const action = CURRENT_POD_ACTIONS.SET_VALIDATION_MESSAGES;

const result = setValidationMessages([
'hi',
'roy'
]);

expect(result.type).toEqual(action);
expect(result.messages).toEqual([
'hi',
'roy'
]);
})

test('setNeedsValidating sets validation needed', () => {
const action = CURRENT_POD_ACTIONS.SET_VALIDATION_MESSAGES;
const result = setNeedsValidating();
expect(result.type).toEqual(action);
expect(result.needsValidating).toEqual(true);
})
});
});

0 comments on commit bc64340

Please sign in to comment.