Last active
May 1, 2020 13:53
-
-
Save willprice76/2996c5b5b5f4d0bb45ff128a10640bc0 to your computer and use it in GitHub Desktop.
Non-generic reducer for load lifecycle
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 { 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