I'm new to ngrx, and trying to redesign my complex template driven form into using ngx-formly and ngrx. I am stuck on the form submission because of a complex business rule we have in place Basically, right before submission, I need to modify some of the data that was entered in the form because our api needs some data that the form does not provide, or needs it formatted differently based on conditions.
So I created a reducer function to handle that data manipulate:
export const submitToRmv = (state: RmvServicesState, action) => {
let vehicleArray = [...state.rmvData.rmvServicesUserEnteredData.vehicles];
const transactionType = state.rmvData.transactionType;
if (transactionType === 'PrefillRegistrationTransfer' || vehicleArray[0].reassignedPlate === 'Y') {
//data manipulation is here, but removed just to simplify example
}
vehicleArray = [...vehicleArray, ...state.rmvData.rmvServicesUserEnteredData.tradeInVehicles];
const newTransactionType = transactionType === 'PrefillNewTitleAndRegistration' || transactionType === 'PrefillRegistrationTransfer' &&
vehicleArray[0].reassignedPlate === 'Y' ? 'Reassign' : transactionType.replace('Prefill', '');
return {
...state,
rmvData: {
...state.rmvData,
transactionType: newTransactionType,
rmvServicesUserEnteredData: {
...state.rmvData.rmvServicesUserEnteredData,
vehicles: vehicleArray,
},
},
};
I also created the effect to get triggered when submission occurs
rmvSubmit$ = createEffect(() => {
return this.actions$.pipe(
ofType(RmvPrefillActions.validationSubmission),
mergeMap((value) =>
this.rmvService.getReadySubmit(value.getReadyRequest)
.pipe(map(res => RmvPrefillActions.validationResponse({response: res}))
)
));
});
But I am stuck trying to figure out how to get my action to trigger before the effect does. My issue is that if I use the same action for both the reducer and effect, when submission occurs, it seems to be triggering the effect first, then the reducer, but if I create a different action for the reducer, I am stuck trying to figure out where to dispatch the action because I need to the reducer to change the state, then call the effect with that new state passed into the http service call