import { {{NAME}}_HAS_ERROR, {{NAME}}_IS_LOADING, {{NAME}}_FETCH_DATA_SUCCESS, {{NAME}}_INVALIDATED } from "./action-types"; export function {{name}}HasError(bool) { return { type: {{NAME}}_HAS_ERROR, hasError: bool }; } export function {{name}}IsLoading(bool) { return { type: {{NAME}}_IS_LOADING, isLoading: bool }; } export function {{name}}Invalidated(bool) { return { type: {{NAME}}_INVALIDATED, invalidated: bool }; } export function {{name}}FetchDataSuccess({{name}}) { {{name}}Invalidated(false) return { type: {{NAME}}_FETCH_DATA_SUCCESS, {{name}}, {{name}}FetchedAt: Date.now() }; } export function {{name}}FetchData(url) { return (dispatch) => { dispatch({{name}}IsLoading(true)); fetch(url) .then((response) => { if (!response.ok) { throw Error(response.statusText); } dispatch({{name}}IsLoading(false)); return response; }) .then((response) => response.json()) .then(({{name}}) => { dispatch({{name}}Invalidated(false)); dispatch({{name}}FetchDataSuccess({{name}})); }) .catch(() => dispatch({{name}}HasError(true))); }; }