Skip to content

Instantly share code, notes, and snippets.

@tomkis
Last active March 14, 2019 15:02
Show Gist options
  • Save tomkis/109091640e7d27f5da8352598f046136 to your computer and use it in GitHub Desktop.
Save tomkis/109091640e7d27f5da8352598f046136 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
});
});
}
@rsharmainstart
Copy link

Do you have a completed example on this ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment