import { Button } from "@heroui/button"; import { Input, Textarea } from "@heroui/input"; import React, { useEffect, useState } from "react"; import { Controller, useForm } from "react-hook-form"; import * as z from "zod"; import { zodResolver } from "@hookform/resolvers/zod"; import { deleteArticleComment, editArticleComment, getArticleComment, otpRequest, otpValidation, postArticleComment, } from "@/services/master-user"; import { error } from "@/config/swal"; import { UserProfileIcon } from "@/components/icons/globals"; import { convertDateFormat, getCookiesDecrypt } from "@/utils/global"; import Cookies from "js-cookie"; import OTPInput from "react-otp-input"; import Swal from "sweetalert2"; import withReactContent from "sweetalert2-react-content"; import { SendIcon, TimesIcon } from "@/components/icons"; import { saveActivity } from "@/services/activity-log"; import { usePathname } from "next/navigation"; const commentSchema = z.object({ name: z.string().min(1, { message: "Judul harus diisi", }), email: z .string() .email({ message: "Email tidak valid", }) .min(1, { message: "Harus diisi", }), comment: z.string().min(1, { message: "Deskripsi harus diisi", }), }); export default function Comment(props: { id: string | null }) { const { id } = props; const MySwal = withReactContent(Swal); const pathname = usePathname(); const [needOtp, setNeedOtp] = useState(false); const [otpValue, setOtpValue] = useState(""); const [commentList, setCommentList] = useState([]); const [openCommentId, setOpenCommentId] = useState(0); const [editCommentId, setEditCommentId] = useState(0); const [replyValue, setReplyValue] = useState(""); const [editValue, setEditValue] = useState(""); const [userId, setUserId] = useState(); const [token, setToken] = useState(); useEffect(() => { const userId = getCookiesDecrypt("uie"); const token = Cookies.get("access_token"); setUserId(userId); setToken(token); }, []); const formOptions = { resolver: zodResolver(commentSchema), }; type UserSettingSchema = z.infer; const { control, handleSubmit, formState: { errors }, setValue, reset, } = useForm(formOptions); useEffect(() => { fetchData(); }, [id]); const fetchData = async () => { const res = await getArticleComment(String(id)); setCommentList(res?.data?.data); }; const onSubmit = async (values: z.infer) => { if (!needOtp) { const res = await otpRequest(values.email, values?.name); if (res?.error) { error(res.message); return false; } setNeedOtp(true); } else { const validation = await otpValidation(values.email, otpValue); if (validation?.error) { error("OTP Tidak Sesuai"); return false; } const data = { commentFromName: values.name, commentFromEmail: values.email, articleId: Number(id), isPublic: false, message: values.comment, parentId: 0, }; const res = await postArticleComment(data); if (res?.error) { error(res?.message); return false; } const req: any = { activityTypeId: 5, url: "https://humas.polri.go.id/" + pathname, articleId: Number(id), }; const resActivity = await saveActivity(req); reset(); fetchData(); setNeedOtp(false); } }; const handleDelete = async (id: number) => { MySwal.fire({ title: "Delete Comment", text: "", icon: "warning", showCancelButton: true, cancelButtonColor: "#d33", confirmButtonColor: "#3085d6", confirmButtonText: "Simpan", }).then((result) => { if (result.isConfirmed) { doDelete(id); } }); }; const doDelete = async (id: number) => { const res = await deleteArticleComment(id); if (res?.error) { error(res?.message); return false; } MySwal.fire({ title: "Sukses", icon: "success", confirmButtonColor: "#3085d6", confirmButtonText: "OK", }).then((result) => { if (result.isConfirmed) { } }); fetchData(); }; const sendComment = async (idComment: number) => { const data = { articleId: Number(id), isPublic: true, message: replyValue, parentId: idComment, }; const res = await postArticleComment(data); if (res?.error) { error(res?.message); return false; } const req: any = { activityTypeId: 5, url: "https://humas.polri.go.id/" + pathname, articleId: Number(id), userId: Number(userId), }; const resActivity = await saveActivity(req, token); fetchData(); }; // const sendActivity = async () => {}; const editComment = async (idComment: number, parentId: number) => { const data = { articleId: Number(id), isPublic: true, id: idComment, message: editValue, parentId: parentId, }; const res = await editArticleComment(data, idComment); if (res?.error) { error(res?.message); return false; } setEditCommentId(0); fetchData(); }; const childComment = (parentId: number) => { const filteredComment = commentList.filter( (a: any) => a.parentId === parentId ); return filteredComment.length > 0 ? (
{filteredComment.map((list: any) => (

{list?.commentFromName}

{convertDateFormat(list?.updatedAt)}

{editCommentId === list?.id ? ( } labelPlacement="outside" className="w-full " classNames={{ inputWrapper: [ "border-1 rounded-lg", "dark:group-data-[focused=false]:bg-transparent !border-1 dark:!border-gray-400", ], }} variant="bordered" /> setEditCommentId(0)} >
) : (

{list?.message}

{userId === "16" && ( { setEditValue(list?.message); setEditCommentId(list?.id); }} > Edit )} {(userId === String(list?.commentFromId) || userId === "16") && ( handleDelete(list?.id)} > Hapus )}
)}
))} ) : ( "" ); }; return (
{!needOtp ? ( <> Tinggalkan balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *

Komentar

(