Created
November 12, 2020 06:55
-
-
Save guillaumewuip/464c0f3c1346167724630a00aa270e10 to your computer and use it in GitHub Desktop.
State and Store in frontend codebases - Redux example - reducer
This file contains hidden or 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
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