-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
Copy pathundo.ts
54 lines (51 loc) · 1.59 KB
/
undo.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { take, takeEvery, put, race } from 'redux-saga/effects';
import { showNotification } from '../actions/notificationActions';
import {
UNDOABLE,
UNDO,
COMPLETE,
startOptimisticMode,
stopOptimisticMode,
} from '../actions/undoActions';
import { refreshView } from '../actions/uiActions';
export function* handleUndoRace(undoableAction: { payload: { action: any } }) {
const {
payload: { action },
} = undoableAction;
const { onSuccess, onFailure, ...metaWithoutSideEffects } = action.meta;
yield put(startOptimisticMode());
// dispatch action in optimistic mode (no fetch), with success side effects
yield put({
...action,
type: `${action.type}_OPTIMISTIC`,
meta: {
...metaWithoutSideEffects,
...onSuccess,
optimistic: true,
},
});
// wait for undo or delay
const { complete } = yield race({
undo: take(UNDO),
complete: take(COMPLETE),
});
yield put(stopOptimisticMode());
if (complete) {
// if not cancelled, redispatch the action, this time immediate, and without success side effect
yield put({
...action,
meta: {
...metaWithoutSideEffects,
onSuccess: { refresh: true },
onFailure: { ...onFailure, refresh: true },
},
});
} else {
yield put(showNotification('ra.notification.canceled'));
yield put(refreshView());
}
}
export default function* watchUndoable() {
// @ts-ignore
yield takeEvery(UNDOABLE, handleUndoRace);
}