Last active
March 14, 2019 15:02
-
-
Save tomkis/109091640e7d27f5da8352598f046136 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
Do you have a completed example on this ?