I am new to ngrx / reactive pattern. I am trying to redesign some complex forms that we have from a template driven approach with component driven models to reactive forms using ngrx.
due to the way our api is written, some of the data from the form needs to be manipulated before posting the request to the server. In my old design, this was a somewhat complex function that I had to write with a ton of conditionals, but because everything was tied directly to the component itself, it was a straightforward approach to do the data manipulation, then subscribe to the endpoint
Now I am trying to use ngrx and ngx-formly for the form, so that I don't have to have multiple components and a ton of cluttered code, but I still need to manipulate the data before it hits the endpoint
Is there a way in ngrx effects for me to dispatch an action right before the http call?
I am still learning ngrx kind of on the fly, so only know the basic calls, but basically, need to know how I can make the form submission dispatch an action that would first call a reducer action to change the state, then right after the state has been updated, call an effect to send that data to the endpoint
Here is the reducer function I am trying to create
export const submitToRmv = (state: RmvServicesState, action) => {
let vehicleArray = [...state.rmvData.rmvServicesUserEnteredData.vehicles];
const transactionType = state.rmvData.transactionType;
if (transactionType === 'PrefillRegistrationTransfer' || vehicleArray[0].reassignedPlate === 'Y') {
const newVeh = {
// modified codehere ...
};
vehicleArray.push(newVeh);
vehicleArray[0].plateNumber = '';
vehicleArray[0].plateType = '';
}
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 created a set of actions
export const validationSubmission = createAction(
'[Rmv Prefill Api] Rmv Validation Submission',
props<{getReadyRequest: RmvServicesData }>()
);
export const validationResponse = createAction(
'[Rmv Prefill Api] Rmv Validation Response',
props<{response: RmvValidationResponse }>()
);
my effect is to trigger that action
rmvSubmit$ = createEffect(() => {
return this.actions$.pipe(
ofType(RmvPrefillActions.validationSubmission),
mergeMap((value) =>
this.rmvService.getReadySubmit(value.getReadyRequest)
.pipe(map(res => RmvPrefillActions.validationResponse({response: res}))
)
));
});
In my form, I am dispatching that action, and what I was hoping to happen was get the reducer function to execute the code to manipulate the data, then somehow, once it is finished, to then trigger the effect In my testing, it looks like the effect is triggering first, as the values being posted to my api is the unmodified data, then when I look at the redux dev tools, I do see that my values have changed, so reducer is working, just not in the order I want it to