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 ) { const { payload : { action } , } = undoableAction ; const { onSuccess , onFailure , ... metaWithoutSideEffects } = action . meta ; yield put ( startOptimisticMode ( ) ) ; yield put ( { ... action , type : `${ action . type }_OPTIMISTIC` , meta : { ... metaWithoutSideEffects , ... onSuccess , optimistic : true , } , } ) ; const { complete } = yield race ( { undo : take ( UNDO ) , complete : take ( COMPLETE ) , } ) ; yield put ( stopOptimisticMode ( ) ) ; if ( complete ) { 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 ( ) { yield takeEvery ( UNDOABLE , handleUndoRace ) ; }