@@ -8,6 +8,7 @@ interface ReplyCommentPayload {
88 comment_post_ID : number ;
99 comment_parent : number ;
1010 comment : string ;
11+ reply_context ?: 'list' | 'single' ;
1112}
1213
1314interface DeleteCommentPayload {
@@ -25,6 +26,7 @@ interface ReplyQnAPayload {
2526 course_id : number ;
2627 question_id : number ;
2728 answer : string ;
29+ reply_context ?: 'list' | 'single' ;
2830}
2931interface UpdateQnAPayload {
3032 question_id : number ;
@@ -36,6 +38,8 @@ interface DeleteQnAPayload {
3638 context ?: 'question' | 'reply' ;
3739}
3840
41+ type DiscussionCardType = 'qna' | 'comment' ;
42+
3943const FORM_ID_PREFIXES = {
4044 COMMENT_EDIT : 'lesson-comment-edit-' ,
4145 COMMENT_REPLY : 'lesson-comment-reply-form-' ,
@@ -87,6 +91,8 @@ const discussionsPage = () => {
8791 isArchived : false ,
8892 editingId : null as number | null ,
8993 editingFormId : null as string | null ,
94+ replyingId : null as number | null ,
95+ replyingCommentId : null as number | null ,
9096 loadingReplies : false ,
9197 repliesOrder : 'DESC' ,
9298 $nextTick : undefined as ( ( callback : ( ) => void ) => void ) | undefined ,
@@ -118,12 +124,19 @@ const discussionsPage = () => {
118124 this . replyCommentMutation = this . query . useMutation ( this . replyComment , {
119125 onSuccess : ( _ , payload ) => {
120126 toast . success ( __ ( 'Reply saved successfully' , 'tutor' ) ) ;
121- this . reloadReplies ( ) ;
122127
123128 const formId = `${ FORM_ID_PREFIXES . COMMENT_REPLY } ${ payload . comment_parent } ` ;
124129 if ( form . hasForm ( formId ) ) {
125130 form . reset ( formId ) ;
126131 }
132+
133+ if ( payload . reply_context === 'single' ) {
134+ this . reloadReplies ( ) ;
135+ } else {
136+ this . setReplyingComment ( null ) ;
137+ this . updateCommentReplyCount ( payload . comment_parent ) ;
138+ this . highlightCard ( payload . comment_parent , 'comment' ) ;
139+ }
127140 } ,
128141 onError : ( error : Error ) => {
129142 toast . error ( convertToErrorMessage ( error ) ) ;
@@ -197,11 +210,19 @@ const discussionsPage = () => {
197210 this . replyQnAMutation = this . query . useMutation ( this . replyQnA , {
198211 onSuccess : ( _ , payload ) => {
199212 toast . success ( __ ( 'Reply saved successfully' , 'tutor' ) ) ;
200- this . reloadReplies ( ) ;
213+
201214 const formId = `${ FORM_ID_PREFIXES . QNA_REPLY } ${ payload . question_id } ` ;
202215 if ( form . hasForm ( formId ) ) {
203216 form . reset ( formId ) ;
204217 }
218+
219+ if ( payload . reply_context === 'single' ) {
220+ this . reloadReplies ( ) ;
221+ } else {
222+ this . setReplying ( null ) ;
223+ this . updateReplyCount ( payload . question_id ) ;
224+ this . highlightCard ( payload . question_id ) ;
225+ }
205226 } ,
206227 onError : ( error : Error ) => {
207228 toast . error ( convertToErrorMessage ( error ) ) ;
@@ -308,11 +329,17 @@ const discussionsPage = () => {
308329 }
309330 } ,
310331
311- handleReplyComment ( data : { comment : string } , commentId : number , courseId : number ) {
332+ handleReplyComment (
333+ data : { comment : string } ,
334+ commentId : number ,
335+ courseId : number ,
336+ context : ReplyCommentPayload [ 'reply_context' ] = 'single' ,
337+ ) {
312338 return this . replyCommentMutation ?. mutate ( {
313339 comment : data . comment ,
314340 comment_parent : commentId ,
315341 comment_post_ID : courseId ,
342+ reply_context : context ,
316343 } ) ;
317344 } ,
318345
@@ -345,6 +372,86 @@ const discussionsPage = () => {
345372 } ) ;
346373 }
347374 } ,
375+
376+ setReplying ( id : number | null ) {
377+ this . replyingId = id ;
378+
379+ if ( id ) {
380+ const formId = `${ FORM_ID_PREFIXES . QNA_REPLY } ${ id } ` ;
381+ this . $nextTick ?.( ( ) => {
382+ if ( form . hasForm ( formId ) ) {
383+ form . setFocus ( formId , 'answer' ) ;
384+ }
385+ } ) ;
386+ }
387+ } ,
388+
389+ toggleReply ( id : number ) {
390+ if ( this . replyingId === id ) {
391+ this . setReplying ( null ) ;
392+ } else {
393+ this . setReplying ( id ) ;
394+ }
395+ } ,
396+
397+ updateReplyCount ( questionId : number ) {
398+ // Find the reply count element and increment it
399+ const card = document . querySelector ( `[data-question-id="${ questionId } "]` ) ;
400+ if ( card ) {
401+ const countElement = card . querySelector ( '.tutor-discussion-card-reply-count' ) ;
402+ if ( countElement ) {
403+ const currentCount = parseInt ( countElement . textContent || '0' , 10 ) ;
404+ countElement . textContent = String ( currentCount + 1 ) ;
405+ }
406+ }
407+ } ,
408+
409+ setReplyingComment ( id : number | null ) {
410+ this . replyingCommentId = id ;
411+
412+ if ( id ) {
413+ const formId = `${ FORM_ID_PREFIXES . COMMENT_REPLY } ${ id } ` ;
414+ this . $nextTick ?.( ( ) => {
415+ if ( form . hasForm ( formId ) ) {
416+ form . setFocus ( formId , 'comment' ) ;
417+ }
418+ } ) ;
419+ }
420+ } ,
421+
422+ toggleCommentReply ( id : number ) {
423+ if ( this . replyingCommentId === id ) {
424+ this . setReplyingComment ( null ) ;
425+ } else {
426+ this . setReplyingComment ( id ) ;
427+ }
428+ } ,
429+
430+ updateCommentReplyCount ( commentId : number ) {
431+ // Find the reply count element and increment it
432+ const card = document . querySelector ( `[data-comment-id="${ commentId } "]` ) ;
433+ if ( card ) {
434+ const countElement = card . querySelector ( '.tutor-discussion-card-reply-count' ) ;
435+ if ( countElement ) {
436+ const currentCount = parseInt ( countElement . textContent || '0' , 10 ) ;
437+ countElement . textContent = String ( currentCount + 1 ) ;
438+ }
439+ }
440+ } ,
441+
442+ highlightCard ( id : number , type : DiscussionCardType = 'qna' ) {
443+ const attr = type === 'comment' ? 'data-comment-id' : 'data-question-id' ;
444+ const el = document . querySelector ( `[${ attr } ="${ id } "]` ) ;
445+ const card = ( el ?. closest ( '.tutor-discussion-card' ) ?? el ) as HTMLElement | null ;
446+ if ( ! card ) return ;
447+
448+ if ( window . innerWidth > 576 ) {
449+ card . style . boxShadow = '0 0 0 2px var(--tutor-text-brand-secondary)' ;
450+ setTimeout ( ( ) => {
451+ card . style . boxShadow = '' ;
452+ } , 300 ) ;
453+ }
454+ } ,
348455 } ;
349456} ;
350457
0 commit comments