Update post.js

This commit is contained in:
Andrean
2023-05-08 16:48:43 +00:00
committed by GitHub
parent 27c1f07cd5
commit 3c88b8777e
+1 -85
View File
@@ -1,5 +1,4 @@
import {
<<<<<<< HEAD
GET_POSTS,
POST_ERROR,
UPDATE_LIKES,
@@ -82,87 +81,4 @@ function postReducer(state = initialState, action) {
}
export default postReducer;
=======
GET_POSTS,
POST_ERROR,
UPDATE_LIKES,
DELETE_POST,
ADD_POST,
GET_POST,
ADD_COMMENT,
REMOVE_COMMENT
} from '../actions/types';
const initialState = {
posts: [],
post: null,
loading: true,
error: {}
};
function postReducer(state = initialState, action) {
const { type, payload } = action;
switch (type) {
case GET_POSTS:
return {
...state,
posts: payload,
loading: false
};
case GET_POST:
return {
...state,
post: payload,
loading: false
};
case ADD_POST:
return {
...state,
posts: [payload, ...state.posts],
loading: false
};
case DELETE_POST:
return {
...state,
posts: state.posts.filter((post) => post._id !== payload),
loading: false
};
case POST_ERROR:
return {
...state,
error: payload,
loading: false
};
case UPDATE_LIKES:
return {
...state,
posts: state.posts.map((post) =>
post._id === payload.id ? { ...post, likes: payload.likes } : post
),
loading: false
};
case ADD_COMMENT:
return {
...state,
post: { ...state.post, comments: payload },
loading: false
};
case REMOVE_COMMENT:
return {
...state,
post: {
...state.post,
comments: state.post.comments.filter(
(comment) => comment._id !== payload
)
},
loading: false
};
default:
return state;
}
}
export default postReducer;
>>>>>>> cc38df43629d64ca77f694c971a13a026b3afcfb