diff --git a/components/details/details-content.tsx b/components/details/details-content.tsx
index c578d27..7eddee8 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 { saveActivity } from "@/service/activity-log";
+import { useForm } from "react-hook-form";
type TabKey = "trending" | "comments" | "latest";
@@ -46,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([]);
@@ -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" },
@@ -473,85 +542,121 @@ export default function DetailContent() {
ditandai *
-
diff --git a/service/master-user.ts b/service/master-user.ts
index 41bcf3d..af0f6ca 100644
--- a/service/master-user.ts
+++ b/service/master-user.ts
@@ -94,16 +94,21 @@ export async function otpValidation(email: string, otpCode: string) {
return await httpPost(pathUrl, { email, otpCode });
}
+// export async function postArticleComment(data: any) {
+// const headers = token
+// ? {
+// "content-type": "application/json",
+// Authorization: `Bearer ${token}`,
+// }
+// : {
+// "content-type": "application/json",
+// };
+// return await httpPost(`/article-comments`, headers, data);
+// }
+
export async function postArticleComment(data: any) {
- const headers = token
- ? {
- "content-type": "application/json",
- Authorization: `Bearer ${token}`,
- }
- : {
- "content-type": "application/json",
- };
- return await httpPost(`/article-comments`, headers, data);
+ const pathUrl = `/article-comments`;
+ return await httpPostInterceptor(pathUrl, data);
}
export async function editArticleComment(data: any, id: number) {
@@ -112,7 +117,7 @@ export async function editArticleComment(data: any, id: number) {
}
export async function getArticleComment(id: string) {
- const pathUrl = `/article-comments?isPublic=true&articleId=${id}`;
+ const pathUrl = `/article-comments?isPublic=false&articleId=${id}`;
return await httpGet(pathUrl);
}