Skip to content

Instantly share code, notes, and snippets.

@guillaumewuip
Created November 12, 2020 06:55
Show Gist options
  • Save guillaumewuip/464c0f3c1346167724630a00aa270e10 to your computer and use it in GitHub Desktop.
Save guillaumewuip/464c0f3c1346167724630a00aa270e10 to your computer and use it in GitHub Desktop.
State and Store in frontend codebases - Redux example - reducer
const initialState = { loading: true, users: [], error: undefined };
export function reducer(state = initialState, action) {
console.log({ type: action.type, action });
switch (action.type) {
case "FETCH_USERS_SUCCEEDED":
return {
...state,
loading: false,
users: action.payload.users.filter(
(apiUser) => apiUser.type !== "ADMIN"
)
};
case "FETCH_USERS_FAILED":
return {
...state,
loading: false,
error: new Error("oops")
};
case "CREATE_USER_SUCCEEDED":
return {
...state,
users: [...state.users, action.payload.user]
};
case "CREATE_USER_FAILED":
return {
...state,
error: new Error("oops")
};
default: {
return state;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment