From f8d9513d6ad55d2cd9af881a23869a10a759ef82 Mon Sep 17 00:00:00 2001 From: YasamanKahdooei Date: Thu, 30 May 2024 15:00:41 +0330 Subject: [PATCH] Added Violation component and modifeid Comment Componnent and Delete Component thats all related to RequestComponent --- components/RecentRequest/RecentRequests.tsx | 2 +- components/Requests/RequestCard.tsx | 38 ++- components/Requests/RequestComment.tsx | 42 ++- components/Requests/RequestComments.tsx | 17 +- components/Requests/RequestViolations.tsx | 282 ++++++++++++++------ utils/CitizenReport.ts | 9 +- 6 files changed, 282 insertions(+), 108 deletions(-) diff --git a/components/RecentRequest/RecentRequests.tsx b/components/RecentRequest/RecentRequests.tsx index 8e5baa1..15942c4 100644 --- a/components/RecentRequest/RecentRequests.tsx +++ b/components/RecentRequest/RecentRequests.tsx @@ -46,7 +46,7 @@ function RecentRequests() {
{/* */} - + {/* */}
diff --git a/components/Requests/RequestCard.tsx b/components/Requests/RequestCard.tsx index d744eb5..ba936ab 100644 --- a/components/Requests/RequestCard.tsx +++ b/components/Requests/RequestCard.tsx @@ -15,6 +15,8 @@ import { convertFullTime } from "@/lib/utils"; import { useRouter } from "next/navigation"; import RequestViolations from "./RequestViolations"; +import { postReportViolation } from "@/utils/CitizenReport"; + // interface RequestCardProps { // isSelfRequest?: boolean; // } @@ -34,14 +36,24 @@ interface RequestItem { medias: Media[]; isLiked: boolean; likes: number; - commentsCount:number; -} + commentsCount: number; +} interface RequestCardProps { item: RequestItem; isSelfRequest?: boolean; } +// Define the post violation function type +type PostViolationArgs = { + id: string; + payload: { + instanceId: number; + violationTypeId: number; + description: string; + }; +}; + const RequestCard: React.FC = ({ item, isSelfRequest }) => { const router = useRouter(); @@ -50,6 +62,15 @@ const RequestCard: React.FC = ({ item, isSelfRequest }) => { router.push(`/User/MyRequest/${item.id}`); } }; + + // Correctly wrapped function + const wrappedPostReportViolation = async ({ + id, + payload, + }: PostViolationArgs) => { + return postReportViolation(id, payload); + }; + return ( //
<> @@ -105,13 +126,22 @@ const RequestCard: React.FC = ({ item, isSelfRequest }) => {
- + {/* */} + + + - +
diff --git a/components/Requests/RequestComment.tsx b/components/Requests/RequestComment.tsx index 9946848..af5eea0 100644 --- a/components/Requests/RequestComment.tsx +++ b/components/Requests/RequestComment.tsx @@ -5,9 +5,10 @@ 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 { useMutation, useQueryClient } from "@tanstack/react-query"; +import { deleteComment, postCommentViolation } from "@/utils/CitizenReport"; import { toast } from "sonner"; +import RequestViolations from "./RequestViolations"; interface RequestCommentProps { id: string; @@ -15,9 +16,21 @@ interface RequestCommentProps { dateTime: string; user: any; canDelete: boolean; - onDeleteCommentSuccess:any + onDeleteCommentSuccess: any; + commentsCount: () => void; } +// Define the post violation function type +type PostViolationArgs = { + id: string; + payload: { + instanceId: number; + violationTypeId: number; + description: string; + }; +}; + + const getUserTitle = (user: any) => { if (user.firstName || user.lastName) return `${user.firstName || ""} ${user.lastName || ""}`; @@ -32,14 +45,18 @@ const RequestComment: React.FC = ({ user, canDelete, onDeleteCommentSuccess, + commentsCount, }) => { + const queryClient = useQueryClient(); const DeleteComment = useMutation({ mutationKey: ["deleteComment"], mutationFn: deleteComment, onSuccess: (res) => { + queryClient.invalidateQueries({ queryKey: ["Comments"] }); + commentsCount(); toast(<>{res.message}, { - className: "!bg-green !text-black", + className: "!bg-green !text-black flex justify-end", }); }, onError: (error: any | string) => { @@ -53,6 +70,13 @@ const RequestComment: React.FC = ({ const handelDelete = () => { DeleteComment.mutate(id); }; + // Correctly wrapped function + const wrappedPostCommentViolation = async ({ + id, + payload, + }: PostViolationArgs) => { + return postCommentViolation(id, payload); + }; return (
@@ -88,9 +112,15 @@ const RequestComment: React.FC = ({ {comment}

{canDelete && ( - + )} - +
diff --git a/components/Requests/RequestComments.tsx b/components/Requests/RequestComments.tsx index ebf34d2..f87a82d 100644 --- a/components/Requests/RequestComments.tsx +++ b/components/Requests/RequestComments.tsx @@ -31,9 +31,9 @@ interface Comment { id: string; text: string; dateTime: string; - user:any; - canDelete:boolean; - onDeleteCommentSuccess:any + user: any; + canDelete: boolean; + onDeleteCommentSuccess: any; } // Define the post comment function type @@ -75,8 +75,8 @@ const RequestComments: React.FC = ({ setCommentText(""); // Increment the comments count setCommentsCounts((prevCount) => prevCount + 1); - toast("successfully", { - className: "!bg-green !text-black", + toast("نظر شما با موفقیت ثبت شد", { + className: "!bg-green !text-black flex justify-end", }); }, onError: (error) => { @@ -126,9 +126,12 @@ const RequestComments: React.FC = ({ id={comment.id} comment={comment.text} dateTime={comment.dateTime} - user={comment.user} + user={comment.user} canDelete={comment.canDelete} onDeleteCommentSuccess={refetch()} + commentsCount={() => + setCommentsCounts((prev) => prev - 1) + } /> ))} @@ -151,7 +154,7 @@ const RequestComments: React.FC = ({ wrapperClass="" /> ) : ( - + )}