Created
April 21, 2016 15:43
-
-
Save calvinfroedge/2a313ed525ba0c136284075b79640428 to your computer and use it in GitHub Desktop.
Example notifications middleware with 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 { takeLatest } from 'redux-saga' | |
import { EVENT1, EVENT2, EVENTETC } from '../constants' | |
import { put } from 'redux-saga/effects' | |
import { addAlert } from '../actions/alerts' | |
import t from '../../i18n/helper' | |
function* dispatchAlert(action) { | |
let message; | |
try { | |
message = t('alerts.'+action.type); | |
yield put(addAlert({message})); | |
} catch(e){ | |
if(e.name != 'SagaCancellationException') throw(e); | |
} | |
} | |
//Check every action to see if we should dispatch an alert | |
export default function* alerts() { | |
yield* takeLatest([ | |
EVENT1, | |
EVENT2, | |
EVENTETC | |
], dispatchAlert); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment