Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save phamquyhai/be47f7405d96a1d048c6811788d8900b to your computer and use it in GitHub Desktop.
Save phamquyhai/be47f7405d96a1d048c6811788d8900b to your computer and use it in GitHub Desktop.
Implementing confirmation dialog via redux-saga
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