-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcomment-panel.tsx
More file actions
143 lines (131 loc) · 4.35 KB
/
comment-panel.tsx
File metadata and controls
143 lines (131 loc) · 4.35 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import React, { SetStateAction, useEffect } from "react"
import * as Dailp from "src/graphql/dailp"
import * as css from "./comment-panel.css"
import { Button } from "./components"
import { useCommentValueContext } from "./components/edit-comment-feature"
import { TranslatedParagraph } from "./segment"
// The type of comment panel that is displayed.
export enum CommentAction {
PostComment = "PostComment",
EditComment = "EditComment",
}
// Displays the comment panel for a selected segment.
export const CommentPanel = (p: {
segment: Dailp.FormFieldsFragment | TranslatedParagraph
setIsCommenting: React.Dispatch<SetStateAction<boolean>>
commentAction: CommentAction
commentObject?: Dailp.CommentFieldsFragment
}) => {
const { commentValues, setCommentValues } = useCommentValueContext()
useEffect(() => {
setCommentValues((prev) => ({
...prev,
id: p.commentObject?.id.toString() ?? "",
textContent: p.commentObject?.textContent ?? "",
commentType: p.commentObject?.commentType ?? "STORY",
}))
}, [p.commentObject?.id])
const [postCommentResult, postComment] = Dailp.usePostCommentMutation()
const [editCommentResult, editComment] = Dailp.useUpdateCommentMutation()
const commentTypeNames: Record<Dailp.CommentType, string> = {
// ... TS will then make sure you have an entry for everything on the "CommentTag" type that you import from the codegen
[Dailp.CommentType.Story]: "Story",
[Dailp.CommentType.Suggestion]: "Suggestion",
[Dailp.CommentType.Question]: "Question",
}
const handleInputChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
setCommentValues({
...commentValues,
textContent: event.target.value,
})
}
const handleSelectChange = (event: React.ChangeEvent<HTMLSelectElement>) => {
setCommentValues({
...commentValues,
commentType: event.target.value,
})
}
const handleSubmit = async (e: React.MouseEvent<HTMLButtonElement>) => {
e.preventDefault()
const handleError = (error: any, action: string) => {
if (error) {
console.error(error)
alert(`Something went wrong ${action}`)
} else {
console.log("Submitted!")
alert(`Your comment has been ${action}`)
p.setIsCommenting(false)
}
}
if (commentValues.textContent && commentValues.commentType) {
if (p.commentAction === CommentAction.PostComment) {
const { error: postError } = await postComment({
input: {
parentId: p.segment.id,
parentType:
p.segment.__typename === "DocumentParagraph"
? Dailp.CommentParentType.Paragraph
: Dailp.CommentParentType.Word,
textContent: commentValues.textContent,
commentType: commentValues.commentType as Dailp.CommentType,
},
})
handleError(postError, "posted")
}
} else {
alert("Please add a comment before submitting.")
}
}
return (
<div>
{!(p.commentAction === CommentAction.EditComment) ? (
<header className={css.wordPanelHeader}>
<h2 className={css.editCherHeader}>
{p.segment.__typename === "DocumentParagraph"
? "Paragraph " + p.segment?.index
: p.segment.source}
</h2>
</header>
) : (
<></>
)}
<form>
<textarea
placeholder="Add a comment..."
value={commentValues.textContent}
onChange={handleInputChange}
className={css.inputStyling}
/>
<div>
<label htmlFor="dropdown" className={css.spacing}>
Tag:
</label>
<select
id="dropdown"
value={commentValues.commentType}
onChange={handleSelectChange}
className={css.spacing}
>
{Object.entries<string>(commentTypeNames).map(([option, label]) => (
<option key={option} value={option}>
{label}
</option>
))}
</select>
</div>
{!(p.commentAction === CommentAction.EditComment) ? (
<Button
type="button"
className={css.commentButton}
onClick={handleSubmit}
>
Save
</Button>
) : (
<></>
)}
</form>
</div>
)
}
export default CommentPanel