Skip to content

Instantly share code, notes, and snippets.

@techomoro
Created October 6, 2021 10:19
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 techomoro/7a35dbdbe16b666db2377d5f3f2e8a5f to your computer and use it in GitHub Desktop.
Save techomoro/7a35dbdbe16b666db2377d5f3f2e8a5f to your computer and use it in GitHub Desktop.
// store/posts/reducer.js
import {
GET_POSTS,
GET_POSTS_SUCCESS,
GET_POSTS_FAIL,
GET_POST_DETAILS,
GET_POST_DETAILS_SUCCESS,
GET_POST_DETAILS_FAIL,
} from "./actionTypes";
const initialState = {
posts: [],
post: {},
loadingPosts: false,
loadingPostDetails: false,
error: {
message: "",
},
};
const PostReducer = (state = initialState, action) => {
switch (action.type) {
case GET_POSTS:
state = { ...state, loadingPosts: true };
break;
case GET_POSTS_SUCCESS:
state = { ...state, posts: action.payload, loadingPosts: false };
break;
case GET_POSTS_FAIL:
state = {
...state,
error: {
message: "Error",
},
loadingPosts: false,
};
break;
case GET_POST_DETAILS:
state = { ...state, loadingPostDetails: true };
break;
case GET_POST_DETAILS_SUCCESS:
state = { ...state, post: action.payload[0], loadingPostDetails: false };
break;
case GET_POST_DETAILS_FAIL:
state = {
...state,
error: {
message: "Error",
},
loadingPostDetails: false,
};
break;
default:
state = { ...state };
break;
}
return state;
};
export default PostReducer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment