Forked from tomkis/redux-saga-confirmation-dialog.js
Created
August 17, 2018 09:55
-
-
Save phamquyhai/be47f7405d96a1d048c6811788d8900b to your computer and use it in GitHub Desktop.
Implementing confirmation dialog via redux-saga
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { select, put, take } from 'redux-saga/effects'; | |
function* emptySaga() {} | |
export function* withConfirmation(text, onConfirm, onCancel = emptySaga) { | |
yield put({ type: 'ShowConfirmationDialog', payload: text }); | |
const { type } = yield take([ | |
'ConfirmationDialogConfirmed', | |
'ConfirmationDialogCanceled' | |
]); | |
switch (type) { | |
case 'ConfirmationDialogConfirmed': | |
yield* onConfirm(); | |
break; | |
case 'ConfirmationDialogCanceled': | |
yield* onCancel(); | |
break; | |
default: | |
throw `${type} - Missing impl`; | |
} | |
yield put({ type: 'HideConfirmationDialog' }); | |
} | |
export function* requestDelete({ payload }) { | |
const { | |
id, | |
firstName, | |
lastName | |
} = yield select(appState => appState.list.users.find(({ id }) => id === payload)); | |
yield* withConfirmation(`Are you sure that you want to delete ${firstName} ${lastName}?`, function*() { | |
yield put({ | |
type: 'DeleteUser', | |
payload: id | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment