From 130dcc44fd3e995bd57517171fb5fd30a808dc44 Mon Sep 17 00:00:00 2001 From: Anang Yusman Date: Sun, 12 Oct 2025 23:37:39 +0800 Subject: [PATCH] update --- components/details/details-content.tsx | 263 +++++++++++++++++-------- service/master-user.ts | 25 ++- 2 files changed, 199 insertions(+), 89 deletions(-) diff --git a/components/details/details-content.tsx b/components/details/details-content.tsx index 3f31e57..a0f2979 100644 --- a/components/details/details-content.tsx +++ b/components/details/details-content.tsx @@ -3,11 +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 Author from "../landing-page/author"; import { Link2, MailIcon } from "lucide-react"; import { getAdvertise } from "@/service/advertisement"; +import { + getArticleComment, + otpRequest, + otpValidation, + postArticleComment, +} from "@/service/master-user"; +import { useForm } from "react-hook-form"; +import { saveActivity } from "@/service/activity-log"; type TabKey = "trending" | "comments" | "latest"; @@ -47,6 +55,7 @@ export default function DetailContent() { const params = useParams(); const id = params?.id; const [page, setPage] = useState(1); + const pathname = usePathname(); const [totalPage, setTotalPage] = useState(1); const [articles, setArticles] = useState([]); const [articleDetail, setArticleDetail] = useState(null); @@ -72,7 +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" }, { id: "comments", label: "Comments" }, @@ -474,85 +543,121 @@ export default function DetailContent() { ditandai *

-
-
- -