Skip to content

Instantly share code, notes, and snippets.

@willprice76
Last active May 1, 2020 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willprice76/2996c5b5b5f4d0bb45ff128a10640bc0 to your computer and use it in GitHub Desktop.
Save willprice76/2996c5b5b5f4d0bb45ff128a10640bc0 to your computer and use it in GitHub Desktop.
Non-generic reducer for load lifecycle
import { Recipe } from "../api";
export type LoadRecipesActionState = {
recipe?: Recipe[];
isLoading: boolean;
loadingFailed: boolean;
loadingError?: string;
};
export type LoadRecipesActionResult =
| { type: "busy" }
| { type: "success"; result: Recipe[] }
| { type: "error"; error: string };
export const createLoadRecipesReducer = (
state: LoadRecipesActionState,
action: LoadRecipesActionResult
): LoadRecipesActionState => {
switch (action.type) {
case "busy":
return { isLoading: true, loadingFailed: false };
case "success":
return { isLoading: false, loadingFailed: false, recipe: action.result };
case "error":
return { isLoading: false, loadingFailed: true, loadingError: action.error };
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment