diff --git a/components/Requests/RequestComment.tsx b/components/Requests/RequestComment.tsx new file mode 100644 index 0000000..801f5a5 --- /dev/null +++ b/components/Requests/RequestComment.tsx @@ -0,0 +1,96 @@ +"use client"; +import React from "react"; +import { convertFullTime } from "@/lib/utils"; +import RequestViolation from "./RequestViolation"; +import { BsTrash3 } from "react-icons/bs"; +import ph from "@/assets/images/ph.png"; +import Image from "next/image"; +import { useMutation } from "@tanstack/react-query"; +import { deleteComment } from "@/utils/CitizenReport"; +import { toast } from "sonner"; + +interface RequestCommentProps { + id: string; + comment: string; + dateTime: string; + user: any; + canDelete: boolean; +} + +const getUserTitle = (user: any) => { + if (user.firstName || user.lastName) + return `${user.firstName || ""} ${user.lastName || ""}`; + else if (user.title) return user.title; + else return "شهروند"; +}; + +const RequestComment: React.FC = ({ + id, + comment, + dateTime, + user, + canDelete, +}) => { + const DeleteComment = useMutation({ + mutationKey: ["deleteComment"], + mutationFn: deleteComment, + onSuccess: (res) => { + toast(<>{res.message}, { + className: "!bg-green !text-black", + }); + }, + onError: (error: any | string) => { + toast(<>{error.message}, { + className: "!bg-red-500 !text-white", + }); + }, + }); + + const handelDelete = () => { + DeleteComment.mutate(id); + }; + + return ( +
+
+ picture +
+
+
+

+ {getUserTitle(user)} +

+ + {convertFullTime(dateTime)} + +
+ +
+

+ {comment} +

+ {canDelete && ( + + )} + +
+
+
+ ); +}; + +export default RequestComment; diff --git a/components/Requests/RequestComments.tsx b/components/Requests/RequestComments.tsx index 818131b..29bacf8 100644 --- a/components/Requests/RequestComments.tsx +++ b/components/Requests/RequestComments.tsx @@ -1,7 +1,7 @@ "use client"; -import { getComments } from "@/utils/CitizenReport"; -import { useQuery } from "@tanstack/react-query"; -import React, { useState } from "react"; +import { getComments, postComment } from "@/utils/CitizenReport"; +import { useMutation, useQuery } from "@tanstack/react-query"; +import React, { useEffect, useState } from "react"; import { FaRegComment } from "react-icons/fa"; import { Drawer, @@ -16,6 +16,9 @@ import { import { Button } from "../ui/button"; import { MdSend } from "react-icons/md"; import { Textarea } from "../ui/textarea"; +import { toast } from "sonner"; +import { Oval, ThreeDots } from "react-loader-spinner"; +import RequestComment from "./RequestComment"; // Define types for props interface RequestCommentsProps { @@ -23,47 +26,69 @@ interface RequestCommentsProps { commentsCount: number; } +// Define types for comments +interface Comment { + id: string; + text: string; + dateTime: string; + user:any; + canDelete:boolean; +} + +// Define the post comment function type +type PostCommentArgs = { + id: string; + comment: string; +}; + const RequestComments: React.FC = ({ reportId, commentsCount, }) => { // Component state + const [comments, setComments] = useState([]); const [commentsCounts, setCommentsCounts] = useState(commentsCount); - const [comment, setComment] = useState(""); + const [commentText, setCommentText] = useState(""); const { data, isLoading, isError, isSuccess, refetch } = useQuery({ - queryKey: ["Comments"], + queryKey: ["Comments", reportId], queryFn: () => getComments(reportId), // refetchOnWindowFocus: false, }); - const handleSubmit = async (event: React.FormEvent) => { - event.preventDefault(); - - const requestBody = { - comment: comment, - }; - - try { - const response = await fetch(`/api/comments/${reportId}`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify(requestBody), - }); - - if (!response.ok) { - throw new Error("Network response was not ok"); - } - - // Handle success (e.g., clear the input, show a success message, etc.) - setComment(""); - console.log("Comment submitted successfully!"); - } catch (error) { - // Handle error - console.error("There was an error submitting the comment:", error); + useEffect(() => { + console.log("Fetched data:", data); // Log the fetched data + if (data) { + setComments(data.data); + console.log("Comments state updated:", data); } + }, [data]); + + // Post Commnet + const mutation = useMutation({ + mutationFn: ({ id, comment }: PostCommentArgs) => postComment(id, comment), + onSuccess: () => { + // Refetch comments after a new one is added + refetch(); + // Reset the comment input field + setCommentText(""); + // Increment the comments count + setCommentsCounts((prevCount) => prevCount + 1); + toast("successfully", { + className: "!bg-green !text-black", + }); + }, + onError: (error) => { + console.error("Failed to post comment:", error); + toast(<>{error.message}, { + className: "!bg-red-500 !text-white", + }); + }, + }); + + const handleSubmit = (e: React.FormEvent) => { + e.preventDefault(); + mutation.mutate({ id: reportId, comment: commentText }); }; return ( @@ -73,25 +98,66 @@ const RequestComments: React.FC = ({ e.stopPropagation()}> - - -

- هنوز نظری برای این درخواست ثبت نشده است. -

+ + + {isLoading && ( +
+ +
+ )} + {isError &&

Failed to load comments.

} + {isSuccess && comments.length > 0 && commentsCounts > 0 ? ( +
+ {comments.map((comment) => ( + <> + {console.log(comment)} + + + ))} +
+ ) : ( + !isLoading &&

هنوز نظری برای این درخواست ثبت نشده است.

+ )}
-
+ +