diff --git a/src/app/api/admin/collaboration/group/route.ts b/src/app/api/admin/collaboration/group/route.ts index 1e2eb335..283f0d64 100644 --- a/src/app/api/admin/collaboration/group/route.ts +++ b/src/app/api/admin/collaboration/group/route.ts @@ -4,136 +4,143 @@ import _ from "lodash"; import { NextResponse } from "next/server"; export async function GET(request: Request) { + const { searchParams } = new URL(request.url); + const search = searchParams.get("search"); + const page = searchParams.get("page"); + const takeData = 1; + const skipData = Number(page) * takeData - takeData; - 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; - try { - let fixData; - - if (!page) { - fixData = await prisma.projectCollaboration_RoomChat.findMany({ - orderBy: { - createdAt: "desc", - }, - where: { - isActive: true, - }, - select: { - id: true, - createdAt: true, - isActive: true, - name: true, - ProjectCollaboration_AnggotaRoomChat: { - select: { - User: { - select: { - id: true, - Profile: true, - }, - }, - }, - }, - ProjectCollaboration: { - select: { - id: true, - isActive: true, - title: true, - lokasi: true, - purpose: true, - benefit: true, - createdAt: true, - report: true, - Author: { - select: { - id: true, - Profile: { - select: { - name: true, - }, - }, - }, - }, - ProjectCollaborationMaster_Industri: true, - }, - }, - }, - }); - } else { - const data = await prisma.projectCollaboration_RoomChat.findMany({ - take: takeData, - skip: skipData, - orderBy: { - createdAt: "desc", - }, - where: { - isActive: true, - }, - select: { - id: true, - createdAt: true, - isActive: true, - name: true, - ProjectCollaboration_AnggotaRoomChat: { - select: { - User: { - select: { - id: true, - Profile: true, - }, - }, - }, - }, - ProjectCollaboration: { - select: { - id: true, - isActive: true, - title: true, - lokasi: true, - purpose: true, - benefit: true, - createdAt: true, - report: true, - Author: { - select: { - id: true, - Profile: { - select: { - name: true, - }, - }, - }, - }, - ProjectCollaborationMaster_Industri: true, - }, - }, - }, - }); - - const nCount = await prisma.projectCollaboration_RoomChat.count({ - where: { - isActive: true, - }, - }) - - fixData = { - data: data, - nPage: _.ceil(nCount / takeData) - } - } - - } catch (error) { - - backendLogger.error("Error get data collaboration group >>", error); - return NextResponse.json({ - success: false, - message: "Error get data collaboration group", - reason: (error as Error).message + if (!page) { + fixData = await prisma.projectCollaboration_RoomChat.findMany({ + orderBy: { + createdAt: "desc", }, - { status: 500 } - ) + where: { + isActive: true, + }, + select: { + id: true, + createdAt: true, + isActive: true, + name: true, + ProjectCollaboration_AnggotaRoomChat: { + select: { + User: { + select: { + id: true, + Profile: true, + }, + }, + }, + }, + ProjectCollaboration: { + select: { + id: true, + isActive: true, + title: true, + lokasi: true, + purpose: true, + benefit: true, + createdAt: true, + report: true, + Author: { + select: { + id: true, + Profile: { + select: { + name: true, + }, + }, + }, + }, + ProjectCollaborationMaster_Industri: true, + }, + }, + }, + }); + } else { + const data = await prisma.projectCollaboration_RoomChat.findMany({ + take: takeData, + skip: skipData, + orderBy: { + createdAt: "desc", + }, + where: { + isActive: true, + }, + select: { + id: true, + createdAt: true, + isActive: true, + name: true, + ProjectCollaboration_AnggotaRoomChat: { + select: { + User: { + select: { + id: true, + Profile: true, + }, + }, + }, + }, + ProjectCollaboration: { + select: { + id: true, + isActive: true, + title: true, + lokasi: true, + purpose: true, + benefit: true, + createdAt: true, + report: true, + Author: { + select: { + id: true, + Profile: { + select: { + name: true, + }, + }, + }, + }, + ProjectCollaborationMaster_Industri: true, + }, + }, + }, + }); + + const nCount = await prisma.projectCollaboration_RoomChat.count({ + where: { + isActive: true, + }, + }); + + fixData = { + data: data, + nPage: _.ceil(nCount / takeData), + }; } -} \ No newline at end of file + + return NextResponse.json( + { + success: true, + message: "Success get data collaboration group", + data: fixData, + }, + { status: 200 } + ); + } catch (error) { + backendLogger.error("Error get data collaboration group >>", error); + return NextResponse.json( + { + success: false, + message: "Error get data collaboration group", + reason: (error as Error).message, + }, + { status: 500 } + ); + } +} diff --git a/src/app/dev/admin/colab/sub-menu/group/page.tsx b/src/app/dev/admin/colab/sub-menu/group/page.tsx index 8b499f57..02695ada 100644 --- a/src/app/dev/admin/colab/sub-menu/group/page.tsx +++ b/src/app/dev/admin/colab/sub-menu/group/page.tsx @@ -6,7 +6,7 @@ export default async function Page() { return ( <> - + ); } diff --git a/src/app_modules/admin/colab/lib/api_fetch_admin_collaboration.ts b/src/app_modules/admin/colab/lib/api_fetch_admin_collaboration.ts index 57765d71..e7907b83 100644 --- a/src/app_modules/admin/colab/lib/api_fetch_admin_collaboration.ts +++ b/src/app_modules/admin/colab/lib/api_fetch_admin_collaboration.ts @@ -71,7 +71,7 @@ const apiGetAdminCollaborationRoomById = async ({ search, }: { page: string; - search: string; + search?: string; }) => { const { token } = await fetch("/api/get-cookie").then((res) => res.json()); if (!token) return await token.json().catch(() => null); diff --git a/src/app_modules/admin/colab/sub_menu/group.tsx b/src/app_modules/admin/colab/sub_menu/group.tsx index 9a64c93c..9840cbea 100644 --- a/src/app_modules/admin/colab/sub_menu/group.tsx +++ b/src/app_modules/admin/colab/sub_menu/group.tsx @@ -14,8 +14,9 @@ import { Stack, Table, Text, - Title + Title, } from "@mantine/core"; +import { useShallowEffect } from "@mantine/hooks"; import { IconCircleDot } from "@tabler/icons-react"; import { useState } from "react"; import { ComponentAdminGlobal_TitlePage } from "../../_admin_global/_component"; @@ -23,28 +24,25 @@ import Admin_DetailButton from "../../_admin_global/_component/button/detail_but import ComponentAdminGlobal_HeaderTamplate from "../../_admin_global/header_tamplate"; import { Admin_V3_ComponentPaginationBreakpoint } from "../../_components_v3/comp_pagination_breakpoint"; import ComponentAdminColab_DetailData from "../component/detail_data"; -import adminColab_getListAllGroupChat from "../fun/get/get_list_all_group_chat"; import adminColab_getOneRoomChatById from "../fun/get/get_one_room_chat_by_id"; +import { apiGetAdminCollaborationRoomById } from "../lib/api_fetch_admin_collaboration"; +import CustomSkeleton from "@/app_modules/components/CustomSkeleton"; -export default function AdminColab_TableGroup({ - listGroup, -}: { - listGroup: any; -}) { +export default function AdminColab_TableGroup() { return ( <> - + ); } -function TableMenu({ listGroup }: { listGroup: any }) { - const [data, setData] = useState( - listGroup.data +function TableMenu() { + const [data, setData] = useState( + null ); - const [isNPage, setNPage] = useState(listGroup.nPage); + const [isNPage, setNPage] = useState(1); const [activePage, setActivePage] = useState(1); const [idProject, setIdProject] = useState(""); @@ -52,14 +50,37 @@ function TableMenu({ listGroup }: { listGroup: any }) { const [loadingDetail, setLoadingDetail] = useState(false); const [detailData, setDetailData] = useState(); + useShallowEffect(() => { + onLoadData(); + }, [activePage]); + + async function onLoadData() { + try { + const response = await apiGetAdminCollaborationRoomById({ + page: `${activePage}`, + }); + + if (response?.success && response?.data?.data) { + setData(response.data.data); + setNPage(response.data.nPage || 1); + } else { + console.error("Invalid data format recieved", response); + setData([]); + } + } catch (error) { + console.error("Error get data table reject", error); + setData([]); + } + } + // PAGINATION dan No awal data di tampilkan let noAwal = activePage * 5 - 4; - async function onLoad(pindahPage: any) { - const load = await adminColab_getListAllGroupChat({ page: pindahPage }); - setActivePage(pindahPage); - setData(load.data as any); - setNPage(load.nPage); - } + // async function onLoad(pindahPage: any) { + // const load = await adminColab_getListAllGroupChat({ page: pindahPage }); + // setActivePage(pindahPage); + // setData(load.data as any); + // setNPage(load.nPage); + // } async function onDetailData(roomId: string) { setLoadingDetail(true); @@ -75,46 +96,63 @@ function TableMenu({ listGroup }: { listGroup: any }) { }); } - const tableRow = data.map((e, i) => ( - - {/* + const tableRow = () => { + if (!Array.isArray(data) || data.length === 0) { + return ( + + +
+ Tidak ada data +
+ + + ); + } + + return data.map((e, i) => ( + + {/*
{noAwal++}
*/} - - - - {e?.ProjectCollaboration?.Author?.Profile?.name} - - - - - - {e?.name} - - - - - - {e?.ProjectCollaboration?.ProjectCollaborationMaster_Industri?.name} - - - - -
- {e?.ProjectCollaboration_AnggotaRoomChat.length} -
- - -
- - - -
- - - )); + + + + {e?.ProjectCollaboration?.Author?.Profile?.name} + + + + + + {e?.name} + + + + + + { + e?.ProjectCollaboration?.ProjectCollaborationMaster_Industri + ?.name + } + + + + +
+ {e?.ProjectCollaboration_AnggotaRoomChat.length} +
+ + +
+ + + +
+ + + )); + }; return ( <> @@ -124,44 +162,49 @@ function TableMenu({ listGroup }: { listGroup: any }) { color={AdminColor.softBlue} component={<>} /> - - - - - - - {/* + + + + + + + {tableRow()} +
+ + {!data ? ( + + ) : ( + + + + + + + {/* */} - - - - - - - - {tableRow} -
No
- Admin Room - - Nama Group - - Industri - -
Anggota Group
-
-
Aksi
-
-
- { - onLoad(val); - }} - /> -
-
+
+ Admin Room + + Nama Group + + Industri + +
Anggota Group
+
+
Aksi
+
+
+ { + setActivePage(val); + }} + /> +
+
+ )}