我正在尝试使用确认对话框来实现删除史诗。
我想出了这种方法。它的优点是易于测试。
我的问题是,这是一个好方法,我应该担心添加takeUntil(action$.ofType(MODAL_NO_CLICKED))
吗?
如果您能想出更好的方法来实现这一点,请告诉我。
const deleteNotification$ = (id, { ajax }) => ajax({ url: `api/delete/{id}` });
// showYesNo is an action to eventually show a dialog using this approach https://stackoverflow.com/a/35641680/235659
const showYesNo = payload => ({
type: SHOW_MODAL,
modalType: MODAL_TYPE_YES_NO,
modalProps: { ...payload },
});
const deleteNotificationEpic = (action$, store, dependencies) => {
let uid = dependencies.uid; // dependencies.uid is added here to allow passing the uid during unit test.
return merge(
// Show confirmation dialog.
action$.pipe(
ofType(NOTIFICATION_DELETE_REQUEST),
map(action => {
uid = shortid.generate();
return showYesNo({
message: 'NOTIFICATION_DELETE_CONFIRMATION',
payload: {
notificationId: action.notificationId,
uid,
},
})
}),
),
// Deletes the notification if the user clicks on Yes
action$.pipe(
ofType(MODAL_YES_CLICKED),
filter(({ payload }) => payload.uid === uid),
mergeMap(({ payload }) =>
deleteNotification$(payload.notificationId, dependencies).pipe(
mergeMap(() => of(deleteNotificationSuccess())),
catchError(error => of(deleteNotificationSuccess(error))),
),
),
),
);
};
我知道我可以在 React 级别显示确认对话框,并且仅在用户单击“是”时才调度删除操作,但我的问题是一个更普遍的情况,在决定显示之前我可能有一些逻辑(调用后端)确认对话框与否。