This repository was archived by the owner on Jul 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathappReducer.ts
More file actions
72 lines (69 loc) · 1.8 KB
/
appReducer.ts
File metadata and controls
72 lines (69 loc) · 1.8 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
import { AppState } from "types/app";
import {
AppAction,
LOG_IN,
LOG_OUT,
ADD_POST,
SET_POSTS,
DELETE_POST,
SET_CONFIG,
SET_MODERATORS,
SET_MODERATOR_NAMES,
UPDATE_AUTH,
} from "types/actions";
const appReducer = (state: AppState, action: AppAction) => {
switch (action.type) {
case SET_CONFIG: {
return { ...state, ...action.value } as AppState;
}
case UPDATE_AUTH: {
return { ...state, user: { ...state.user, loading: true } } as AppState;
}
case LOG_IN: {
const { user, thread, space, box, moderators } = action.value;
return { ...state, user, thread, space, box, moderators } as AppState;
}
case LOG_OUT: {
return {
...state,
user: {
loggedIn: false,
loading: false,
},
thread: null,
space: null,
box: null,
} as AppState;
}
case ADD_POST: {
const posts = state.posts || [];
posts.unshift(action.value.post);
return { ...state, posts } as AppState;
}
case SET_POSTS: {
return { ...state, posts: action.value.posts } as AppState;
}
case DELETE_POST: {
const { postId } = action.value;
const index = state.posts?.findIndex(
(post) => post.threadData?.postId === postId
);
let posts = state.posts ? [...state.posts] : [];
if (typeof index !== "undefined" && index > -1) {
posts.splice(index, 1);
}
return { ...state, posts } as AppState;
}
case SET_MODERATORS: {
const { moderators } = action.value;
return { ...state, moderators } as AppState;
}
case SET_MODERATOR_NAMES: {
const { moderatorNames } = action.value;
return { ...state, moderatorNames } as AppState;
}
default:
throw new Error();
}
};
export default appReducer;