shahrbin-citizen/components/Requests/RequestComment.tsx

130 lines
3.4 KiB
TypeScript

"use client";
import React from "react";
import { convertFullTime } from "@/lib/utils";
import { BsTrash3 } from "react-icons/bs";
import ph from "@/assets/images/ph.png";
import Image from "next/image";
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;
comment: string;
dateTime: string;
user: any;
canDelete: boolean;
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 || ""}`;
else if (user.title) return user.title;
else return "شهروند";
};
const RequestComment: React.FC<RequestCommentProps> = ({
id,
comment,
dateTime,
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 flex justify-end",
});
},
onError: (error: any | string) => {
toast(<>{error.message}</>, {
className: "!bg-red-500 !text-white",
});
onDeleteCommentSuccess();
},
});
const handelDelete = () => {
DeleteComment.mutate(id);
};
// Correctly wrapped function
const wrappedPostCommentViolation = async ({
id,
payload,
}: PostViolationArgs) => {
return postCommentViolation(id, payload);
};
return (
<div className="flex w-full items-stretch mb-3 p-2 border-b-[1px] border-b-gray-200 last:border-b-0">
<div className="w-12 h-12 ml-2">
<Image
className="object-cover rounded-full w-full h-full"
alt="picture"
quality={100}
height={50}
width={50}
src={
user?.avatar?.url3
? `${process.env.NEXT_PUBLIC_BASE_URL_APP}${user?.avatar?.url3}`
: ph
}
/>
</div>
<div className="flex flex-1 flex-col">
<div className="flex w-full items-center justify-between">
<p className="text-xs font-bold text-gray-500">
{getUserTitle(user)}
</p>
<span className="text-xs font-bold text-gray-500">
{convertFullTime(dateTime)}
</span>
</div>
<div className="flex flex-1 items-center justify-between">
<p
className="flex flex-1 text-justify text-sm text-black"
style={{ lineBreak: "anywhere" }}
>
{comment}
</p>
{canDelete && (
<BsTrash3
className="text-red cursor-pointer"
onClick={handelDelete}
/>
)}
<RequestViolations
reportId={id}
mutationFn={wrappedPostCommentViolation}
/>
</div>
</div>
</div>
);
};
export default RequestComment;