From c9f766314c9ddbf95a31cb17bc71460603609820 Mon Sep 17 00:00:00 2001 From: Bagasbanuna02 Date: Tue, 18 Feb 2025 10:54:46 +0800 Subject: [PATCH 1/7] fix forum deskripsi: - fix api forumku --- .gitignore | 10 +- src/app/api/forum/forumku/[id]/route.ts | 3 - src/app/dev/forum/forumku/[id]/page.tsx | 34 +-- .../forumku_component/forumku_header.tsx | 15 +- .../forumku_component/forumku_more_button.tsx | 243 ++++++++++-------- src/app_modules/forum/forumku/index.tsx | 34 +-- 6 files changed, 154 insertions(+), 185 deletions(-) diff --git a/.gitignore b/.gitignore index 11b0beac..852b9549 100644 --- a/.gitignore +++ b/.gitignore @@ -38,11 +38,11 @@ yarn-error.log* # logs logs/ -**/logs/ -*.log -**/*.log -log/ -**/log/ +# **/logs/ +# *.log +# **/*.log +# log/ +# **/log/ # typescript *.tsbuildinfo diff --git a/src/app/api/forum/forumku/[id]/route.ts b/src/app/api/forum/forumku/[id]/route.ts index 4c430580..4176c517 100644 --- a/src/app/api/forum/forumku/[id]/route.ts +++ b/src/app/api/forum/forumku/[id]/route.ts @@ -11,9 +11,6 @@ async function GET(request: Request, { params }: { params: { id: string } }) { const takeData = 5; const skipData = Number(page) * takeData - takeData; - console.log("id", id) - console.log("page >", page) - if (!page) { fixData = await prisma.forum_Posting.findMany({ orderBy: { diff --git a/src/app/dev/forum/forumku/[id]/page.tsx b/src/app/dev/forum/forumku/[id]/page.tsx index 81cdd338..16578b31 100644 --- a/src/app/dev/forum/forumku/[id]/page.tsx +++ b/src/app/dev/forum/forumku/[id]/page.tsx @@ -1,42 +1,12 @@ import { funGetUserIdByToken } from "@/app_modules/_global/fun/get"; import { Forum_Forumku } from "@/app_modules/forum"; -import { forum_countPostingByAuthorId } from "@/app_modules/forum/fun/count/count_posting_by_author_id"; -import { forum_getAllPostingByAuhtorId } from "@/app_modules/forum/fun/get/get_list_posting_by_author_id"; -import { user_getOneByUserId } from "@/app_modules/home/fun/get/get_one_user_by_id"; -import _ from "lodash"; -export default async function Page({ params }: { params: { id: string } }) { - const authorId = params.id; +export default async function Page() { const userLoginId = await funGetUserIdByToken(); - const dataAuthor = await user_getOneByUserId(authorId); - const auhtorSelectedData = _.omit(dataAuthor, [ - "Profile.email", - "Profile.alamat", - "Profile.jenisKelamin", - "Profile.createdAt", - "Profile.updatedAt", - "Profile.imagesBackgroundId", - ]); - // console.log(dataAuthor) - // console.log(auhtorSelectedData) - - // await new Promise((a, b) => { - // setTimeout(a, 1000); - // }); - - const dataPosting = await forum_getAllPostingByAuhtorId({ - authorId: authorId, - page: 1, - }); - const totalPosting = await forum_countPostingByAuthorId(authorId); - return ( <> - + ); } diff --git a/src/app_modules/forum/component/forumku_component/forumku_header.tsx b/src/app_modules/forum/component/forumku_component/forumku_header.tsx index cceeb246..e656b8c9 100644 --- a/src/app_modules/forum/component/forumku_component/forumku_header.tsx +++ b/src/app_modules/forum/component/forumku_component/forumku_header.tsx @@ -1,16 +1,13 @@ "use client"; -import { RouterForum } from "@/lib/router_hipmi/router_forum"; -import { RouterProfile } from "@/lib/router_hipmi/router_katalog"; +import { ComponentGlobal_LoaderAvatar } from "@/app_modules/_global/component"; import { ComponentGlobal_NotifikasiPeringatan } from "@/app_modules/_global/notif_global/notifikasi_peringatan"; -import { Avatar, Badge, Grid, Group, Loader, Stack, Text } from "@mantine/core"; +import { RouterForum } from "@/lib/router_hipmi/router_forum"; +import { Badge, Grid, Group, Stack, Text } from "@mantine/core"; import { IconCircle } from "@tabler/icons-react"; import { useRouter } from "next/navigation"; -import { useState } from "react"; import { MODEL_FORUM_POSTING } from "../../model/interface"; import ComponentForum_ForumkuMoreButton from "./forumku_more_button"; -import ComponentGlobal_Loader from "@/app_modules/_global/component/loader"; -import { ComponentGlobal_LoaderAvatar } from "@/app_modules/_global/component"; export default function ComponentForum_ForumkuHeaderCard({ data, @@ -52,7 +49,7 @@ export default function ComponentForum_ForumkuHeaderCard({ {data.Author.username - ? data.Author.username + ? data.Author.Profile.name : "Nama author "} @@ -68,9 +65,7 @@ export default function ComponentForum_ForumkuHeaderCard({ : "red" } > - - {data?.ForumMaster_StatusPosting.status} - + {data?.ForumMaster_StatusPosting.status} diff --git a/src/app_modules/forum/component/forumku_component/forumku_more_button.tsx b/src/app_modules/forum/component/forumku_component/forumku_more_button.tsx index ce6d46d6..8e1bcccd 100644 --- a/src/app_modules/forum/component/forumku_component/forumku_more_button.tsx +++ b/src/app_modules/forum/component/forumku_component/forumku_more_button.tsx @@ -1,7 +1,7 @@ "use client"; -import { RouterForum } from "@/lib/router_hipmi/router_forum"; import { ComponentGlobal_NotifikasiBerhasil } from "@/app_modules/_global/notif_global/notifikasi_berhasil"; +import { RouterForum } from "@/lib/router_hipmi/router_forum"; import { ActionIcon, Button, @@ -26,16 +26,17 @@ import { import { useRouter } from "next/navigation"; import { useState } from "react"; +import { + AccentColor, + MainColor, +} from "@/app_modules/_global/color/color_pallet"; import { ComponentGlobal_NotifikasiGagal } from "@/app_modules/_global/notif_global/notifikasi_gagal"; +import { clientLogger } from "@/util/clientLogger"; import mqtt_client from "@/util/mqtt_client"; import _ from "lodash"; import { forum_funDeletePostingById } from "../../fun/delete/fun_delete_posting_by_id"; import { forum_funEditStatusPostingById } from "../../fun/edit/fun_edit_status_posting_by_id"; import { MODEL_FORUM_POSTING } from "../../model/interface"; -import { - AccentColor, - MainColor, -} from "@/app_modules/_global/color/color_pallet"; export default function ComponentForum_ForumkuMoreButton({ authorId, @@ -254,11 +255,12 @@ function ButtonDelete({ const [loading, setLoading] = useState(false); async function onDelete() { - setOpenDel(false); - await forum_funDeletePostingById(postingId as any).then(async (res) => { - if (res.status === 200) { - ComponentGlobal_NotifikasiBerhasil(`Postingan Terhapus`, 2000); - setLoading(true); + try { + setLoading(true); + + const deleteData = await forum_funDeletePostingById(postingId as any); + if (deleteData.status === 200) { + setOpenDel(false); const cloneData = _.clone(allData); const hapusData = cloneData.filter((e) => e.id !== postingId); @@ -271,10 +273,16 @@ function ButtonDelete({ data: hapusData, }) ); + + ComponentGlobal_NotifikasiBerhasil(`Postingan Terhapus`, 2000); } else { - ComponentGlobal_NotifikasiGagal(res.message); + setLoading(false); + ComponentGlobal_NotifikasiGagal(deleteData.message); } - }); + } catch (error) { + setLoading(false); + clientLogger.error("Error get data forum", error); + } } return ( <> @@ -323,120 +331,131 @@ function ButtonStatus({ const [loading, setLoading] = useState(false); async function onTutupForum() { - setOpenStatus(false); - - const upateStatusClose = await forum_funEditStatusPostingById( - postingId as any, - 2 - ); - if (upateStatusClose.status === 200) { - ComponentGlobal_NotifikasiBerhasil(`Forum Ditutup`, 2000); + try { setLoading(true); - - const cloneData = _.clone(allData); - const loadData = cloneData.map( - (e) => ( - e.id === postingId, - { - ...e, - ForumMaster_StatusPosting: { - id: e.id === postingId ? 2 : e.ForumMaster_StatusPosting.id, - status: - e.id === postingId - ? "Close" - : e.ForumMaster_StatusPosting.status, - }, - } - ) - ); - onLoadData(loadData); - - // - mqtt_client.publish( - "Forum_ganti_status", - JSON.stringify({ - id: postingId, - data: loadData, - }) + const upateStatusClose = await forum_funEditStatusPostingById( + postingId as any, + 2 ); - const findData = cloneData.find((val) => val.id === postingId); - const updateDetail = { - ...findData, - ForumMaster_StatusPosting: { - id: 2, - status: "Close", - }, - }; + if (upateStatusClose.status === 200) { + setOpenStatus(false); + ComponentGlobal_NotifikasiBerhasil(`Forum Ditutup`, 2000); - mqtt_client.publish( - "Forum_detail_ganti_status", - JSON.stringify({ - id: postingId, - data: updateDetail.ForumMaster_StatusPosting, - }) - ); - } else { - ComponentGlobal_NotifikasiGagal(upateStatusClose.message); + const cloneData = _.clone(allData); + const loadData = cloneData.map( + (e) => ( + e.id === postingId, + { + ...e, + ForumMaster_StatusPosting: { + id: e.id === postingId ? 2 : e.ForumMaster_StatusPosting.id, + status: + e.id === postingId + ? "Close" + : e.ForumMaster_StatusPosting.status, + }, + } + ) + ); + onLoadData(loadData); + + mqtt_client.publish( + "Forum_ganti_status", + JSON.stringify({ + id: postingId, + data: loadData, + }) + ); + + const findData = cloneData.find((val) => val.id === postingId); + const updateDetail = { + ...findData, + ForumMaster_StatusPosting: { + id: 2, + status: "Close", + }, + }; + + mqtt_client.publish( + "Forum_detail_ganti_status", + JSON.stringify({ + id: postingId, + data: updateDetail.ForumMaster_StatusPosting, + }) + ); + } else { + setLoading(false); + ComponentGlobal_NotifikasiGagal(upateStatusClose.message); + } + } catch (error) { + setLoading(false); + clientLogger.error("Error get data forum", error); } } async function onBukaForum() { - setOpenStatus(false); - - const updateStatusOpen = await forum_funEditStatusPostingById( - postingId as any, - 1 - ); - if (updateStatusOpen.status === 200) { - ComponentGlobal_NotifikasiBerhasil(`Forum Dibuka`, 2000); + try { setLoading(true); - - const cloneData = _.clone(allData); - const loadData = cloneData.map( - (e) => ( - e.id === postingId, - { - ...e, - ForumMaster_StatusPosting: { - id: e.id === postingId ? 1 : e.ForumMaster_StatusPosting.id, - status: - e.id === postingId - ? "Open" - : e.ForumMaster_StatusPosting.status, - }, - } - ) + const updateStatusOpen = await forum_funEditStatusPostingById( + postingId as any, + 1 ); - mqtt_client.publish( - "Forum_ganti_status", - JSON.stringify({ - id: postingId, - data: loadData, - }) - ); + if (updateStatusOpen.status === 200) { + setOpenStatus(false); + ComponentGlobal_NotifikasiBerhasil(`Forum Dibuka`, 2000); - onLoadData(loadData); + const cloneData = _.clone(allData); + const loadData = cloneData.map( + (e) => ( + e.id === postingId, + { + ...e, + ForumMaster_StatusPosting: { + id: e.id === postingId ? 1 : e.ForumMaster_StatusPosting.id, + status: + e.id === postingId + ? "Open" + : e.ForumMaster_StatusPosting.status, + }, + } + ) + ); - const findData = cloneData.find((val) => val.id === postingId); - const updateDetail = { - ...findData, - ForumMaster_StatusPosting: { - id: 1, - status: "Open", - }, - }; + mqtt_client.publish( + "Forum_ganti_status", + JSON.stringify({ + id: postingId, + data: loadData, + }) + ); - mqtt_client.publish( - "Forum_detail_ganti_status", - JSON.stringify({ - id: postingId, - data: updateDetail.ForumMaster_StatusPosting, - }) - ); - } else { - ComponentGlobal_NotifikasiGagal(updateStatusOpen.message); + onLoadData(loadData); + + const findData = cloneData.find((val) => val.id === postingId); + const updateDetail = { + ...findData, + ForumMaster_StatusPosting: { + id: 1, + status: "Open", + }, + }; + + mqtt_client.publish( + "Forum_detail_ganti_status", + JSON.stringify({ + id: postingId, + data: updateDetail.ForumMaster_StatusPosting, + }) + ); + } else { + setLoading(false); + ComponentGlobal_NotifikasiGagal(updateStatusOpen.message); + } + } catch (error) { + setLoading(false); + clientLogger.error("Error get data forum", error); } } diff --git a/src/app_modules/forum/forumku/index.tsx b/src/app_modules/forum/forumku/index.tsx index 6c437185..c56d83ad 100644 --- a/src/app_modules/forum/forumku/index.tsx +++ b/src/app_modules/forum/forumku/index.tsx @@ -1,44 +1,33 @@ "use client"; -import { RouterForum } from "@/lib/router_hipmi/router_forum"; -import { AccentColor } from "@/app_modules/_global/color/color_pallet"; +import ComponentGlobal_CreateButton from "@/app_modules/_global/component/button_create"; +import { apiGetUserById } from "@/app_modules/_global/lib/api_user"; import { MODEL_USER } from "@/app_modules/home/model/interface"; +import { RouterForum } from "@/lib/router_hipmi/router_forum"; +import { clientLogger } from "@/util/clientLogger"; import { - ActionIcon, - Affix, Center, Loader, - Stack, - Text, - rem, + Stack } from "@mantine/core"; -import { useShallowEffect, useWindowScroll } from "@mantine/hooks"; -import { IconPencilPlus, IconSearchOff } from "@tabler/icons-react"; +import { useShallowEffect } from "@mantine/hooks"; import _ from "lodash"; import { ScrollOnly } from "next-scroll-loader"; import { useParams, useRouter } from "next/navigation"; import { useState } from "react"; -import ComponentForum_ForumkuMainCardView from "../component/forumku_component/forumku_view"; -import { forum_getAllPostingByAuhtorId } from "../fun/get/get_list_posting_by_author_id"; -import { MODEL_FORUM_POSTING } from "../model/interface"; -import ComponentForum_ViewForumProfile from "./forum_profile"; -import ComponentGlobal_CreateButton from "@/app_modules/_global/component/button_create"; -import { apiGetUserById } from "@/app_modules/_global/lib/api_user"; -import backendLogger from "@/util/backendLogger"; -import { clientLogger } from "@/util/clientLogger"; import { apiGetForumkuById } from "../component/api_fetch_forum"; +import ComponentForum_ForumkuMainCardView from "../component/forumku_component/forumku_view"; +import { Forum_ComponentIsDataEmpty } from "../component/other_component"; import { Forum_SkeletonCard, Forum_SkeletonForumku, } from "../component/skeleton_view"; -import { data } from "autoprefixer"; -import { Forum_ComponentIsDataEmpty } from "../component/other_component"; +import { MODEL_FORUM_POSTING } from "../model/interface"; +import ComponentForum_ViewForumProfile from "./forum_profile"; export default function Forum_Forumku({ - totalPosting, userLoginId, }: { - totalPosting: number; userLoginId: string; }) { const router = useRouter(); @@ -56,7 +45,6 @@ export default function Forum_Forumku({ }); if (response) { - console.log("response", response); setDataUser(response.data); } } catch (error) { @@ -117,7 +105,7 @@ export default function Forum_Forumku({ ) : ( )} From 4318ea4890bdce33c3fb9d40cd43221396e5a15a Mon Sep 17 00:00:00 2001 From: Bagasbanuna02 Date: Tue, 18 Feb 2025 16:25:23 +0800 Subject: [PATCH 2/7] fix forum deskripsi: - fix postingan dan komentar --- run.build.dev | 1 - run.env.build.dev | 1 + run.env.local => run.env.build.local | 0 run.dev => run.env.dev | 0 run.env.start.dev | 1 + run.env.strat.local | 0 src/app/api/forum/[id]/komentar/route.ts | 93 ++++++++ .../forum/detail/main-detail/[id]/page.tsx | 8 +- src/app/zCoba/skeleton/page.tsx | 26 +-- .../forum/component/api_fetch_forum.ts | 43 +++- .../detail_create_komentar.tsx | 80 ++++--- .../detail_component/detail_list_komentar.tsx | 94 +++------ .../komentar_author_header_name.tsx | 6 +- .../forum/component/skeleton_view.tsx | 35 ++- src/app_modules/forum/detail/main_detail.tsx | 199 ++++++++++++------ src/middleware.ts | 1 + 16 files changed, 397 insertions(+), 191 deletions(-) delete mode 100644 run.build.dev create mode 100644 run.env.build.dev rename run.env.local => run.env.build.local (100%) rename run.dev => run.env.dev (100%) create mode 100644 run.env.start.dev create mode 100644 run.env.strat.local create mode 100644 src/app/api/forum/[id]/komentar/route.ts diff --git a/run.build.dev b/run.build.dev deleted file mode 100644 index 49348626..00000000 --- a/run.build.dev +++ /dev/null @@ -1 +0,0 @@ -nice -n 19 bun --env-file=.env.build run --bun build diff --git a/run.env.build.dev b/run.env.build.dev new file mode 100644 index 00000000..f8351819 --- /dev/null +++ b/run.env.build.dev @@ -0,0 +1 @@ +nice -n 19 bun --env-file=.env run --bun build diff --git a/run.env.local b/run.env.build.local similarity index 100% rename from run.env.local rename to run.env.build.local diff --git a/run.dev b/run.env.dev similarity index 100% rename from run.dev rename to run.env.dev diff --git a/run.env.start.dev b/run.env.start.dev new file mode 100644 index 00000000..e802570f --- /dev/null +++ b/run.env.start.dev @@ -0,0 +1 @@ +nice -n 19 bun --env-file=.env run --bun start \ No newline at end of file diff --git a/run.env.strat.local b/run.env.strat.local new file mode 100644 index 00000000..e69de29b diff --git a/src/app/api/forum/[id]/komentar/route.ts b/src/app/api/forum/[id]/komentar/route.ts new file mode 100644 index 00000000..404bcb16 --- /dev/null +++ b/src/app/api/forum/[id]/komentar/route.ts @@ -0,0 +1,93 @@ +import backendLogger from "@/util/backendLogger"; +import { NextResponse } from "next/server"; + +export { GET }; + +async function GET(request: Request, { params }: { params: { id: string } }) { + try { + let fixData; + const { id } = params; + const { searchParams } = new URL(request.url); + const page = searchParams.get("page"); + const takeData = 5; + const skipData = Number(page) * takeData - takeData; + + if (!page) { + fixData = await prisma.forum_Komentar.findMany({ + orderBy: { + createdAt: "desc", + }, + where: { + forum_PostingId: id, + isActive: true, + }, + select: { + id: true, + isActive: true, + komentar: true, + createdAt: true, + Author: { + select: { + id: true, + username: true, + Profile: { + select: { + name: true, + imageId: true, + }, + }, + }, + }, + authorId: true, + }, + }); + } else { + fixData = await prisma.forum_Komentar.findMany({ + take: takeData, + skip: skipData, + orderBy: { + createdAt: "desc", + }, + where: { + forum_PostingId: id, + isActive: true, + }, + select: { + id: true, + isActive: true, + komentar: true, + createdAt: true, + Author: { + select: { + id: true, + username: true, + Profile: { + select: { + name: true, + imageId: true, + }, + }, + }, + }, + authorId: true, + }, + }); + } + + return NextResponse.json({ + success: true, + message: "Berhasil mendapatkan data", + data: fixData, + }); + } catch (error) { + backendLogger.error("Error Get Forum Komentar >>", error); + return NextResponse.json( + { + success: false, + message: "API Error Get Data", + reason: (error as Error).message, + }, + { status: 500 } + ); + } +} diff --git a/src/app/dev/forum/detail/main-detail/[id]/page.tsx b/src/app/dev/forum/detail/main-detail/[id]/page.tsx index b48bb504..3905795f 100644 --- a/src/app/dev/forum/detail/main-detail/[id]/page.tsx +++ b/src/app/dev/forum/detail/main-detail/[id]/page.tsx @@ -10,21 +10,19 @@ export default async function Page({ params }: { params: { id: string } }) { let postingId = params.id; const userLoginId = await funGetUserIdByToken(); - const dataPosting = await forum_getOnePostingById(postingId); + // const dataPosting = await forum_getOnePostingById(postingId); const listKomentar = await forum_funGetAllKomentarById({ postingId: postingId, page: 1, }); - // dataPosting?.isActive === false && redirect(RouterForum.beranda); - const countKomentar = await forum_countTotalKomenById(postingId); return ( <> diff --git a/src/app/zCoba/skeleton/page.tsx b/src/app/zCoba/skeleton/page.tsx index 5e0c03ad..2e5b8aa9 100644 --- a/src/app/zCoba/skeleton/page.tsx +++ b/src/app/zCoba/skeleton/page.tsx @@ -16,24 +16,24 @@ export default function Voting_ComponentSkeletonViewPuh() { header={} > -
- -
- - - - - - - + + + - - + + + + - + + + + + +
{/* diff --git a/src/app_modules/forum/component/api_fetch_forum.ts b/src/app_modules/forum/component/api_fetch_forum.ts index 6bf3d540..08f00db1 100644 --- a/src/app_modules/forum/component/api_fetch_forum.ts +++ b/src/app_modules/forum/component/api_fetch_forum.ts @@ -1,4 +1,9 @@ -export { apiGetAllForum, apiGetOneForumById, apiGetForumkuById }; +export { + apiGetAllForum, + apiGetOneForumById, + apiGetForumkuByUserId as apiGetForumkuById, + apiGetKomentarForumById, +}; const apiGetAllForum = async ({ page, @@ -72,7 +77,7 @@ const apiGetOneForumById = async ({ id }: { id: string }) => { } }; -const apiGetForumkuById = async ({ +const apiGetForumkuByUserId = async ({ id, page, }: { @@ -111,3 +116,37 @@ const apiGetForumkuById = async ({ throw error; // Re-throw the error to handle it in the calling function } }; + +const apiGetKomentarForumById = async ({ id , page}: { id: string , page: string}) => { + try { + // Fetch token from cookie + const { token } = await fetch("/api/get-cookie").then((res) => res.json()); + if (!token) { + console.error("No token found"); + return null; + } + + const nextPage = `?page=${page}`; + const response = await fetch(`/api/forum/${id}/komentar${nextPage}`, { + method: "GET", + headers: { + "Content-Type": "application/json", + Accept: "application/json", + Authorization: `Bearer ${token}`, + }, + }); + + // Check if the response is OK + if (!response.ok) { + const errorData = await response.json().catch(() => null); + console.error("Failed to get all forum:", response.statusText, errorData); + throw new Error(errorData?.message || "Failed to get all forum"); + } + + // Return the JSON response + return await response.json(); + } catch (error) { + console.error("Error get all forum", error); + throw error; // Re-throw the error to handle it in the calling function + } +}; diff --git a/src/app_modules/forum/component/detail_component/detail_create_komentar.tsx b/src/app_modules/forum/component/detail_component/detail_create_komentar.tsx index a07eb6e0..224220aa 100644 --- a/src/app_modules/forum/component/detail_component/detail_create_komentar.tsx +++ b/src/app_modules/forum/component/detail_component/detail_create_komentar.tsx @@ -19,16 +19,20 @@ import { MODEL_FORUM_POSTING } from "../../model/interface"; import { useRouter } from "next/navigation"; import { MainColor } from "@/app_modules/_global/color/color_pallet"; import mqtt_client from "@/util/mqtt_client"; +import backendLogger from "@/util/backendLogger"; +import { clientLogger } from "@/util/clientLogger"; export default function ComponentForum_DetailCreateKomentar({ postingId, onSetKomentar, data, userLoginId, + onSetNewKomentar, }: { postingId: string; onSetKomentar: (val: any) => void; data: MODEL_FORUM_POSTING; userLoginId: string; + onSetNewKomentar: (val: boolean) => void; }) { const router = useRouter(); const [value, setValue] = useState(""); @@ -40,45 +44,52 @@ export default function ComponentForum_DetailCreateKomentar({ return null; } - const createComment = await forum_funCreateKomentar(postingId, value); - if (createComment.status === 201) { - // const loadKomentar = await forum_funGetAllKomentarById(data.id); + try { + setLoading(true); + const createComment = await forum_funCreateKomentar(postingId, value); + if (createComment.status === 201) { + // const loadData = await forum_funGetAllKomentarById({ + // postingId: data.id, + // page: 1, + // }); + // onSetKomentar(loadData); - const loadData = await forum_funGetAllKomentarById({ - postingId: data.id, - page: 1, - }); - onSetKomentar(loadData); + onSetNewKomentar(true); + setValue(""); + setIsEmpty(true); + ComponentGlobal_NotifikasiBerhasil(createComment.message, 2000); - setValue(""); - setIsEmpty(true); - ComponentGlobal_NotifikasiBerhasil(createComment.message, 2000); + if (userLoginId !== data.Author.id) { + const dataNotif = { + appId: data.id, + userId: data.authorId, + pesan: value, + kategoriApp: "FORUM", + title: "Komentar baru", + }; - if (userLoginId !== data.Author.id) { - const dataNotif = { - appId: data.id, - userId: data.authorId, - pesan: value, - kategoriApp: "FORUM", - title: "Komentar baru", - }; + const createNotifikasi = await notifikasiToUser_funCreate({ + data: dataNotif as any, + }); - const createNotifikasi = await notifikasiToUser_funCreate({ - data: dataNotif as any, - }); - - if (createNotifikasi.status === 201) { - mqtt_client.publish( - "USER", - JSON.stringify({ - userId: dataNotif.userId, - count: 1, - }) - ); + if (createNotifikasi.status === 201) { + mqtt_client.publish( + "USER", + JSON.stringify({ + userId: dataNotif.userId, + count: 1, + }) + ); + } } + } else { + setLoading(false); + ComponentGlobal_NotifikasiGagal(createComment.message); } - } else { - ComponentGlobal_NotifikasiGagal(createComment.message); + } catch (error) { + setLoading(false); + + clientLogger.error("Error create komentar forum", error); } } @@ -117,8 +128,9 @@ export default function ComponentForum_DetailCreateKomentar({ } bg={MainColor.yellow} color={"yellow"} + c="black" loaderPosition="center" - loading={loading ? true : false} + loading={loading} radius={"xl"} onClick={() => onComment()} > diff --git a/src/app_modules/forum/component/detail_component/detail_list_komentar.tsx b/src/app_modules/forum/component/detail_component/detail_list_komentar.tsx index 04c42e52..1232555b 100644 --- a/src/app_modules/forum/component/detail_component/detail_list_komentar.tsx +++ b/src/app_modules/forum/component/detail_component/detail_list_komentar.tsx @@ -1,14 +1,9 @@ "use client"; -import { - Card, - Divider, - Spoiler, - Stack, - Text -} from "@mantine/core"; +import { Card, Divider, Spoiler, Stack, Text } from "@mantine/core"; import { MODEL_FORUM_KOMENTAR } from "../../model/interface"; import ComponentForum_KomentarAuthorNameOnHeader from "../komentar_component/komentar_author_header_name"; +import { ComponentGlobal_CardStyles } from "@/app_modules/_global/component"; export default function ComponentForum_KomentarView({ data, @@ -23,65 +18,34 @@ export default function ComponentForum_KomentarView({ }) { return ( <> - - - - - - - - {data.komentar ? ( - -
- - ) : ( - "" - )} - - - + + - - - - - - - - {/* - {_.isEmpty(data) ? ( -
- - Belum ada komentar - -
- ) : ( - -
- - {" "} - Komentar - -
- {data.map((e, i) => ( - - ))} -
- )} -
*/} + + + {data.komentar ? ( + +
+ + ) : ( + "" + )} + + + ); } diff --git a/src/app_modules/forum/component/komentar_component/komentar_author_header_name.tsx b/src/app_modules/forum/component/komentar_component/komentar_author_header_name.tsx index 344c8087..46c0f76f 100644 --- a/src/app_modules/forum/component/komentar_component/komentar_author_header_name.tsx +++ b/src/app_modules/forum/component/komentar_component/komentar_author_header_name.tsx @@ -12,6 +12,7 @@ import { ComponentGlobal_LoaderAvatar } from "@/app_modules/_global/component"; import ComponentGlobal_Loader from "@/app_modules/_global/component/loader"; import { data } from "autoprefixer"; import { MODEL_PROFILE } from "@/app_modules/katalog/profile/model/interface"; +import moment from "moment"; export default function ComponentForum_KomentarAuthorNameOnHeader({ userId, @@ -84,10 +85,11 @@ export default function ComponentForum_KomentarAuthorNameOnHeader({ {tglPublish - ? tglPublish.toLocaleDateString(["id-ID"], { + ? new Intl.DateTimeFormat("id-ID", { day: "numeric", month: "short", - }) + year: "numeric", + }).format(new Date(tglPublish)) : new Date().toLocaleDateString(["id-ID"], { day: "numeric", month: "short", diff --git a/src/app_modules/forum/component/skeleton_view.tsx b/src/app_modules/forum/component/skeleton_view.tsx index 44241208..67b6c18e 100644 --- a/src/app_modules/forum/component/skeleton_view.tsx +++ b/src/app_modules/forum/component/skeleton_view.tsx @@ -1,7 +1,12 @@ import CustomSkeleton from "@/app_modules/components/CustomSkeleton"; import { Center, Grid, Group, Stack } from "@mantine/core"; -export { Forum_SkeletonCard, Forum_SkeletonForumku }; +export { + Forum_SkeletonCard, + Forum_SkeletonForumku, + Forum_SkeletonKomentar, + Forum_SkeletonListKomentar, +}; function Forum_SkeletonCard() { return ( @@ -14,7 +19,7 @@ function Forum_SkeletonCard() { ); } -function Forum_SkeletonForumku(){ +function Forum_SkeletonForumku() { return ( <> @@ -40,3 +45,29 @@ function Forum_SkeletonForumku(){ ); } + +function Forum_SkeletonKomentar() { + return ( + <> + + + + + + + + + ); +} + +function Forum_SkeletonListKomentar() { + return ( + <> + + {Array.from(new Array(2)).map((e, i) => ( + + ))} + + + ); +} diff --git a/src/app_modules/forum/detail/main_detail.tsx b/src/app_modules/forum/detail/main_detail.tsx index 2ee67ed4..c0353948 100644 --- a/src/app_modules/forum/detail/main_detail.tsx +++ b/src/app_modules/forum/detail/main_detail.tsx @@ -1,6 +1,6 @@ "use client"; -import { Box, Center, Loader, Stack, TextInput } from "@mantine/core"; +import { Box, Center, Group, Loader, Stack, TextInput } from "@mantine/core"; import _ from "lodash"; import { MODEL_FORUM_KOMENTAR, MODEL_FORUM_POSTING } from "../model/interface"; import mqtt_client from "@/util/mqtt_client"; @@ -12,41 +12,99 @@ import ComponentForum_KomentarView from "../component/detail_component/detail_li import ComponentForum_DetailForumView from "../component/detail_component/detail_view"; import { ScrollOnly } from "next-scroll-loader"; import { forum_funGetAllKomentarById } from "../fun/get/get_all_komentar_by_id"; +import { + apiGetKomentarForumById, + apiGetOneForumById, +} from "../component/api_fetch_forum"; +import { useParams } from "next/navigation"; +import { clientLogger } from "@/util/clientLogger"; +import CustomSkeleton from "@/app_modules/components/CustomSkeleton"; +import { + Forum_SkeletonKomentar, + Forum_SkeletonListKomentar, +} from "../component/skeleton_view"; +import ComponentGlobal_IsEmptyData from "@/app_modules/_global/component/is_empty_data"; export default function Forum_MainDetail({ - dataPosting, - listKomentar, userLoginId, countKomentar, }: { - dataPosting: MODEL_FORUM_POSTING; - listKomentar: MODEL_FORUM_KOMENTAR[]; userLoginId: string; countKomentar: number; }) { - const [data, setData] = useState(dataPosting); - const [lsKomentar, setLsKomentar] = useState(listKomentar); + const param = useParams<{ id: string }>(); + const [data, setData] = useState(null); + const [lsKomentar, setLsKomentar] = useState([]); const [activePage, setActivePage] = useState(1); + const [newKomentar, setNewKomentar] = useState(false); - // useShallowEffect(() => { - // onLoadKomentar({ - // onLoad(val) { - // setKomentar(val); - // }, - // }); - // }, [setKomentar]); + useShallowEffect(() => { + handleLoadData(); + }, []); - // async function onLoadKomentar({ onLoad }: { onLoad: (val: any) => void }) { - // const loadKomentar = await forum_getKomentarById(data.id); - // onLoad(loadKomentar); - // } + const handleLoadData = async () => { + try { + const response = await apiGetOneForumById({ + id: param.id, + }); + + if (response) { + setData(response.data); + } + } catch (error) { + clientLogger.error("Error get data forum", error); + setData(null); + } + }; + + useShallowEffect(() => { + handleLoadDataKomentar(); + }, [newKomentar]); + + const handleLoadDataKomentar = async () => { + try { + const response = await apiGetKomentarForumById({ + id: param.id, + page: `${activePage}`, + }); + + if (response.success) { + setLsKomentar(response.data); + } else { + setLsKomentar([]); + } + } catch (error) { + clientLogger.error("Error get data komentar forum", error); + setLsKomentar([]); + } + }; + + const handleMoreDataKomentar = async () => { + try { + const nextPage = activePage + 1; + const response = await apiGetKomentarForumById({ + id: param.id, + page: `${nextPage}`, + }); + + if (response.success) { + setActivePage(nextPage); + return response.data; + } else { + return null; + } + } catch (error) { + clientLogger.error("Error get data komentar forum", error); + return null; + } + }; useShallowEffect(() => { mqtt_client.subscribe("Forum_detail_ganti_status"); mqtt_client.on("message", (topic: any, message: any) => { const newData = JSON.parse(message.toString()); - if (newData.id === data.id) { + if (newData.id === data?.id) { const cloneData = _.clone(data); // console.log(newData.data); @@ -66,58 +124,65 @@ export default function Forum_MainDetail({ return ( <> - { - setData(val); - }} - /> - - {(data?.ForumMaster_StatusPosting?.id as any) === 1 ? ( - { - setLsKomentar(val); - }} - data={data} - userLoginId={userLoginId} - /> + {!data ? ( + ) : ( - "" + { + setData(val); + }} + /> )} - - ( -
- -
- )} - data={lsKomentar} - setData={setLsKomentar} - moreData={async () => { - const loadData = await forum_funGetAllKomentarById({ - postingId: data.id, - page: activePage + 1, - }); - setActivePage((val) => val + 1); + {!data ? ( + + ) : ( + (data?.ForumMaster_StatusPosting?.id as any) === 1 && ( + { + setLsKomentar(val); + }} + data={data} + userLoginId={userLoginId} + onSetNewKomentar={(val) => { + setNewKomentar(val); + }} + /> + ) + )} - return loadData; - }} - > - {(item) => ( - - )} -
-
+ {!lsKomentar.length ? ( + + ) : _.isEmpty(lsKomentar) ? ( + + ) : ( + + ( +
+ +
+ )} + data={lsKomentar} + setData={setLsKomentar} + moreData={handleMoreDataKomentar} + > + {(item) => ( + + )} +
+
+ )}
); diff --git a/src/middleware.ts b/src/middleware.ts index 97c69c95..c86b5904 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -31,6 +31,7 @@ const middlewareConfig: MiddlewareConfig = { "/api/auth/*", "/api/origin-url", "/api/event/*", + "/api/forum/*", // ADMIN API // >> buat dibawah sini << From 0d230c3c47bb8370893f6e00499a5e57a378112f Mon Sep 17 00:00:00 2001 From: Bagasbanuna02 Date: Wed, 19 Feb 2025 10:30:02 +0800 Subject: [PATCH 3/7] fix api forum deskripsi: - delete button and function --- src/app/api/forum/[id]/route.ts | 8 +- .../forum/detail/main-detail/[id]/page.tsx | 23 +---- .../detail_create_komentar.tsx | 8 -- .../detail_component/detail_header.tsx | 93 ++++++++++++------- .../detail_component/detail_view.tsx | 8 +- src/app_modules/forum/detail/main_detail.tsx | 67 ++++++------- .../forum/fun/create/fun_create_komentar.tsx | 31 ++++--- .../fun/delete/fun_delete_posting_by_id.ts | 31 ++++--- src/app_modules/forum/model/interface.tsx | 1 + 9 files changed, 145 insertions(+), 125 deletions(-) diff --git a/src/app/api/forum/[id]/route.ts b/src/app/api/forum/[id]/route.ts index 9a18775f..080024b0 100644 --- a/src/app/api/forum/[id]/route.ts +++ b/src/app/api/forum/[id]/route.ts @@ -24,7 +24,6 @@ async function GET(request: Request, { params }: { params: { id: string } }) { Profile: true, }, }, - _count: { select: { Forum_Komentar: true, @@ -35,10 +34,15 @@ async function GET(request: Request, { params }: { params: { id: string } }) { }, }); + const fixData = { + ...data, + count: data?._count.Forum_Komentar, + } + return NextResponse.json({ success: true, message: "Success get data", - data: data, + data: fixData, }); } catch (error) { backendLogger.error("Error get data forum", error); diff --git a/src/app/dev/forum/detail/main-detail/[id]/page.tsx b/src/app/dev/forum/detail/main-detail/[id]/page.tsx index 3905795f..596596bb 100644 --- a/src/app/dev/forum/detail/main-detail/[id]/page.tsx +++ b/src/app/dev/forum/detail/main-detail/[id]/page.tsx @@ -1,31 +1,12 @@ -import { RouterForum } from "@/lib/router_hipmi/router_forum"; import { funGetUserIdByToken } from "@/app_modules/_global/fun/get"; import Forum_MainDetail from "@/app_modules/forum/detail/main_detail"; -import { forum_countTotalKomenById } from "@/app_modules/forum/fun/count/count_total_komentar_by_id"; -import { forum_funGetAllKomentarById } from "@/app_modules/forum/fun/get/get_all_komentar_by_id"; -import { forum_getOnePostingById } from "@/app_modules/forum/fun/get/get_one_posting_by_id"; -import { redirect } from "next/navigation"; -export default async function Page({ params }: { params: { id: string } }) { - let postingId = params.id; +export default async function Page() { const userLoginId = await funGetUserIdByToken(); - // const dataPosting = await forum_getOnePostingById(postingId); - const listKomentar = await forum_funGetAllKomentarById({ - postingId: postingId, - page: 1, - }); - - const countKomentar = await forum_countTotalKomenById(postingId); - return ( <> - + ); } diff --git a/src/app_modules/forum/component/detail_component/detail_create_komentar.tsx b/src/app_modules/forum/component/detail_component/detail_create_komentar.tsx index 224220aa..2057c68d 100644 --- a/src/app_modules/forum/component/detail_component/detail_create_komentar.tsx +++ b/src/app_modules/forum/component/detail_component/detail_create_komentar.tsx @@ -23,13 +23,11 @@ import backendLogger from "@/util/backendLogger"; import { clientLogger } from "@/util/clientLogger"; export default function ComponentForum_DetailCreateKomentar({ postingId, - onSetKomentar, data, userLoginId, onSetNewKomentar, }: { postingId: string; - onSetKomentar: (val: any) => void; data: MODEL_FORUM_POSTING; userLoginId: string; onSetNewKomentar: (val: boolean) => void; @@ -48,11 +46,6 @@ export default function ComponentForum_DetailCreateKomentar({ setLoading(true); const createComment = await forum_funCreateKomentar(postingId, value); if (createComment.status === 201) { - // const loadData = await forum_funGetAllKomentarById({ - // postingId: data.id, - // page: 1, - // }); - // onSetKomentar(loadData); onSetNewKomentar(true); setValue(""); @@ -88,7 +81,6 @@ export default function ComponentForum_DetailCreateKomentar({ } } catch (error) { setLoading(false); - clientLogger.error("Error create komentar forum", error); } } diff --git a/src/app_modules/forum/component/detail_component/detail_header.tsx b/src/app_modules/forum/component/detail_component/detail_header.tsx index c232ddf7..f012055e 100644 --- a/src/app_modules/forum/component/detail_component/detail_header.tsx +++ b/src/app_modules/forum/component/detail_component/detail_header.tsx @@ -50,6 +50,8 @@ import { } from "@/app_modules/_global/color/color_pallet"; import { ComponentGlobal_LoaderAvatar } from "@/app_modules/_global/component"; import ComponentGlobal_Loader from "@/app_modules/_global/component/loader"; +import { clientLogger } from "@/util/clientLogger"; +import _ from "lodash"; export default function ComponentForum_DetailHeader({ data, @@ -123,7 +125,8 @@ export default function ComponentForum_DetailHeader({ postingId={data?.id} authorId={data?.Author.id} userLoginId={userLoginId} - statusId={data?.forumMaster_StatusPostingId} + statusId={data?.ForumMaster_StatusPosting.id} + dataPosting={data} onLoadData={(val) => { onLoadData(val); }} @@ -141,12 +144,14 @@ function ComponentForum_DetailButtonMore_V2({ postingId, statusId, userLoginId, + dataPosting, onLoadData, }: { authorId: any; postingId?: any; statusId: any; userLoginId: any; + dataPosting: any; onLoadData: (val: any) => void; }) { const router = useRouter(); @@ -312,6 +317,7 @@ function ComponentForum_DetailButtonMore_V2({ postingId={postingId} setOpenStatus={setOpenStatusClose} statusId={statusId} + dataPosting={dataPosting} onLoadData={(val) => { onLoadData(val); }} @@ -338,13 +344,15 @@ function ButtonDelete({ if (loading) return ; async function onDelete() { - setOpenDel(false); - await forum_funDeletePostingById(postingId as any).then((res) => { - if (res.status === 200) { - ComponentGlobal_NotifikasiBerhasil(`Postingan Terhapus`, 2000); - setLoading(true); + try { + setLoading(true); + const responseDelete = await forum_funDeletePostingById(postingId as any); + if (responseDelete.status === 200) { + setOpenDel(false); router.back(); + ComponentGlobal_NotifikasiBerhasil(`Postingan Terhapus`, 2000); + // mqtt_client.publish( // "Forum_detail_hapus_data", // JSON.stringify({ @@ -352,9 +360,13 @@ function ButtonDelete({ // }) // ); } else { - ComponentGlobal_NotifikasiGagal(res.message); + setLoading(false); + ComponentGlobal_NotifikasiGagal(responseDelete.message); } - }); + } catch (error) { + setLoading(false); + clientLogger.error("Error get data forum", error); + } } return ( <> @@ -387,38 +399,40 @@ function ButtonStatus({ postingId, setOpenStatus, statusId, + dataPosting, onLoadData, }: { postingId?: string; setOpenStatus: any; statusId?: any; + dataPosting: any; onLoadData: (val: any) => void; }) { const [loading, setLoading] = useState(false); async function onTutupForum() { - setOpenStatus(false); - - const closeForum = await forum_funEditStatusPostingById( - postingId as any, - 2 - ); - if (closeForum.status === 200) { - ComponentGlobal_NotifikasiBerhasil(`Forum Ditutup`, 2000); + try { setLoading(true); + const closeForum = await forum_funEditStatusPostingById( + postingId as any, + 2 + ); - const loadData = await forum_getOnePostingById(postingId as any); - onLoadData(loadData); + if (closeForum.status === 200) { + setOpenStatus(false); + ComponentGlobal_NotifikasiBerhasil(`Forum Ditutup`, 2000); - if (loadData) { + const cloneData = _.clone(dataPosting); const updateData = { - ...loadData, + ...cloneData, ForumMaster_StatusPosting: { id: 2, status: "Close", }, }; + onLoadData(updateData); + mqtt_client.publish( "Forum_detail_ganti_status", JSON.stringify({ @@ -426,32 +440,39 @@ function ButtonStatus({ data: updateData.ForumMaster_StatusPosting, }) ); + } else { + setLoading(false); + ComponentGlobal_NotifikasiGagal(closeForum.message); } - } else { - ComponentGlobal_NotifikasiGagal(closeForum.message); + } catch (error) { + setLoading(false); + clientLogger.error("Error get data forum", error); } } async function onBukaForum() { - setOpenStatus(false); + setLoading(true); - const openForum = await forum_funEditStatusPostingById(postingId as any, 1); - if (openForum.status === 200) { - ComponentGlobal_NotifikasiBerhasil(`Forum Dibuka`, 2000); - setLoading(true); + try { + const openForum = await forum_funEditStatusPostingById( + postingId as any, + 1 + ); + if (openForum.status === 200) { + setOpenStatus(false); + ComponentGlobal_NotifikasiBerhasil(`Forum Dibuka`, 2000); - const loadData = await forum_getOnePostingById(postingId as any); - onLoadData(loadData); - - if (loadData) { + const cloneData = _.clone(dataPosting); const updateData = { - ...loadData, + ...cloneData, ForumMaster_StatusPosting: { id: 1, status: "Open", }, }; + onLoadData(updateData); + mqtt_client.publish( "Forum_detail_ganti_status", JSON.stringify({ @@ -459,9 +480,13 @@ function ButtonStatus({ data: updateData.ForumMaster_StatusPosting, }) ); + } else { + setLoading(false); + ComponentGlobal_NotifikasiGagal(openForum.message); } - } else { - ComponentGlobal_NotifikasiGagal(openForum.message); + } catch (error) { + setLoading(false); + clientLogger.error("Error get data forum", error); } } diff --git a/src/app_modules/forum/component/detail_component/detail_view.tsx b/src/app_modules/forum/component/detail_component/detail_view.tsx index c445c779..7f36366b 100644 --- a/src/app_modules/forum/component/detail_component/detail_view.tsx +++ b/src/app_modules/forum/component/detail_component/detail_view.tsx @@ -1,14 +1,10 @@ "use client"; -import { Card, Stack, Group, Text, Box } from "@mantine/core"; +import { ComponentGlobal_CardStyles } from "@/app_modules/_global/component"; +import { Box, Group, Stack, Text } from "@mantine/core"; import { IconMessageCircle, IconMessageCircleX } from "@tabler/icons-react"; import { MODEL_FORUM_POSTING } from "../../model/interface"; import ComponentForum_DetailHeader from "./detail_header"; -import { - AccentColor, - MainColor, -} from "@/app_modules/_global/color/color_pallet"; -import { ComponentGlobal_CardStyles } from "@/app_modules/_global/component"; export default function ComponentForum_DetailForumView({ data, diff --git a/src/app_modules/forum/detail/main_detail.tsx b/src/app_modules/forum/detail/main_detail.tsx index c0353948..962a3159 100644 --- a/src/app_modules/forum/detail/main_detail.tsx +++ b/src/app_modules/forum/detail/main_detail.tsx @@ -27,16 +27,17 @@ import ComponentGlobal_IsEmptyData from "@/app_modules/_global/component/is_empt export default function Forum_MainDetail({ userLoginId, - countKomentar, }: { userLoginId: string; - countKomentar: number; }) { const param = useParams<{ id: string }>(); - const [data, setData] = useState(null); - const [lsKomentar, setLsKomentar] = useState([]); + const [dataPosting, setDataPosting] = useState( + null + ); + const [listKomentar, setListKomentar] = useState([]); const [activePage, setActivePage] = useState(1); const [newKomentar, setNewKomentar] = useState(false); + const [isLoading, setIsLoading] = useState(false); useShallowEffect(() => { handleLoadData(); @@ -44,16 +45,21 @@ export default function Forum_MainDetail({ const handleLoadData = async () => { try { + setIsLoading(true); const response = await apiGetOneForumById({ id: param.id, }); if (response) { - setData(response.data); + setDataPosting(response.data); + } else { + setDataPosting(null); } } catch (error) { clientLogger.error("Error get data forum", error); - setData(null); + setDataPosting(null); + } finally { + setIsLoading(false); } }; @@ -69,13 +75,13 @@ export default function Forum_MainDetail({ }); if (response.success) { - setLsKomentar(response.data); + setListKomentar(response.data); } else { - setLsKomentar([]); + setListKomentar([]); } } catch (error) { clientLogger.error("Error get data komentar forum", error); - setLsKomentar([]); + setListKomentar([]); } }; @@ -104,8 +110,8 @@ export default function Forum_MainDetail({ mqtt_client.on("message", (topic: any, message: any) => { const newData = JSON.parse(message.toString()); - if (newData.id === data?.id) { - const cloneData = _.clone(data); + if (newData.id === dataPosting?.id) { + const cloneData = _.clone(dataPosting); // console.log(newData.data); const updateData = { @@ -116,37 +122,34 @@ export default function Forum_MainDetail({ }, }; - setData(updateData as any); + setDataPosting(updateData as any); } }); - }, [data]); + }, [dataPosting]); return ( <> - {!data ? ( + {!dataPosting || !listKomentar ? ( ) : ( { - setData(val); + setDataPosting(val); }} /> )} - {!data ? ( + {!dataPosting ? ( ) : ( - (data?.ForumMaster_StatusPosting?.id as any) === 1 && ( + (dataPosting?.ForumMaster_StatusPosting?.id as any) === 1 && ( { - setLsKomentar(val); - }} - data={data} + postingId={dataPosting?.id} + data={dataPosting} userLoginId={userLoginId} onSetNewKomentar={(val) => { setNewKomentar(val); @@ -155,12 +158,12 @@ export default function Forum_MainDetail({ ) )} - {!lsKomentar.length ? ( + {!listKomentar.length && isLoading ? ( - ) : _.isEmpty(lsKomentar) ? ( - + ) : _.isEmpty(listKomentar) ? ( + ) : ( - + ( @@ -168,15 +171,15 @@ export default function Forum_MainDetail({ )} - data={lsKomentar} - setData={setLsKomentar} + data={listKomentar} + setData={setListKomentar} moreData={handleMoreDataKomentar} > {(item) => ( )} diff --git a/src/app_modules/forum/fun/create/fun_create_komentar.tsx b/src/app_modules/forum/fun/create/fun_create_komentar.tsx index a58f595b..7468af8d 100644 --- a/src/app_modules/forum/fun/create/fun_create_komentar.tsx +++ b/src/app_modules/forum/fun/create/fun_create_komentar.tsx @@ -8,17 +8,26 @@ export async function forum_funCreateKomentar( postingId: string, komentar: string ) { - const userLoginId = await funGetUserIdByToken(); + try { + const userLoginId = await funGetUserIdByToken(); - const create = await prisma.forum_Komentar.create({ - data: { - komentar: komentar, - forum_PostingId: postingId, - authorId: userLoginId, - }, - }); + const create = await prisma.forum_Komentar.create({ + data: { + komentar: komentar, + forum_PostingId: postingId, + authorId: userLoginId, + }, + }); - if (!create) return { status: 400, message: "Gagal menambahkan komentar" }; - revalidatePath("/dev/forum/detail"); - return { status: 201, message: "Berhasil menambahkan komentar" }; + if (!create) return { status: 400, message: "Gagal menambahkan komentar" }; + + return { status: 201, message: "Berhasil menambahkan komentar" }; + } catch (error) { + console.log(error); + return { + status: 500, + message: "Error API", + error: (error as Error).message, + }; + } } diff --git a/src/app_modules/forum/fun/delete/fun_delete_posting_by_id.ts b/src/app_modules/forum/fun/delete/fun_delete_posting_by_id.ts index c5a21a2f..0e59acaf 100644 --- a/src/app_modules/forum/fun/delete/fun_delete_posting_by_id.ts +++ b/src/app_modules/forum/fun/delete/fun_delete_posting_by_id.ts @@ -4,16 +4,25 @@ import prisma from "@/lib/prisma"; import { revalidatePath } from "next/cache"; export async function forum_funDeletePostingById(forumId: string) { - const del = await prisma.forum_Posting.update({ - where: { - id: forumId, - }, - data: { - isActive: false, - }, - }); + try { + const del = await prisma.forum_Posting.update({ + where: { + id: forumId, + }, + data: { + isActive: false, + }, + }); - if (!del) return { status: 400, message: "Gagal dihapus" }; - revalidatePath("/dev/forum/main"); - return { status: 200, message: "Berhasil dihapus" }; + if (!del) return { status: 400, message: "Gagal dihapus" }; + revalidatePath("/dev/forum/main"); + return { status: 200, message: "Berhasil dihapus" }; + } catch (error) { + console.log(error); + return { + status: 500, + message: "Error API", + error: (error as Error).message, + }; + } } diff --git a/src/app_modules/forum/model/interface.tsx b/src/app_modules/forum/model/interface.tsx index ae216cce..83c7a8df 100644 --- a/src/app_modules/forum/model/interface.tsx +++ b/src/app_modules/forum/model/interface.tsx @@ -14,6 +14,7 @@ export interface MODEL_FORUM_POSTING { Forum_ReportPosting: MODEL_FORUM_MASTER_REPORT[]; ForumMaster_StatusPosting: MODEL_FORUM_MASTER_STATUS; forumMaster_StatusPostingId: number; + count: number } export interface MODEL_FORUM_KOMENTAR { From 15792bb430fb4df58c2032fe5c6b5a66315bf011 Mon Sep 17 00:00:00 2001 From: Bagasbanuna02 Date: Wed, 19 Feb 2025 11:58:53 +0800 Subject: [PATCH 4/7] fix forum deskripsi: - fix create report posting --- src/app/api/forum/master/route.ts | 32 ++++++ .../report/posting-lainnya/[id]/page.tsx | 8 +- .../dev/forum/report/posting/[id]/page.tsx | 7 +- .../forum/component/api_fetch_forum.ts | 42 ++++++++ .../fun/create/fun_create_report_posting.ts | 31 +++--- .../fun_create_report_posting_lainnya.ts | 34 ++++--- .../fun/master/get_master_kategori_report.ts | 3 - .../forum/report/posting/index.tsx | 99 ++++++++++++------- .../forum/report/posting/lainnya.tsx | 68 ++++++++----- .../forum/report/posting/layout.tsx | 2 +- 10 files changed, 226 insertions(+), 100 deletions(-) create mode 100644 src/app/api/forum/master/route.ts diff --git a/src/app/api/forum/master/route.ts b/src/app/api/forum/master/route.ts new file mode 100644 index 00000000..a2b1e1eb --- /dev/null +++ b/src/app/api/forum/master/route.ts @@ -0,0 +1,32 @@ +import { NextResponse } from "next/server"; +import prisma from "@/lib/prisma"; +import backendLogger from "@/util/backendLogger"; + +export async function GET(request: Request) { + try { + const data = await prisma.forumMaster_KategoriReport.findMany({ + orderBy: { + createdAt: "asc", + }, + where: { + isActive: true, + }, + }); + + return NextResponse.json({ + success: true, + message: "Berhasil mendapatkan data", + data: data, + }); + } catch (error) { + backendLogger.error("Error Get Master Kategori Report >>", error); + return NextResponse.json( + { + success: false, + message: "Gagal mendapatkan data", + reason: (error as Error).message, + }, + { status: 500 } + ); + } +} diff --git a/src/app/dev/forum/report/posting-lainnya/[id]/page.tsx b/src/app/dev/forum/report/posting-lainnya/[id]/page.tsx index d376be6e..b66294b2 100644 --- a/src/app/dev/forum/report/posting-lainnya/[id]/page.tsx +++ b/src/app/dev/forum/report/posting-lainnya/[id]/page.tsx @@ -1,16 +1,12 @@ import { funGetUserIdByToken } from "@/app_modules/_global/fun/get"; import { Forum_ReportPostingLainnya } from "@/app_modules/forum"; -export default async function Page({ params }: { params: { id: string } }) { - let postingId = params.id; +export default async function Page() { const userLoginId = await funGetUserIdByToken(); return ( <> - + ); } diff --git a/src/app/dev/forum/report/posting/[id]/page.tsx b/src/app/dev/forum/report/posting/[id]/page.tsx index 4cecc0da..2ba25627 100644 --- a/src/app/dev/forum/report/posting/[id]/page.tsx +++ b/src/app/dev/forum/report/posting/[id]/page.tsx @@ -1,18 +1,13 @@ import { funGetUserIdByToken } from "@/app_modules/_global/fun/get"; import { Forum_ReportPosting } from "@/app_modules/forum"; -import { forum_getMasterKategoriReport } from "@/app_modules/forum/fun/master/get_master_kategori_report"; -export default async function Page({ params }: { params: { id: string } }) { - let postingId = params.id; +export default async function Page() { const userLoginId = await funGetUserIdByToken(); - const listReport = await forum_getMasterKategoriReport(); return ( <> diff --git a/src/app_modules/forum/component/api_fetch_forum.ts b/src/app_modules/forum/component/api_fetch_forum.ts index 08f00db1..53cd61a2 100644 --- a/src/app_modules/forum/component/api_fetch_forum.ts +++ b/src/app_modules/forum/component/api_fetch_forum.ts @@ -3,6 +3,7 @@ export { apiGetOneForumById, apiGetForumkuByUserId as apiGetForumkuById, apiGetKomentarForumById, + apiGetMasterReportForum, }; const apiGetAllForum = async ({ @@ -150,3 +151,44 @@ const apiGetKomentarForumById = async ({ id , page}: { id: string , page: string throw error; // Re-throw the error to handle it in the calling function } }; + + +const apiGetMasterReportForum = async () => { + try { + // Fetch token from cookie + const { token } = await fetch("/api/get-cookie").then((res) => + res.json() + ); + if (!token) { + console.error("No token found"); + return null; + } + + const response = await fetch(`/api/forum/master`, { + method: "GET", + headers: { + "Content-Type": "application/json", + Accept: "application/json", + Authorization: `Bearer ${token}`, + }, + }); + + // Check if the response is OK + if (!response.ok) { + const errorData = await response.json().catch(() => null); + console.error( + "Failed to get all forum:", + response.statusText, + errorData + ); + throw new Error(errorData?.message || "Failed to get all forum"); + } + + // Return the JSON response + return await response.json(); + } catch (error) { + console.error("Error get all forum", error); + throw error; // Re-throw the error to handle it in the calling function + } + +} \ No newline at end of file diff --git a/src/app_modules/forum/fun/create/fun_create_report_posting.ts b/src/app_modules/forum/fun/create/fun_create_report_posting.ts index cffe83c5..db705a27 100644 --- a/src/app_modules/forum/fun/create/fun_create_report_posting.ts +++ b/src/app_modules/forum/fun/create/fun_create_report_posting.ts @@ -3,7 +3,6 @@ import prisma from "@/lib/prisma"; import { funGetUserIdByToken } from "@/app_modules/_global/fun/get"; - export async function forum_funCreateReportPosting({ postingId, kategoriId, @@ -11,18 +10,28 @@ export async function forum_funCreateReportPosting({ postingId: string; kategoriId: number; }) { + try { const userLoginId = await funGetUserIdByToken(); + if (!userLoginId) + return { status: 400, message: "Gagal menambahkan report posting!" }; + const createReport = await prisma.forum_ReportPosting.create({ + data: { + userId: userLoginId, + forum_PostingId: postingId, + forumMaster_KategoriReportId: kategoriId, + }, + }); - const createReport = await prisma.forum_ReportPosting.create({ - data: { - userId: userLoginId, - forum_PostingId: postingId, - forumMaster_KategoriReportId: kategoriId, - }, - }); + if (!createReport) + return { status: 400, message: "Gagal menambahkan report posting!" }; - if (!createReport) - return { status: 400, message: "Gagal menambahkan report posting!" }; - return { status: 201, message: "Berhasil me-report posting!" }; + return { status: 201, message: "Berhasil me-report posting!" }; + } catch (error) { + return { + status: 400, + message: "Error menambahkan report posting", + error: (error as Error).message, + }; + } } diff --git a/src/app_modules/forum/fun/create/fun_create_report_posting_lainnya.ts b/src/app_modules/forum/fun/create/fun_create_report_posting_lainnya.ts index 41506a2a..70dcb2df 100644 --- a/src/app_modules/forum/fun/create/fun_create_report_posting_lainnya.ts +++ b/src/app_modules/forum/fun/create/fun_create_report_posting_lainnya.ts @@ -3,20 +3,32 @@ import prisma from "@/lib/prisma"; import { funGetUserIdByToken } from "@/app_modules/_global/fun/get"; - export async function forum_funCreateReportPostingLainnya( postingId: string, deskripsi: string ) { - const userLoginId = await funGetUserIdByToken(); - const create = await prisma.forum_ReportPosting.create({ - data: { - forum_PostingId: postingId, - deskripsi: deskripsi, - userId: userLoginId, - }, - }); + try { + const userLoginId = await funGetUserIdByToken(); + if (!userLoginId) + return { status: 400, message: "Gagal menambah report !" }; - if (!create) return { status: 400, message: "Gagal menambah report !" }; - return { status: 201, message: "Berhasil menambah report !" }; + const create = await prisma.forum_ReportPosting.create({ + data: { + forum_PostingId: postingId, + deskripsi: deskripsi, + userId: userLoginId, + }, + }); + + if (!create) return { status: 400, message: "Gagal menambah report !" }; + + return { status: 201, message: "Berhasil menambah report !" }; + } catch (error) { + console.log(error); + return { + status: 500, + message: "Error API", + error: (error as Error).message, + }; + } } diff --git a/src/app_modules/forum/fun/master/get_master_kategori_report.ts b/src/app_modules/forum/fun/master/get_master_kategori_report.ts index 7d5db0fc..b10aeab5 100644 --- a/src/app_modules/forum/fun/master/get_master_kategori_report.ts +++ b/src/app_modules/forum/fun/master/get_master_kategori_report.ts @@ -4,8 +4,5 @@ import prisma from "@/lib/prisma"; export async function forum_getMasterKategoriReport() { const data = await prisma.forumMaster_KategoriReport.findMany({}); - - const changeType = JSON.stringify(data, null,2) - return data; } diff --git a/src/app_modules/forum/report/posting/index.tsx b/src/app_modules/forum/report/posting/index.tsx index fdd84c79..55c2d34b 100644 --- a/src/app_modules/forum/report/posting/index.tsx +++ b/src/app_modules/forum/report/posting/index.tsx @@ -11,23 +11,43 @@ import notifikasiToAdmin_funCreate from "@/app_modules/notifikasi/fun/create/cre import mqtt_client from "@/util/mqtt_client"; import { Button, Radio, Stack, Text, Title } from "@mantine/core"; import { toNumber } from "lodash"; -import { useRouter } from "next/navigation"; +import { useParams, useRouter } from "next/navigation"; import { useState } from "react"; import { forum_funCreateReportPosting } from "../../fun/create/fun_create_report_posting"; import forum_getOneKategoriById from "../../fun/get/get_one_kategori_by_id"; import { MODEL_FORUM_MASTER_REPORT } from "../../model/interface"; +import { useShallowEffect } from "@mantine/hooks"; +import { apiGetMasterReportForum } from "../../component/api_fetch_forum"; +import { clientLogger } from "@/util/clientLogger"; +import CustomSkeleton from "@/app_modules/components/CustomSkeleton"; export default function Forum_ReportPosting({ - postingId, - listReport, userLoginId, }: { - postingId: string; - listReport: MODEL_FORUM_MASTER_REPORT[]; userLoginId: string; }) { + const param = useParams<{ id: string }>(); + const postingId = param.id; + const [listReport, setListReport] = useState(); const [reportValue, setReportValue] = useState("1"); + useShallowEffect(() => { + handleLoadMasterReport(); + }, []); + + const handleLoadMasterReport = async () => { + try { + const response = await apiGetMasterReportForum(); + if (response.success) { + setListReport(response.data); + } + } catch (error) { + clientLogger.error("Error get master report", error); + } + }; + + if (!listReport) return ; + return ( <> (); + const postingId = param.id; const [deskripsi, setDeskripsi] = useState(""); + return ( <> @@ -50,50 +52,64 @@ function ButtonAction({ userLoginId: string; }) { const router = useRouter(); + const [isLoading, setIsLoading] = useState(false); async function onReport() { - const report = await forum_funCreateReportPostingLainnya( - postingId, - deskripsi - ); - if (report.status === 201) { - ComponentGlobal_NotifikasiBerhasil(report.message); - router.back(); + try { + setIsLoading(true); + const report = await forum_funCreateReportPostingLainnya( + postingId, + deskripsi + ); + if (report.status === 201) { + ComponentGlobal_NotifikasiBerhasil(report.message); + router.back(); - const dataNotif = { - appId: postingId, - pesan: deskripsi, - kategoriApp: "FORUM", - title: "Lainnya", - userId: userLoginId, - status: "Report Posting", - }; + const dataNotif = { + appId: postingId, + pesan: deskripsi, + kategoriApp: "FORUM", + title: "Lainnya", + userId: userLoginId, + status: "Report Posting", + }; - const createNotifikasi = await notifikasiToAdmin_funCreate({ - data: dataNotif as any, - }); + const createNotifikasi = await notifikasiToAdmin_funCreate({ + data: dataNotif as any, + }); - if (createNotifikasi.status === 201) { - mqtt_client.publish("ADMIN", JSON.stringify({ count: 1 })); + if (createNotifikasi.status === 201) { + mqtt_client.publish("ADMIN", JSON.stringify({ count: 1 })); + } + } else { + setIsLoading(false); + ComponentGlobal_NotifikasiGagal(report.message); } - } else { - ComponentGlobal_NotifikasiGagal(report.message); + } catch (error) { + setIsLoading(false); + clientLogger.error("Error create report posting", error); } } return ( <>