Skip to content

Instantly share code, notes, and snippets.

@guillaumewuip
Last active November 12, 2020 07:12
Show Gist options
  • Save guillaumewuip/63f6f348dd8d83b369c1d6f992e37ab6 to your computer and use it in GitHub Desktop.
Save guillaumewuip/63f6f348dd8d83b369c1d6f992e37ab6 to your computer and use it in GitHub Desktop.
State and Store in frontend codebase - State example - state API
export const decode = (apiPayload: unknown): Loaded | Failure => {
// parse payload
// remove admins
// if something goes wrong, return Failure
};
export const users = (state: Loaded) => state.users;
export const addUser = (user: User.User) => (
state: Loaded
): Loaded | Failure => {
if (state.users.length >= 10) {
return new Error("oops");
}
return {
...state,
users: [...state.users, user]
};
};
export const isLoading = (state: State): state is Loading => // ...
export const isError = (state: State): state is Failure => // ...
export const canCreateUser = (state: State): boolean => //...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment