Skip to content

Instantly share code, notes, and snippets.

@ozcanzaferayan
Last active January 27, 2020 20:37
Show Gist options
  • Save ozcanzaferayan/09a4bbdfe9581c9552dc38ef1aa2fcf1 to your computer and use it in GitHub Desktop.
Save ozcanzaferayan/09a4bbdfe9581c9552dc38ef1aa2fcf1 to your computer and use it in GitHub Desktop.
sagas directory
import { put, fork, select, all, take, call } from 'redux-saga/effects'
import { getMessages } from 'reducers/selectors'
import { api } from 'services'
import { messages, LOAD_MESSAGES } from 'actions'
function* fetchEntity(entity, apiFn, id) {
yield put( entity.request(id) )
const {response, error} = yield call(apiFn, id)
if(response)
yield put( entity.success(id, response) )
else
yield put( entity.failure(id, error) )
}
export const fetchMessages = fetchEntity.bind(null, messages, api.fetchMessages);
function* loadMessages(binId, requiredFields) {
const messages = yield select(getMessages, binId)
if (!messages || requiredFields.some(key => !messages.hasOwnProperty(key))) {
yield call(fetchMessages, binId)
}
}
function* watchLoadMessages() {
while(true) {
const {binId, requiredFields = []} = yield take(LOAD_MESSAGES)
yield fork(loadMessages, binId, requiredFields)
}
}
export default function* rootSaga() {
yield all([
fork(watchLoadMessages),
])
}
{
"name": "sagas",
"version": "0.0.1"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment