Skip to content

Commit fb8f206

Browse files
committed
refactor: use same code for ticket comment threads
ref: #112
1 parent f83f3c5 commit fb8f206

File tree

3 files changed

+41
-62
lines changed

3 files changed

+41
-62
lines changed

src/components/page/ticket/TicketComment.jsx

Lines changed: 23 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,18 @@ import IconLoader from "../../IconLoader"
88
import { secondsToTime } from "../../../layout/Ticket"
99
import Section from "../../Section"
1010
import TicketCommentForm from "./TicketCommentForm"
11+
import TicketComments from "./TicketComments"
1112

1213

1314

1415
const TicketComment = ({
15-
discussion = false,
1616
comment_data = {},
1717
metadata = null,
1818
ticket_id = null,
1919
post_url,
2020
edit_callback = null,
21-
callback_value = null
21+
callback_value = null,
22+
parent_comment = null,
2223
}) => {
2324

2425
if( String(post_url).includes('?') ) {
@@ -30,7 +31,7 @@ const TicketComment = ({
3031

3132
const [ comment_page_data, setCommentPageData ] = useState( comment_data )
3233

33-
const [ reload, setReload ] = useState(false)
34+
const [ start_thread, setStartThread ] = useState( false )
3435

3536
let comment_header = ' wrote'
3637

@@ -114,20 +115,21 @@ const TicketComment = ({
114115

115116
useEffect(() => {
116117

117-
if( comment_page_data ) {
118+
if( comment_page_data._urls.threads && ! parent_comment) {
118119
apiFetch(
119-
`${post_url + '/threads?page[size]=500'}`,
120+
`${comment_page_data._urls.threads}?page[size]=500`,
120121
(data) => {
121122
setThreads(data)
122123
},
123124
undefined,
124125
undefined,
125126
false
126127
)
127-
128-
setReload(false)
129128
}
130-
},[ reload ])
129+
},[
130+
comment_page_data._urls.threads,
131+
parent_comment,
132+
])
131133

132134

133135

@@ -141,7 +143,7 @@ const TicketComment = ({
141143
)
142144
.then((result) => {
143145

144-
if( result.status == 200 ) {
146+
if( result.status === 200 ) {
145147

146148
if( result.api_metadata !== null ) {
147149

@@ -193,9 +195,9 @@ const TicketComment = ({
193195
<span style={{
194196
cursor: 'pointer'
195197
}} onClick={(e) => {
196-
setThreads( {
197-
results: []
198-
} )
198+
if(threads.results.length === 0) {
199+
setStartThread( (start_thread ? false : true) )
200+
}
199201
}}>
200202
<IconLoader
201203
name={'reply'}
@@ -242,6 +244,7 @@ const TicketComment = ({
242244
comment_data={comment_page_data}
243245
metadata={comment_metadata}
244246
post_url = {post_url}
247+
parent_id={parent_comment}
245248
ticket_id={ticket_id}
246249
is_edit = {true}
247250
cancelbuttonOnSubmit={(e) => {
@@ -265,7 +268,6 @@ const TicketComment = ({
265268
</h4>
266269
)}
267270
>
268-
269271
<div className="comment row">
270272

271273
{ comment_page_data.source &&
@@ -391,7 +393,8 @@ const TicketComment = ({
391393

392394
</Section>
393395
}
394-
{ threads?.results[0]?.parent &&
396+
397+
{ ((threads ? (threads.results.length > 0) : false) || start_thread ) &&
395398
<div
396399
className="replies"
397400
style={{
@@ -411,46 +414,12 @@ const TicketComment = ({
411414
width = '20px'
412415
/>
413416
</h3>
414-
<ul
415-
className="replies"
416-
style={{
417-
paddingLeft: '1.6rem'
418-
}}
419-
>
420-
{ threads.results &&
421-
threads.results.map((comment, index) => (
422-
<li
423-
className="replies"
424-
id={comment.id}
425-
key={'comment-reply-' + comment.id}
426-
>
427-
<TicketComment
428-
comment_data={comment}
429-
discussion = {true}
430-
metadata = {comment_metadata}
431-
edit_callback = {() => {
432-
setReload(true)
433-
}}
434-
/>
435-
</li>
436-
))}
437-
<li
438-
className="replies"
439-
key={'comment-reply-form-' + comment_page_data.id}
440-
>
441-
<TicketCommentForm
442-
metadata={comment_metadata}
443-
post_url = {`${post_url}/threads`}
444-
ticket_id={ticket_id}
445-
parent_id = {comment_page_data['id'] ? comment_page_data['id'] : threads.results[0].parent}
446-
commentCallback={(response) => {
447-
448-
setCommentPageData(response.api_page_data);
449-
450-
}}
451-
/>
452-
</li>
453-
</ul>
417+
<TicketComments
418+
comment_metadata = {metadata}
419+
comments_url = {comment_page_data._urls.threads}
420+
ticket_id = {ticket_id}
421+
parent_comment={comment_page_data.id}
422+
/>
454423
</div>}
455424
</div>
456425
);

src/components/page/ticket/TicketCommentForm.jsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ const TicketCommentForm = ({
4343
the view serializer.
4444
*/
4545
const [form_data, setFormData] = useState({
46-
['comment_type']: comment_type}
47-
)
46+
['comment_type']: comment_type
47+
})
4848

4949
const user = useContext(UserContext)
5050

@@ -167,9 +167,11 @@ const TicketCommentForm = ({
167167
e.target.reset();
168168

169169
// Reset the form
170-
setFormData({});
171-
setCommentType('comment')
172170
setCommentMetadata(metadata)
171+
setCommentType('comment')
172+
setFormData({
173+
['comment_type']: 'comment'
174+
});
173175
}
174176

175177
}}

src/components/page/ticket/TicketComments.jsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import { MARKDOWN_TICKET_LINK_UNESCAPE_RE } from "../../../functions/markdown_pl
1313
const TicketComments = ({
1414
comment_metadata,
1515
ticket_id,
16-
comments_url
16+
comments_url,
17+
parent_comment = null,
1718
}) => {
1819

1920
const [comments, setComments] = useState({
@@ -67,7 +68,11 @@ const TicketComments = ({
6768
do_fetch();
6869

6970

70-
}, [reload, comments_url])
71+
}, [
72+
comments.fetch_url,
73+
comments_url,
74+
reload,
75+
])
7176

7277

7378
return (
@@ -100,7 +105,7 @@ const TicketComments = ({
100105
:
101106
comments.comments[key]['_urls']['_self'];
102107

103-
if( url == comment_metadata.urls.self ) {
108+
if( url === comment_metadata.urls.self ) {
104109
needs_metadata = false
105110

106111
}
@@ -143,6 +148,8 @@ const TicketComments = ({
143148
ticket_id={ticket_id}
144149
edit_callback = {setRelaod}
145150
callback_value = {reload}
151+
comments_url = {comments_url}
152+
parent_comment={parent_comment}
146153
/>
147154
</li>
148155
)
@@ -153,7 +160,8 @@ const TicketComments = ({
153160
>
154161
<TicketCommentForm
155162
metadata={comment_metadata}
156-
post_url = {comments_url.replace('/comment', '') }
163+
post_url = {comment_metadata.urls.self.replace('/comment', '') }
164+
parent_id={parent_comment}
157165
ticket_id={ticket_id}
158166
commentCallback={() => {
159167
setRelaod(reload ? false : true )

0 commit comments

Comments
 (0)