-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcreate-input-mode-reducer.ts
More file actions
79 lines (74 loc) · 2.09 KB
/
create-input-mode-reducer.ts
File metadata and controls
79 lines (74 loc) · 2.09 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
73
74
75
76
77
78
79
import type {
InputBoxMode,
ReducerAction,
} from '@widgets/community/types/input-box-type';
interface ComposeModeReducerType {
postId: string;
_prev: InputBoxMode;
action: ReducerAction;
}
export const createInputModeReducer = ({
postId,
_prev,
action,
}: ComposeModeReducerType): InputBoxMode => {
switch (action.type) {
case 'COMMENT_CREATE':
return {
type: 'comment',
action: 'create',
postId,
} as const;
case 'COMMENT_EDIT':
return {
type: 'comment',
action: 'edit',
postId,
commentId: action.commentId,
initialContent: action.initialContent,
images: action.images ?? [],
deleteImageIds: [],
} as const;
case 'REPLY_CREATE':
return {
type: 'reply',
action: 'create',
postId,
parentCommentId: action.parentCommentId,
} as const;
case 'REPLY_EDIT':
return {
type: 'reply',
action: 'edit',
postId,
commentId: action.commentId,
commentReplyId: action.commentReplyId,
initialContent: action.initialContent,
images: action.images ?? [],
deleteImageIds: [],
} as const;
case 'COMMENT_EDIT_DELETE_IMAGE': {
if (!(_prev.type === 'comment' && _prev.action === 'edit')) {
return _prev;
}
const nextImages = (_prev.images ?? []).filter(
(img) => img.imageId !== action.imageId,
);
const nextDelete = [...(_prev.deleteImageIds ?? []), action.imageId];
return { ..._prev, images: nextImages, deleteImageIds: nextDelete };
}
case 'REPLY_EDIT_DELETE_IMAGE': {
if (!(_prev.type === 'reply' && _prev.action === 'edit')) {
return _prev;
}
const nextImages = (_prev.images ?? []).filter(
(img) => img.imageId !== action.imageId,
);
const nextDelete = [...(_prev.deleteImageIds ?? []), action.imageId];
return { ..._prev, images: nextImages, deleteImageIds: nextDelete };
}
case 'RESET':
default:
return { type: 'reset', action: 'reset' } as const;
}
};