From e97fd9807740b5f81397dbd4e584f55b161b42ad Mon Sep 17 00:00:00 2001 From: Bagasbanuna02 Date: Wed, 16 Apr 2025 20:39:40 +0800 Subject: [PATCH] fix responsive admin forum --- .../api/admin/forum/[id]/komentar/route.ts | 101 +++++++ src/app/dev/admin/forum/detail/[id]/page.tsx | 16 +- .../_admin_global/_component/title_page.tsx | 2 +- .../comp_simple_grid_breakpoint.tsx | 10 +- .../admin/forum/component/button_delete.tsx | 49 ++-- .../forum/component/detail_one_posting.tsx | 42 ++- .../admin/forum/detail/detail_posting.tsx | 272 ++++++++++-------- .../admin/forum/lib/api_fetch_admin_forum.ts | 53 ++++ .../admin/forum/sub_menu/table_posting.tsx | 55 ++-- 9 files changed, 396 insertions(+), 204 deletions(-) create mode 100644 src/app/api/admin/forum/[id]/komentar/route.ts diff --git a/src/app/api/admin/forum/[id]/komentar/route.ts b/src/app/api/admin/forum/[id]/komentar/route.ts new file mode 100644 index 00000000..dd43e628 --- /dev/null +++ b/src/app/api/admin/forum/[id]/komentar/route.ts @@ -0,0 +1,101 @@ +import _ from "lodash"; +import { NextResponse } from "next/server"; + +export async function GET( + request: Request, + { params }: { params: { id: string } } +) { + const { searchParams } = new URL(request.url); + const search = searchParams.get("search"); + const page = searchParams.get("page"); + const takeData = 10; + const skipData = Number(page) * takeData - takeData; + + try { + let fixData; + const { id } = params; + const postingId = id; + + if (!page) { + fixData = await prisma.forum_Komentar.findMany({ + orderBy: { + createdAt: "desc", + }, + where: { + forum_PostingId: postingId, + isActive: true, + komentar: { + contains: search ?? "", + mode: "insensitive", + }, + }, + include: { + Forum_ReportKomentar: true, + Author: { + select: { + username: true + } + } + + }, + }); + } else { + const data = await prisma.forum_Komentar.findMany({ + take: takeData, + skip: skipData, + orderBy: { + createdAt: "desc", + }, + where: { + forum_PostingId: postingId, + isActive: true, + komentar: { + contains: search ?? "", + mode: "insensitive", + }, + }, + include: { + Forum_ReportKomentar: true, + Author: { + select: { + username: true + } + } + + }, + }); + + const nCount = await prisma.forum_Komentar.count({ + where: { + forum_PostingId: postingId, + isActive: true, + komentar: { + contains: search ?? "", + mode: "insensitive", + }, + }, + }); + + fixData = { + data: data, + nPage: _.ceil(nCount / takeData), + }; + } + + return NextResponse.json({ + success: true, + message: "Berhasil mendapatkan data komentar", + data: fixData, + }); + } catch (error) { + console.log("Error get data komentar", error); + return NextResponse.json( + { + status: false, + message: "Gagal mendapatkan data komentar", + error: error || (error as Error).message, + }, + { status: 500 } + ); + } +} diff --git a/src/app/dev/admin/forum/detail/[id]/page.tsx b/src/app/dev/admin/forum/detail/[id]/page.tsx index 69063651..8bc4ed4a 100644 --- a/src/app/dev/admin/forum/detail/[id]/page.tsx +++ b/src/app/dev/admin/forum/detail/[id]/page.tsx @@ -6,20 +6,20 @@ import { adminForum_getOnePostingById } from "@/app_modules/admin/forum/fun/get/ export default async function Page({ params }: { params: { id: string } }) { let postingId = params.id; - const listKomentar = await adminForum_getListKomentarById({ - postingId: postingId, - page: 1, - }); + // const listKomentar = await adminForum_getListKomentarById({ + // postingId: postingId, + // page: 1, + // }); const dataPosting = await adminForum_getOnePostingById(postingId); - const countKomentar = await adminForum_countKomentarByPostingId({postingId: postingId}) - + // const countKomentar = await adminForum_countKomentarByPostingId({ + // postingId: postingId, + // }); return ( <> ); diff --git a/src/app_modules/admin/_admin_global/_component/title_page.tsx b/src/app_modules/admin/_admin_global/_component/title_page.tsx index 804666de..49070859 100644 --- a/src/app_modules/admin/_admin_global/_component/title_page.tsx +++ b/src/app_modules/admin/_admin_global/_component/title_page.tsx @@ -10,7 +10,7 @@ export function ComponentAdminGlobal_TitlePage({ color, component, }: { - name: string; + name: React.ReactNode | string color?: string; component?: React.ReactNode; }) { diff --git a/src/app_modules/admin/_components_v3/comp_simple_grid_breakpoint.tsx b/src/app_modules/admin/_components_v3/comp_simple_grid_breakpoint.tsx index 976a97de..7d1883b0 100644 --- a/src/app_modules/admin/_components_v3/comp_simple_grid_breakpoint.tsx +++ b/src/app_modules/admin/_components_v3/comp_simple_grid_breakpoint.tsx @@ -6,21 +6,23 @@ export function Admin_V3_ComponentBreakpoint({ sm, md, lg, + allCols, }: { children: React.ReactNode; cols?: number; sm?: number; md?: number; lg?: number; + allCols?: number; }) { return ( <> { - if (res.status === 200) { - setLoadingDel2(false); - close(); - ComponentGlobal_NotifikasiBerhasil(res.message); - onSuccesDelete(true); - } else { - ComponentGlobal_NotifikasiGagal(res.message); - } - }); + try { + setLoadingDel2(true); + + await adminForum_funDeletePostingById(postingId).then((res) => { + if (res.status === 200) { + setLoadingDel2(false); + ComponentAdminGlobal_NotifikasiBerhasil(res.message); + onSuccesDelete(true); + } else { + ComponentAdminGlobal_NotifikasiPeringatan(res.message); + } + }); + } catch (error) { + console.log("error delete", error); + ComponentAdminGlobal_NotifikasiGagal( + "Terjadi kesalahan, silahkan coba lagi" + ); + } finally { + close(); + setLoadingDel2(false); + } } return ( <> - - Anda yakin menghapus posting ini + + Anda yakin menghapus posting ini ? + - + - - - - - )); + const rowTable = () => { + if (!Array.isArray(data) || data.length === 0) { + return ( + + +
+ Tidak ada data +
+ + + ); + } + + return data?.map((e, i) => ( + + + + {e?.Author?.username} + + + + + +
+ + + + + + {moment(e?.createdAt).format("DD-MM-YYYY")} + + + +
+ = 3 ? "red" : AdminColor.white + } + fw={"bold"} + fz={"lg"} + > + {e?.Forum_ReportKomentar.length} + +
+ + + + + + + + + )); + }; return ( <> - - - - Komentar - - - {`(${countKomentar})`} - - - } - radius={"xl"} - placeholder="Cari komentar" - onChange={(val) => { - onSearch(val.currentTarget.value); - }} - /> - + + + Komentar: + + + {data?.length} + + + } + component={ + } + radius={"xl"} + placeholder="Cari komentar" + onChange={(val) => { + onSearch(val.currentTarget.value); + }} + /> + } + /> - {_.isEmpty(data) ? ( - + {!data ? ( + ) : ( @@ -207,39 +232,36 @@ function TableKomentar({ p={"md"} w={"100%"} h={"100%"} - > -
Username
+ Username -
Komentar
+ Komentar -
Tgl Komentar
+ Tgl Komentar -
Total Report
+
Total Report
-
Aksi
+
Aksi
- {rowTable} + {rowTable()}
-
- { - onPageClick(val); - }} - /> -
+ { + onPageClick(val); + }} + />
)}
diff --git a/src/app_modules/admin/forum/lib/api_fetch_admin_forum.ts b/src/app_modules/admin/forum/lib/api_fetch_admin_forum.ts index 3c6f5367..2a549e27 100644 --- a/src/app_modules/admin/forum/lib/api_fetch_admin_forum.ts +++ b/src/app_modules/admin/forum/lib/api_fetch_admin_forum.ts @@ -6,6 +6,7 @@ export { apiGetAdminForumReportKomentar, apiGetAdminForumPublish, apiGetAdminHasilReportPosting, + apiAdminGetKomentarForumById, }; const apiGetAdminForumPublishCountDasboard = async () => { @@ -159,3 +160,55 @@ const apiGetAdminHasilReportPosting = async ({ return await response.json().catch(() => null); }; + +const apiAdminGetKomentarForumById = async ({ + id, + page, + search, +}: { + id: string; + page?: string; + search?: string; +}) => { + try { + const { token } = await fetch("/api/get-cookie").then((res) => res.json()); + if (!token) { + console.error("No token found"); + return null; + } + + const isPage = page ? `?page=${page}` : ""; + const isSearch = search ? `&search=${search}` : ""; + const response = await fetch( + `/api/admin/forum/${id}/komentar${isPage}${isSearch}`, + { + 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 admin komentar forum", + response.statusText, + errorData + ); + throw new Error( + errorData?.message || "Failed to get admin komentar forum" + ); + } + + // Return the JSON response + const resulst = await response.json(); + return resulst; + } catch (error) { + console.error("Error get admin komentar forum", error); + throw error; // Re-throw the error to handle it in the calling function + } +}; diff --git a/src/app_modules/admin/forum/sub_menu/table_posting.tsx b/src/app_modules/admin/forum/sub_menu/table_posting.tsx index dd66322f..0eeec292 100644 --- a/src/app_modules/admin/forum/sub_menu/table_posting.tsx +++ b/src/app_modules/admin/forum/sub_menu/table_posting.tsx @@ -96,20 +96,17 @@ function TablePublish() { ); } return data?.map((e, i) => ( - - {/* Aksi */} + + {/* Author */} - - - { - onDelete(val); - }} - /> - + + {e?.Author?.username} + {/* Status */} @@ -127,15 +124,6 @@ function TablePublish() { - {/* Author */} - - - - {e?.Author?.username} - - - - {/* Deskripsi */} @@ -176,6 +164,19 @@ function TablePublish() { + {/* Aksi */} + + + + { + onDelete(val); + }} + /> + + + {/* @@ -222,15 +223,11 @@ function TablePublish() { -
Aksi
+ Username -
Status
- - Username - Postingan @@ -240,6 +237,10 @@ function TablePublish() {
Total Report Posting
+ + +
Aksi
+ {renderTableBody()}