Created
October 6, 2021 10:19
-
-
Save techomoro/7a35dbdbe16b666db2377d5f3f2e8a5f to your computer and use it in GitHub Desktop.
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
// 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