-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
59 lines (54 loc) · 1.67 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { createSlice } from "@reduxjs/toolkit";
const initialState = {
mode: "light",
user: null,
token: null,
posts: [],
comments: [], // new state for comments
};
export const authSlice = createSlice({
name: "auth",
initialState,
reducers: {
setMode: (state) => {
state.mode = state.mode === "light" ? "dark" : "light";
},
setLogin: (state, action) => {
state.user = action.payload.user;
state.token = action.payload.token;
},
setLogout: (state) => {
state.user = null;
state.token = null;
},
setFriends: (state, action) => {
if (state.user) {
state.user.friends = action.payload.friends;
} else {
console.error("user friends non-existent :(");
}
},
setPosts: (state, action) => {
state.posts = action.payload.posts;
console.log("0 setPosts - from client/state", state.posts);
},
setPost: (state, action) => {
const updatedPosts = state.posts.map((post) => {
if (post._id === action.payload.post._id) return action.payload.post;
console.log("1 setPost - from client/state", post);
return post;
});
state.posts = updatedPosts;
},
setPostComments: (state, action) => { // new reducer for comments
const postIndex = state.posts.findIndex(post => post._id === action.payload.postId);
if (postIndex !== -1) {
state.posts[postIndex].comments = action.payload.comments;
}
state.comments = action.payload.comments;
}
},
});
export const { setMode, setLogin, setLogout, setFriends, setPosts, setPost, setPostComments } =
authSlice.actions;
export default authSlice.reducer;