From 5f981d35428b3dca2318c99206ffea35b96cc9ad Mon Sep 17 00:00:00 2001 From: Anang Yusman Date: Mon, 13 Oct 2025 11:42:27 +0800 Subject: [PATCH] update --- components/details/details-content.tsx | 265 +++++++++++++++++-------- service/master-user.ts | 25 ++- 2 files changed, 201 insertions(+), 89 deletions(-) diff --git a/components/details/details-content.tsx b/components/details/details-content.tsx index b070192..9cee60c 100644 --- a/components/details/details-content.tsx +++ b/components/details/details-content.tsx @@ -3,10 +3,19 @@ import Image from "next/image"; import { useEffect, useState } from "react"; import Link from "next/link"; import { getArticleById, getListArticle } from "@/service/article"; -import { close, loading } from "@/config/swal"; -import { useParams } from "next/navigation"; +import { close, error, loading } from "@/config/swal"; +import { useParams, usePathname } from "next/navigation"; import { Link2, MailIcon } from "lucide-react"; import { getAdvertise } from "@/service/advertisement"; +import { register } from "module"; +import { + getArticleComment, + otpRequest, + otpValidation, + postArticleComment, +} from "@/service/master-user"; +import { saveActivity } from "@/service/activity-log"; +import { useForm } from "react-hook-form"; type TabKey = "trending" | "comments" | "latest"; @@ -45,6 +54,7 @@ type Advertise = { export default function DetailContent() { const params = useParams(); const id = params?.id; + const pathname = usePathname(); const [page, setPage] = useState(1); const [totalPage, setTotalPage] = useState(1); const [articles, setArticles] = useState([]); @@ -71,6 +81,67 @@ export default function DetailContent() { const [tabArticles, setTabArticles] = useState([]); const [activeTab, setActiveTab] = useState("trending"); + const [needOtp, setNeedOtp] = useState(false); + const [otpValue, setOtpValue] = useState(""); + const { register, handleSubmit, reset, watch } = useForm(); + const [commentList, setCommentList] = useState([]); + + useEffect(() => { + fetchData(); + }, [id]); + + const fetchData = async () => { + try { + const res = await getArticleComment(String(id)); + const data = res?.data?.data; + setCommentList(data); + console.log("komen", data); + } catch (err) { + console.error("❌ Gagal memuat komentar:", err); + setCommentList([]); + } + }; + + const onSubmit = async (values: any) => { + 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://dev.mikulnews/" + pathname, + articleId: Number(id), + }; + + const resActivity = await saveActivity(req); + reset(); + fetchData(); + setNeedOtp(false); + } + }; const tabs: { id: TabKey; label: string }[] = [ { id: "trending", label: "Trending" }, @@ -473,85 +544,121 @@ export default function DetailContent() { ditandai *

-
-
- -