From a6a27c239a63db7965aac8de4f5088b7cb519911 Mon Sep 17 00:00:00 2001 From: Bagasbanuna02 Date: Mon, 24 Feb 2025 11:12:28 +0800 Subject: [PATCH] fix admin deskripsi: - fix api nomor admin --- src/app/api/master/admin-contact/route.ts | 32 +++ .../dev/admin/app-information/main/page.tsx | 2 - .../_global/lib/api_fetch_master.ts | 62 ++++- .../app_info/fun/update/fun_update_nomor.ts | 28 ++- src/app_modules/admin/app_info/ui/ui_main.tsx | 4 +- .../app_info/view/view_info_whatsapp.tsx | 224 +++++++++++------- src/app_modules/auth/_lib/api_fetch_auth.ts | 2 +- 7 files changed, 237 insertions(+), 117 deletions(-) create mode 100644 src/app/api/master/admin-contact/route.ts diff --git a/src/app/api/master/admin-contact/route.ts b/src/app/api/master/admin-contact/route.ts new file mode 100644 index 00000000..7c6f4f55 --- /dev/null +++ b/src/app/api/master/admin-contact/route.ts @@ -0,0 +1,32 @@ +import backendLogger from "@/util/backendLogger"; +import { NextResponse } from "next/server"; +import prisma from "@/lib/prisma"; + +export async function GET(request: Request) { + try { + const data = await prisma.nomorAdmin.findFirst({ + where: { + isActive: true, + }, + }); + + return NextResponse.json( + { + success: true, + message: "Success get admin contact", + data: data, + }, + { status: 200 } + ); + } catch (error) { + backendLogger.error("Error get admin contact", error); + return NextResponse.json( + { + success: false, + message: "Error get admin contact", + reason: (error as Error).message, + }, + { status: 500 } + ); + } +} diff --git a/src/app/dev/admin/app-information/main/page.tsx b/src/app/dev/admin/app-information/main/page.tsx index 42c3b5de..b163150f 100644 --- a/src/app/dev/admin/app-information/main/page.tsx +++ b/src/app/dev/admin/app-information/main/page.tsx @@ -4,14 +4,12 @@ import adminAppInformation_getNomorAdmin from "@/app_modules/admin/app_info/fun/ import { AdminAppInformation_UiMain } from "@/app_modules/admin/app_info/ui"; export default async function Page() { - const nomorAdmin = await adminAppInformation_getNomorAdmin(); const listBank = await adminAppInformation_getMasterBank(); const dataBidangBisnis = await adminAppInformation_funGetBidangBisnis() return ( <> diff --git a/src/app_modules/_global/lib/api_fetch_master.ts b/src/app_modules/_global/lib/api_fetch_master.ts index 404f7094..4c1e5743 100644 --- a/src/app_modules/_global/lib/api_fetch_master.ts +++ b/src/app_modules/_global/lib/api_fetch_master.ts @@ -1,4 +1,9 @@ -export { apiGetMasterBank, apiGetMasterBidangBisnis, apiGetMasterStatusTransaksi }; +export { + apiGetMasterBank, + apiGetMasterBidangBisnis, + apiGetMasterStatusTransaksi, + apiGetAdminContact, +}; const apiGetMasterBank = async () => { const { token } = await fetch("/api/get-cookie").then((res) => res.json()); @@ -37,14 +42,51 @@ const apiGetMasterStatusTransaksi = async () => { if (!token) return await token.json().catch(() => null); const response = await fetch(`/api/master/status_transaksi`, { + method: "GET", + headers: { + "Content-Type": "application/json", + Accept: "application/json", + "Access-Control-Allow-Origin": "*", + Authorization: `Bearer ${token}`, + }, + }); + + return await response.json().catch(() => null); +}; + +const apiGetAdminContact = 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; + } + + // Send PUT request to update portfolio logo + const response = await fetch(`/api/master/admin-contact`, { method: "GET", headers: { - "Content-Type": "application/json", - Accept: "application/json", - "Access-Control-Allow-Origin": "*", - Authorization: `Bearer ${token}`, - } - }) - - return await response.json().catch(() => null); -} \ No newline at end of file + "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( + "Error get admin contact:", + errorData?.message || "Unknown error" + ); + + return null; + } + + return await response.json(); + } catch (error) { + console.error("Error get admin contact:", error); + throw error; // Re-throw the error to handle it in the calling function + } +}; diff --git a/src/app_modules/admin/app_info/fun/update/fun_update_nomor.ts b/src/app_modules/admin/app_info/fun/update/fun_update_nomor.ts index c2ea5a8f..8d4334aa 100644 --- a/src/app_modules/admin/app_info/fun/update/fun_update_nomor.ts +++ b/src/app_modules/admin/app_info/fun/update/fun_update_nomor.ts @@ -7,15 +7,23 @@ export default async function adminAppInformation_funUpdateNomorAdmin({ }: { data: any; }) { - const updt = await prisma.nomorAdmin.update({ - where: { - id: data.id, - }, - data: { - nomor: data.nomor, - }, - }); + try { + const updt = await prisma.nomorAdmin.update({ + where: { + id: data.id, + }, + data: { + nomor: data.nomor, + }, + }); - if (!updt) return { status: 400, message: "Gagal update" }; - return { status: 200, message: "Berhasil update" }; + if (!updt) return { status: 400, message: "Gagal update" }; + return { status: 200, message: "Berhasil update" }; + } catch (error) { + return { + status: 500, + message: "Error update", + error: (error as Error).message, + }; + } } diff --git a/src/app_modules/admin/app_info/ui/ui_main.tsx b/src/app_modules/admin/app_info/ui/ui_main.tsx index e682dc73..9a6a2759 100644 --- a/src/app_modules/admin/app_info/ui/ui_main.tsx +++ b/src/app_modules/admin/app_info/ui/ui_main.tsx @@ -12,11 +12,9 @@ import { import { AccentColor, AdminColor, MainColor } from "@/app_modules/_global/color/color_pallet"; export default function AdminAppInformation_UiMain({ - nomorAdmin, listBank, dataBidangBisnis, }: { - nomorAdmin: any; listBank: any[]; dataBidangBisnis: any[]; }) { @@ -61,7 +59,7 @@ export default function AdminAppInformation_UiMain({ {selectPage === "1" && ( - + )} {selectPage === "2" && ( diff --git a/src/app_modules/admin/app_info/view/view_info_whatsapp.tsx b/src/app_modules/admin/app_info/view/view_info_whatsapp.tsx index 00bb9129..893b2a7c 100644 --- a/src/app_modules/admin/app_info/view/view_info_whatsapp.tsx +++ b/src/app_modules/admin/app_info/view/view_info_whatsapp.tsx @@ -1,9 +1,17 @@ " use client"; +import { + AccentColor, + AdminColor, +} from "@/app_modules/_global/color/color_pallet"; +import { apiGetAdminContact } from "@/app_modules/_global/lib/api_fetch_master"; +import CustomSkeleton from "@/app_modules/components/CustomSkeleton"; +import { clientLogger } from "@/util/clientLogger"; import { ActionIcon, Button, Collapse, + Grid, Group, Paper, Stack, @@ -11,41 +19,57 @@ import { Title, Tooltip, } from "@mantine/core"; +import { useDisclosure, useShallowEffect } from "@mantine/hooks"; import { IconEdit, IconPhone } from "@tabler/icons-react"; import { useState } from "react"; import { ComponentAdminGlobal_NotifikasiBerhasil } from "../../_admin_global/admin_notifikasi/notifikasi_berhasil"; import { ComponentAdminGlobal_NotifikasiGagal } from "../../_admin_global/admin_notifikasi/notifikasi_gagal"; -import adminAppInformation_getNomorAdmin from "../fun/master/get_nomor_admin"; import adminAppInformation_funUpdateNomorAdmin from "../fun/update/fun_update_nomor"; -import { useDisclosure } from "@mantine/hooks"; -import { AccentColor, AdminColor, MainColor } from "@/app_modules/_global/color/color_pallet"; -export default function AdminAppInformation_ViewInformasiWhatApps({ - nomorAdmin, -}: { - nomorAdmin: any; -}) { - const [dataNomor, setDataNomor] = useState(nomorAdmin); +export default function AdminAppInformation_ViewInformasiWhatApps() { + const [dataNomor, setDataNomor] = useState(null); const [updateNomor, setUpdateNomor] = useState(""); const [opened, { toggle }] = useDisclosure(false); - async function onUpdate() { - const newNumber = (dataNomor.nomor = updateNomor); - setDataNomor({ - ...dataNomor, - nomor: newNumber, - }); + useShallowEffect(() => { + handleLoadData(); + }, []); - const updt = await adminAppInformation_funUpdateNomorAdmin({ - data: dataNomor, - }); - if (updt.status === 200) { - const loadDdata = await adminAppInformation_getNomorAdmin(); - setDataNomor(loadDdata); - toggle(); - ComponentAdminGlobal_NotifikasiBerhasil(updt.message); - } else { - ComponentAdminGlobal_NotifikasiGagal(updt.message); + const handleLoadData = async () => { + try { + const response = await apiGetAdminContact(); + + if (response) { + setDataNomor(response.data); + } else { + setDataNomor(""); + } + } catch (error) { + clientLogger.error("Error get admin contact", error); + setDataNomor(""); + } + }; + + async function onUpdate() { + try { + const newNumber = (dataNomor.nomor = updateNomor); + setDataNomor({ + ...dataNomor, + nomor: newNumber, + }); + + const updt = await adminAppInformation_funUpdateNomorAdmin({ + data: dataNomor, + }); + if (updt.status === 200) { + handleLoadData(); + toggle(); + ComponentAdminGlobal_NotifikasiBerhasil(updt.message); + } else { + ComponentAdminGlobal_NotifikasiGagal(updt.message); + } + } catch (error) { + clientLogger.error("Error update nomor admin", error); } } @@ -59,78 +83,96 @@ export default function AdminAppInformation_ViewInformasiWhatApps({ p={"xs"} style={{ borderRadius: "6px" }} > - Informasi WhatsApp + + Informasi WhatsApp + - - - - - {`+${dataNomor.nomor}`} - - { - toggle(); - setUpdateNomor(dataNomor.nomor); - }} + + + {!dataNomor ? ( + + ) : ( + + + - - - - - + + {`+${dataNomor?.nomor}`} + + { + toggle(); + setUpdateNomor(dataNomor?.nomor); + }} + > + + + + + - - - } - value={updateNomor} - label={Nomor Aktif Admin} - onChange={(val) => { - setUpdateNomor(val.currentTarget.value); - }} - /> - - - - - - - - + + } + value={updateNomor} + label={ + + Nomor Aktif Admin + + } + onChange={(val) => { + setUpdateNomor(val.currentTarget.value); + }} + /> + + + + + + + + + )} + + ); diff --git a/src/app_modules/auth/_lib/api_fetch_auth.ts b/src/app_modules/auth/_lib/api_fetch_auth.ts index 2e859a41..0bd42e65 100644 --- a/src/app_modules/auth/_lib/api_fetch_auth.ts +++ b/src/app_modules/auth/_lib/api_fetch_auth.ts @@ -37,7 +37,7 @@ const apiPostVerifikasiCodeOtp = async ({ nomor }: { nomor: string }) => { }; const apiDeleteAktivasiKodeOtpByNomor = async ({ id }: { id: string }) => { - const respone = await fetch(`/api/auth/code/${id}/peserta`, { + const respone = await fetch(`/api/auth/code/${id}`, { method: "DELETE", headers: { "Content-Type": "application/json",