11import React , { useState } from "react" ;
22import RatingComponent from "./RatingComponent.jsx" ;
3- import { model } from "../../model.js" ; // Adjust the path if needed
4- import { addReviewForCourse } from "../../firebase" ; // Adjust the path if needed
3+ import { model } from "../../model.js" ;
4+ import { addReviewForCourse , deleteReviewById } from "../../firebase" ; // we will add deleteReviewById
55
66function CommentTree ( { courseCode, comment, level = 0 } ) {
77 const [ showReply , setShowReply ] = useState ( false ) ;
88 const [ replyText , setReplyText ] = useState ( "" ) ;
99
10+ const currentUserId = model . user ?. uid ;
11+
1012 const handleReplySubmit = async ( ) => {
1113 if ( replyText . trim ( ) . length === 0 ) return ;
1214
1315 const reply = {
1416 userName : model . user ?. displayName || "Anonymous" ,
17+ userId : model . user ?. uid || "anonymous" ,
1518 text : replyText ,
1619 timestamp : Date . now ( ) ,
1720 overallRating : 0 ,
@@ -23,7 +26,14 @@ function CommentTree({ courseCode, comment, level = 0 }) {
2326 } ;
2427
2528 await addReviewForCourse ( courseCode , reply , comment . id ) ;
26- window . location . reload ( ) ; // quick reload for now; optional optimization later
29+ window . location . reload ( ) ; // quick reload for now
30+ } ;
31+
32+ const handleDeleteComment = async ( ) => {
33+ if ( ! window . confirm ( "Are you sure you want to delete this comment?" ) ) return ;
34+
35+ await deleteReviewById ( courseCode , comment . id , comment . parentId || null ) ;
36+ window . location . reload ( ) ; // quick reload
2737 } ;
2838
2939 return (
@@ -38,12 +48,24 @@ function CommentTree({ courseCode, comment, level = 0 }) {
3848
3949 < p className = "text-sm text-gray-700 mb-1" > { comment . text } </ p >
4050
41- < button
42- className = "text-blue-500 text-sm hover:underline"
43- onClick = { ( ) => setShowReply ( ! showReply ) }
44- >
45- { showReply ? "Cancel" : "Reply" }
46- </ button >
51+ < div className = "flex gap-3 items-center" >
52+ < button
53+ className = "text-blue-500 text-sm hover:underline"
54+ onClick = { ( ) => setShowReply ( ! showReply ) }
55+ >
56+ { showReply ? "Cancel" : "Reply" }
57+ </ button >
58+
59+ { /* Show delete button only if current user is the comment author */ }
60+ { currentUserId && comment . userId === currentUserId && (
61+ < button
62+ className = "text-red-500 text-sm hover:underline"
63+ onClick = { handleDeleteComment }
64+ >
65+ Delete
66+ </ button >
67+ ) }
68+ </ div >
4769
4870 { showReply && (
4971 < div className = "mt-2" >
@@ -63,7 +85,6 @@ function CommentTree({ courseCode, comment, level = 0 }) {
6385 ) }
6486 </ div >
6587
66- { /* Recursive rendering of replies */ }
6788 { comment . replies && comment . replies . length > 0 && (
6889 < div className = "mt-2 space-y-2" >
6990 { comment . replies . map ( ( child ) => (
0 commit comments