Skip to content

Instantly share code, notes, and snippets.

@lukebrandonfarrell
Last active June 18, 2019 07:39
Show Gist options
  • Save lukebrandonfarrell/ca9b31a90dde8b6cc0484cdadf6208fb to your computer and use it in GitHub Desktop.
Save lukebrandonfarrell/ca9b31a90dde8b6cc0484cdadf6208fb to your computer and use it in GitHub Desktop.
A saga example from the redux saga network layer pattern.
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