Last active
January 27, 2020 20:37
-
-
Save ozcanzaferayan/09a4bbdfe9581c9552dc38ef1aa2fcf1 to your computer and use it in GitHub Desktop.
sagas directory
This file contains hidden or 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 { 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), | |
]) | |
} |
This file contains hidden or 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
{ | |
"name": "sagas", | |
"version": "0.0.1" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment