I have the following epic file which is having following function,
I need to call action from onUndo but as seen the action.showwarnToast is already dispatch because of which another action is not dispatching
export const makeXDiningReservation = createDataEpic({
name: "makeXDiningReservation",
apiPath: (action, state) =>
generatePath("pathToApi", {
}),
apiOptions: (action, state) => {
return {
headers: {
"Client-Id": "ship",
"Content-Type": "application/json",
},
body: JSON.stringify(data),
};
},
successActionCreator: (
response,
state,
action,
{ getIntl, ...rest },
...params
) => {
if (isDailyPlannerEnabled && eventOverlap) {
return [
Actions.throttledScheduleRefresh({ force: true }),
Actions.diningReservationSuccessOnShip({
...action,
reservationID: response.bookingId,
}),
Actions.navigateReplace({
pathname: generatePath("path", {
restaurantKey: action.restaurantKey,
}),
state: {
action,
response: { ...response, reservationID: response.bookingId },
},
}),
Actions.showWarnToastMessage(
getIntl(state).formatMessage(
strings.activityOverlapsWithAnotherActivity,
),
{
color: "warning",
variant: "standard",
showUndo: true,
undoData: response,
onUndo: (data) => {
return [
Actions.navigatePush({
pathname: generatePath("/dailyPlanner"),
}),
modalActivityActionDrawerActions.setModalState(true),
];
},
},
),
];
} else {
return [
Actions.diningReservationSuccessOnShip({
...action,
reservationID: response.bookingId,
}),
Actions.navigateReplace({
pathname: generatePath("/dining/:restaurantKey/confirm", {
restaurantKey: action.restaurantKey,
}),
state: {
action,
response: { ...response, reservationID: response.bookingId },
},
}),
];
}
},
failureActionCreator: (error, state, action) => [
Actions.epicError(makeXDiningReservation.name, error),
stopSubmit(action.form, { _error: error }),
],
triggeringActions: [START_DINING_RESERVATION],
type: REQUEST_TYPE.POST_JSON,
waitingIdentifier: "DINING_RESERVATION",
});
I tried to dispatch like this but it did not work help me to fix it as i am not sure how this will work, eventually i want this to be done from epic side.
onUndo: (data) => {
return [
Actions.navigatePush({ pathname: generatePath("/dailyPlanner") }),
modalActivityActionDrawerActions.setModalState(true),
];
};
The epics don't have free access to the store's dispatch function since that is what the
successActionCreatorandfailureActionCreatorhandlers do under the hood, but what I'd suggest is to create a thunk that does. The new Thunk would dispatch theshowWarnToastMessageaction to initiate the toast, and can wrap the undo actions in thethunkApi.dispatchcall.Example:
Now instead of dispatching the
Actions.showWarnToastMessageaction directly you will dispatch this newtoastWarnUndoaction in the epic, passing the appropriate props.