Last active
June 18, 2019 07:39
-
-
Save lukebrandonfarrell/ca9b31a90dde8b6cc0484cdadf6208fb to your computer and use it in GitHub Desktop.
A saga example from the redux saga network layer pattern.
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 { all, call, put, takeLatest } from "redux-saga/effects"; | |
import { FETCH_USER_ORDERS_REQUEST, FETCH_USER_ORDERS_RESPONSE } from "../types"; | |
import { fetchOrdersRequest } from "../api"; | |
export function* fetchUserOrders(action, baseUrl) { | |
try { | |
const { filter } = action.payload; | |
const orders = yield call(fetchOrdersRequest, baseUrl, filter); | |
yield put({ | |
type: FETCH_USER_ORDERS_RESPONSE, | |
payload: { data: orders } | |
}); | |
} catch (e) { | |
const data = e.response.data || {}; | |
yield put({ | |
type: FETCH_USER_ORDERS_RESPONSE, | |
payload: { ...data }, | |
error: true | |
}); | |
} | |
} | |
export default function* UserOrdersSaga(baseUrl) { | |
yield all([ | |
takeLatest(FETCH_USER_ORDERS_REQUEST, action => fetchUserOrders(action, baseUrl)); | |
]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment