diff --git a/src/app/api/image/delete/route.ts b/src/app/api/image/delete/route.ts index dcf3f89b..42d360e3 100644 --- a/src/app/api/image/delete/route.ts +++ b/src/app/api/image/delete/route.ts @@ -4,8 +4,6 @@ import { NextResponse } from "next/server"; export async function DELETE(req: Request) { const data = await req.json(); - - console.log("data request =>", data); const id = data.fileId; const dirId = data.dirId; diff --git a/src/app/api/new/investasi/route.ts b/src/app/api/new/investasi/route.ts index ea13150c..3f7a3592 100644 --- a/src/app/api/new/investasi/route.ts +++ b/src/app/api/new/investasi/route.ts @@ -14,7 +14,8 @@ export async function GET(request: Request) { const kategori = searchParams.get("cat") const status = searchParams.get("status") const page = searchParams.get("page") - const dataSkip = Number(page) * 5 - 5; + const dataTake = 10 + const dataSkip = Number(page) * dataTake - dataTake; if (kategori == "bursa") { const data = await prisma.investasi.findMany({ @@ -61,7 +62,7 @@ export async function GET(request: Request) { // cek data yang lewat // klo ada, update status const dataAwal = await prisma.investasi.findMany({ - take: 5, + take: dataTake, skip: dataSkip, orderBy: [ { @@ -100,28 +101,28 @@ export async function GET(request: Request) { } const data = await prisma.investasi.findMany({ - take: 5, - skip: dataSkip, - orderBy: { - updatedAt: "desc", - }, - where: { - authorId: userLoginId, - masterStatusInvestasiId: status, - }, - select: { - id: true, - title: true, - targetDana: true, - imageId: true, - countDown: true, - updatedAt: true, - MasterPencarianInvestor: { - select: { - name: true - } - } - } + take: dataTake, + skip: dataSkip, + orderBy: { + updatedAt: "desc", + }, + where: { + authorId: userLoginId, + masterStatusInvestasiId: status, + }, + select: { + id: true, + title: true, + targetDana: true, + imageId: true, + countDown: true, + updatedAt: true, + MasterPencarianInvestor: { + select: { + name: true, + }, + }, + }, }); dataFix = data.map((v: any) => ({ diff --git a/src/app/dev/katalog/[id]/layout.tsx b/src/app/dev/katalog/[id]/layout.tsx index 98b718f6..79911004 100644 --- a/src/app/dev/katalog/[id]/layout.tsx +++ b/src/app/dev/katalog/[id]/layout.tsx @@ -1,24 +1,12 @@ +import { funGetUserIdByToken } from "@/app_modules/_global/fun/get"; import { LayoutKatalogNew } from "@/app_modules/katalog/main"; -export default async function Layout({ children, params, }: { children: any; params: { id: string } }) { - // const profileId = params.id; - // const dataProfile = await Profile_getOneProfileAndUserById(profileId); - // const authorId = dataProfile?.userId; - - // const userLoginId = await funGetUserIdByToken(); - // const userRoleId = dataProfile?.User?.masterUserRoleId; +export default async function Layout({ children }: { children: any }) { + const userLoginId = await funGetUserIdByToken(); return ( <> - {/* - {children} - */} - {children} + {children} ); } diff --git a/src/app/dev/portofolio/main/[id]/layout.tsx b/src/app/dev/portofolio/main/[id]/layout.tsx index 1be53c84..b6c68e76 100644 --- a/src/app/dev/portofolio/main/[id]/layout.tsx +++ b/src/app/dev/portofolio/main/[id]/layout.tsx @@ -1,20 +1,14 @@ +import { funGetUserIdByToken } from "@/app_modules/_global/fun/get"; import { PortofolioLayoutNew } from "@/app_modules/katalog/portofolio"; -export default async function Layout({ children, params, }: { children: any; params: { id: string }; }) { - // let portoId = params.id; - // const getPorto = await portofolio_getOneById(portoId); - // const userLoginId = await funGetUserIdByToken(); +export default async function Layout({ children, }: { children: any; }) { + const userLoginId = await funGetUserIdByToken(); return ( <> - {/* + {children} - */} - {children} + ); } diff --git a/src/app/lib/id-derectory.ts b/src/app/lib/id-derectory.ts index dc430470..f2c1303f 100644 --- a/src/app/lib/id-derectory.ts +++ b/src/app/lib/id-derectory.ts @@ -1,6 +1,6 @@ const DIRECTORY_ID = { - profile_foto: "cm0x93rgo000jbp5tj8baoaus", - profile_background: "cm0x93ze8000lbp5t1a8uc9wl", + profile_foto: "cm5ni43ub001pxpug0qw4p11e", + profile_background: "cm5ni4hnq001l12p9gpagxgtv", portofolio_logo: "cm0yjl6ug000310njwmk6j0tx", map_pin: "cm0yjq8up000710njv5klra32", map_image: "cm0yjqnxl000910njplqho07w", diff --git a/src/app_modules/_global/fun/delete/fun_delete_file_by_id.tsx b/src/app_modules/_global/fun/delete/fun_delete_file_by_id.tsx index cb3d48be..27e9c8a0 100644 --- a/src/app_modules/_global/fun/delete/fun_delete_file_by_id.tsx +++ b/src/app_modules/_global/fun/delete/fun_delete_file_by_id.tsx @@ -22,29 +22,7 @@ export async function funGlobal_DeleteFileById({ return { success: false, message: data.message }; } } catch (error) { - console.error("Upload error:", error); + clientLogger.error("Upload error:", error); return { success: false, message: "An unexpected error occurred" }; } - // try { - // const res = await fetch( - // `https://wibu-storage.wibudev.com/api/files/${fileId}/delete`, - // { - // method: "DELETE", - // headers: { - // Authorization: `Bearer ${process.env.WS_APIKEY}`, - // }, - // } - // ); - - // if (res.ok) { - // const hasil = await res.json(); - // return { success: true, message: "File berhasil dihapus" }; - // } else { - // const errorText = await res.json(); - // return { success: false, message: errorText.message }; - // } - // } catch (error) { - // console.error("Upload error:", error); - // return { success: false, message: "An unexpected error occurred" }; - // } } diff --git a/src/app_modules/donasi/lib/api_donasi.ts b/src/app_modules/donasi/lib/api_donasi.ts index a04b7563..c46cfc0b 100644 --- a/src/app_modules/donasi/lib/api_donasi.ts +++ b/src/app_modules/donasi/lib/api_donasi.ts @@ -1,19 +1,59 @@ export const apiGetAllDonasi = async (path?: string) => { - const response = await fetch(`/api/new/donasi${(path) ? path : ''}`) - return await response.json().catch(() => null) -} + const { token } = await fetch("/api/get-cookie").then((res) => res.json()); + if (!token) return await token.json().catch(() => null); + + const response = await fetch(`/api/new/donasi${path ? path : ""}`, { + headers: { + "Content-Type": "application/json", + Accept: "application/json", + "Access-Control-Allow-Origin": "*", + Authorization: `Bearer ${token}`, + }, + }); + return await response.json().catch(() => null); +}; export const apiGetMasterDonasi = async (path?: string) => { - const response = await fetch(`/api/new/donasi/master${(path) ? path : ''}`) - return await response.json().catch(() => null) -} + const { token } = await fetch("/api/get-cookie").then((res) => res.json()); + if (!token) return await token.json().catch(() => null); + + const response = await fetch(`/api/new/donasi/master${path ? path : ""}`, { + headers: { + "Content-Type": "application/json", + Accept: "application/json", + "Access-Control-Allow-Origin": "*", + Authorization: `Bearer ${token}`, + }, + }); + return await response.json().catch(() => null); +}; export const apiGetAllDonasiSaya = async (path?: string) => { - const response = await fetch(`/api/new/donasi/invoice${(path) ? path : ''}`) - return await response.json().catch(() => null) -} + const { token } = await fetch("/api/get-cookie").then((res) => res.json()); + if (!token) return await token.json().catch(() => null); + + const response = await fetch(`/api/new/donasi/invoice${path ? path : ""}`, { + headers: { + "Content-Type": "application/json", + Accept: "application/json", + "Access-Control-Allow-Origin": "*", + Authorization: `Bearer ${token}`, + }, + }); + return await response.json().catch(() => null); +}; export const apiGetOneDonasiById = async (path: string, kategori: string) => { - const response = await fetch(`/api/new/donasi/${path}?cat=${kategori}`) - return await response.json().catch(() => null) -} \ No newline at end of file + const { token } = await fetch("/api/get-cookie").then((res) => res.json()); + if (!token) return await token.json().catch(() => null); + + const response = await fetch(`/api/new/donasi/${path}?cat=${kategori}`, { + headers: { + "Content-Type": "application/json", + Accept: "application/json", + "Access-Control-Allow-Origin": "*", + Authorization: `Bearer ${token}`, + }, + }); + return await response.json().catch(() => null); +}; diff --git a/src/app_modules/home/component/footer_home.tsx b/src/app_modules/home/component/footer_home.tsx index de65b7db..696acbd4 100644 --- a/src/app_modules/home/component/footer_home.tsx +++ b/src/app_modules/home/component/footer_home.tsx @@ -18,10 +18,11 @@ import { Home_ComponentAvatarProfile } from "./comp_avatar_profile"; import { listMenuHomeFooter } from "./list_menu_home"; import { MainColor } from "@/app_modules/_global/color"; import { clientLogger } from "@/util/clientLogger"; +import CustomSkeleton from "@/app_modules/components/CustomSkeleton"; export default function FooterHome() { const router = useRouter(); - const [dataUser, setDataUser] = useState({}); + const [dataUser, setDataUser] = useState(null); useShallowEffect(() => { cekUserLogin(); @@ -36,7 +37,7 @@ export default function FooterHome() { setDataUser(response.data); } } catch (error) { - clientLogger.error("Error get data profile",error); + clientLogger.error("Error get data profile", error); } } @@ -50,84 +51,101 @@ export default function FooterHome() { bottom={0} h={"9vh"} > - - {listMenuHomeFooter.map((e) => ( -
+ {dataUser?.profile === undefined || dataUser?.profile === null ? ( + + {Array.from(new Array(4)).map((_, i) => ( +
+ + + +
+ ))} +
+ ) : ( + + {listMenuHomeFooter.map((e) => ( +
+ { + if ( + dataUser.profile === undefined || + dataUser?.profile === null + ) { + return null; + } else if ( + dataUser.profile === undefined || + dataUser?.profile === null + ) { + router.push(RouterProfile.create, { scroll: false }); + } else { + if (e.link == "") { + ComponentGlobal_NotifikasiPeringatan("Cooming Soon"); + } else { + router.push(e.link, { scroll: false }); + } + } + }} + > + + {e.icon} + + + {e.name} + + +
+ ))} + +
{ if ( dataUser.profile === undefined || dataUser?.profile === null - ) { - return null; - } else if ( - dataUser.profile === undefined || - dataUser?.profile === null ) { router.push(RouterProfile.create, { scroll: false }); } else { - if (e.link == "") { - ComponentGlobal_NotifikasiPeringatan("Cooming Soon"); - } else { - router.push(e.link, { scroll: false }); - } + router.push( + RouterProfile.katalogOLD + `${dataUser?.profile}`, + { + scroll: false, + } + ); } }} > - - {e.icon} + + {dataUser.profile === undefined || + dataUser?.profile === null ? ( + + ) : ( + + )} - - {e.name} + + Profile
- ))} - -
- { - if ( - dataUser.profile === undefined || - dataUser?.profile === null - ) { - router.push(RouterProfile.create, { scroll: false }); - } else { - router.push(RouterProfile.katalogOLD + `${dataUser?.profile}`, { - scroll: false, - }); - } - }} - > - - {dataUser.profile === undefined || dataUser?.profile === null ? ( - - ) : ( - - )} - - - Profile - - -
-
+ + )} ); } diff --git a/src/app_modules/home/view_home_new.tsx b/src/app_modules/home/view_home_new.tsx index b2dd7fda..c632663a 100644 --- a/src/app_modules/home/view_home_new.tsx +++ b/src/app_modules/home/view_home_new.tsx @@ -18,11 +18,12 @@ import BodyHome from "./component/body_home"; import FooterHome from "./component/footer_home"; import { apiGetDataHome } from "./fun/get/api_home"; import { clientLogger } from "@/util/clientLogger"; +import CustomSkeleton from "../components/CustomSkeleton"; export default function HomeViewNew() { const [countNtf, setCountNtf] = useAtom(gs_count_ntf); const [newUserNtf, setNewUserNtf] = useAtom(gs_user_ntf); - const [dataUser, setDataUser] = useState({}); + const [dataUser, setDataUser] = useState(null); const [categoryPage, setCategoryPage] = useAtom(gs_notifikasi_kategori_app); const router = useRouter(); @@ -67,61 +68,69 @@ export default function HomeViewNew() { { - if ( - dataUser.profile != undefined || - dataUser?.profile != null - ) { - router.push(RouterUserSearch.main, { scroll: false }); - } else { - router.push(RouterProfile.create, { scroll: false }); - } - }} - > - - + dataUser?.profile == null ? ( + + ) : ( + { + if ( + dataUser.profile != undefined || + dataUser?.profile != null + ) { + router.push(RouterUserSearch.main, { scroll: false }); + } else { + router.push(RouterProfile.create, { scroll: false }); + } + }} + > + + + ) } customButtonRight={ - { - if ( - dataUser.profile != undefined || - dataUser?.profile != null - ) { - setCategoryPage("Semua"); - router.push( - RouterNotifikasi.categoryApp({ name: "semua" }), - { - scroll: false, - } - ); - } else { - router.push(RouterProfile.create, { scroll: false }); - } - }} - > - {countNtf != null && countNtf > 0 ? ( - - {countNtf > 99 ? "99+" : countNtf} - + dataUser?.profile == null ? ( + + ) : ( + { + if ( + dataUser.profile != undefined || + dataUser?.profile != null + ) { + setCategoryPage("Semua"); + router.push( + RouterNotifikasi.categoryApp({ name: "semua" }), + { + scroll: false, + } + ); + } else { + router.push(RouterProfile.create, { scroll: false }); } - > + }} + > + {countNtf != null && countNtf > 0 ? ( + + {countNtf > 99 ? "99+" : countNtf} + + } + > + + + ) : ( - - ) : ( - - )} - + )} + + ) } /> } diff --git a/src/app_modules/investasi/_lib/api_interface.ts b/src/app_modules/investasi/_lib/api_interface.ts index a1475cff..a31133b6 100644 --- a/src/app_modules/investasi/_lib/api_interface.ts +++ b/src/app_modules/investasi/_lib/api_interface.ts @@ -1,19 +1,64 @@ export const apiGetMasterInvestasi = async (path?: string) => { - const response = await fetch(`/api/new/investasi/master${(path) ? path : ''}`) - return await response.json().catch(() => null) -} + const { token } = await fetch("/api/get-cookie").then((res) => res.json()); + if (!token) return await token.json().catch(() => null); + + const response = await fetch(`/api/new/investasi/master${path ? path : ""}`, { + headers: { + "Content-Type": "application/json", + Accept: "application/json", + "Access-Control-Allow-Origin": "*", + Authorization: `Bearer ${token}`, + }, + }); + + return await response.json().catch(() => null); +}; export const apiGetOneInvestasiById = async (path: string) => { - const response = await fetch(`/api/new/investasi/${path}`) - return await response.json().catch(() => null) -} + const { token } = await fetch("/api/get-cookie").then((res) => res.json()); + if (!token) return await token.json().catch(() => null); + + const response = await fetch(`/api/new/investasi/${path}`, { + headers: { + "Content-Type": "application/json", + Accept: "application/json", + "Access-Control-Allow-Origin": "*", + Authorization: `Bearer ${token}`, + }, + }); + return await response.json().catch(() => null); +}; export const apiGetAllInvestasi = async (path?: string) => { - const response = await fetch(`/api/new/investasi${(path) ? path : ''}`) - return await response.json().catch(() => null) -} + const { token } = await fetch("/api/get-cookie").then((res) => res.json()); + if (!token) return await token.json().catch(() => null); + + const response = await fetch(`/api/new/investasi${path ? path : ""}`, { + headers: { + "Content-Type": "application/json", + Accept: "application/json", + "Access-Control-Allow-Origin": "*", + Authorization: `Bearer ${token}`, + }, + }); + + return await response.json().catch(() => null); +}; export const apiGetAllSahamSaya = async (path?: string) => { - const response = await fetch(`/api/new/investasi/invoice${(path) ? path : ''}`) - return await response.json().catch(() => null) -} \ No newline at end of file + const { token } = await fetch("/api/get-cookie").then((res) => res.json()); + if (!token) return await token.json().catch(() => null); + + const response = await fetch( + `/api/new/investasi/invoice${path ? path : ""}`, + { + headers: { + "Content-Type": "application/json", + Accept: "application/json", + "Access-Control-Allow-Origin": "*", + Authorization: `Bearer ${token}`, + }, + } + ); + return await response.json().catch(() => null); +}; diff --git a/src/app_modules/katalog/component/regular_expressions.ts b/src/app_modules/katalog/component/regular_expressions.ts index bb809be0..e4e9d38b 100644 --- a/src/app_modules/katalog/component/regular_expressions.ts +++ b/src/app_modules/katalog/component/regular_expressions.ts @@ -1,3 +1,3 @@ export var validRegex = /^([a-zA-Z0-9\.!#$%&'*+/=?^_`{|}~-]+)@([a-zA-Z0-9])+.([a-z]+)(.[a-z]+)?$/; -export const gmailRegex = /^[a-zA-Z0-9._%+-]+@gmail\.com$/; \ No newline at end of file +export const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; \ No newline at end of file diff --git a/src/app_modules/katalog/main/layout_new.tsx b/src/app_modules/katalog/main/layout_new.tsx index e96ae293..5fe1b898 100644 --- a/src/app_modules/katalog/main/layout_new.tsx +++ b/src/app_modules/katalog/main/layout_new.tsx @@ -1,66 +1,85 @@ -'use client' +"use client"; import { funGetUserIdByToken } from "@/app_modules/_global/fun/get"; -import { UIGlobal_LayoutHeaderTamplate, UIGlobal_LayoutTamplate } from "@/app_modules/_global/ui"; +import { + UIGlobal_LayoutHeaderTamplate, + UIGlobal_LayoutTamplate, +} from "@/app_modules/_global/ui"; import { apiGetUserProfile } from "@/app_modules/user"; -import { ActionIcon } from "@mantine/core"; +import { ActionIcon, Skeleton } from "@mantine/core"; import { useDisclosure, useShallowEffect } from "@mantine/hooks"; import { IconDotsVertical } from "@tabler/icons-react"; import { useParams } from "next/navigation"; import { useState } from "react"; import DrawerKatalogNew from "../component/drawer_katalog_new"; +import CustomSkeleton from "@/app_modules/components/CustomSkeleton"; -export default function LayoutKatalogNew({ children }: { children: any }) { - const param = useParams<{ id: string }>() - const [authorId, setAuthorId] = useState("") - const [userRoleId, setUserRoleId] = useState("") - const [userLoginId, setUserLoginId] = useState("") - const [opened, { open, close }] = useDisclosure() - const [loading, setLoading] = useState(true); +export default function LayoutKatalogNew({ + children, + userLoginId, +}: { + children: any; + userLoginId: string; +}) { + const param = useParams<{ id: string }>(); + const [authorId, setAuthorId] = useState(""); + const [userRoleId, setUserRoleId] = useState(""); + const [opened, { open, close }] = useDisclosure(); + const [loading, setLoading] = useState(true); - async function getProfile() { - try { - setLoading(true) - const response = await apiGetUserProfile(`?profile=${param.id}`) - const response2 = await funGetUserIdByToken() - if (response) { - setAuthorId(response.data.id) - setUserRoleId(response.data.masterUserRoleId) - setUserLoginId(response2) - } - } catch (error) { - console.error(error); - } finally { - setLoading(false) + async function getProfile() { + try { + setLoading(true); + const response = await apiGetUserProfile(`?profile=${param.id}`); + + if (response) { + setAuthorId(response.data.id); + setUserRoleId(response.data.masterUserRoleId); } - } + } catch (error) { + console.error(error); + } finally { + setLoading(false); + } + } - useShallowEffect(() => { - getProfile() - }, []) + useShallowEffect(() => { + getProfile(); + }, []); - return ( - <> - : - authorId == userLoginId ? ( - open()}> - - - ) : ( - - ) - } - /> + return ( + <> + + + + ) : authorId == userLoginId ? ( + open()} + > + + + ) : ( + + ) } - > - {children} - close()} userRoleId={userRoleId} userId={userLoginId} /> - - - ) -} \ No newline at end of file + /> + } + > + {children} + close()} + userRoleId={userRoleId} + userId={userLoginId} + /> + + + ); +} diff --git a/src/app_modules/katalog/portofolio/component/button/comp_button_edit_logo_bisnis.tsx b/src/app_modules/katalog/portofolio/component/button/comp_button_edit_logo_bisnis.tsx index a5600ad7..c6c85b60 100644 --- a/src/app_modules/katalog/portofolio/component/button/comp_button_edit_logo_bisnis.tsx +++ b/src/app_modules/katalog/portofolio/component/button/comp_button_edit_logo_bisnis.tsx @@ -10,46 +10,73 @@ import { Button } from "@mantine/core"; import { useRouter } from "next/navigation"; import { useState } from "react"; -import { funGlobal_UploadToStorage } from "@/app_modules/_global/fun"; +import { + funGlobal_DeleteFileById, + funGlobal_UploadToStorage, +} from "@/app_modules/_global/fun"; import { DIRECTORY_ID } from "@/app/lib"; import { portofolio_funEditLogoBisnisById } from "../../fun"; +import { clientLogger } from "@/util/clientLogger"; export function ComponentPortofolio_ButtonEditLogoBisnis({ file, portofolioId, + fileRemoveId, }: { file: File; portofolioId: string; + fileRemoveId: string; }) { const router = useRouter(); const [loading, setLoading] = useState(false); async function onUpdate() { - const uploadFileToStorage = await funGlobal_UploadToStorage({ - file: file, - dirId: DIRECTORY_ID.portofolio_logo, - }); - - if (!uploadFileToStorage.success) - return ComponentGlobal_NotifikasiPeringatan("Gagal upload gambar"); - - const res = await portofolio_funEditLogoBisnisById({ - portofolioId: portofolioId, - logoId: uploadFileToStorage.data.id, - }); - if (res.status === 200) { + try { setLoading(true); - ComponentGlobal_NotifikasiBerhasil(res.message); - router.back(); - } else { - ComponentGlobal_NotifikasiGagal(res.message); + + const deleteLogo = await funGlobal_DeleteFileById({ + fileId: fileRemoveId, + dirId: DIRECTORY_ID.portofolio_logo, + }); + + if (!deleteLogo.success) { + setLoading(false); + clientLogger.error("Error delete logo", deleteLogo.message); + } + + const uploadFileToStorage = await funGlobal_UploadToStorage({ + file: file, + dirId: DIRECTORY_ID.portofolio_logo, + }); + + if (!uploadFileToStorage.success) { + setLoading(false); + ComponentGlobal_NotifikasiPeringatan("Gagal upload gambar"); + return; + } + const res = await portofolio_funEditLogoBisnisById({ + portofolioId: portofolioId, + logoId: uploadFileToStorage.data.id, + }); + + if (res.status === 200) { + ComponentGlobal_NotifikasiBerhasil(res.message); + router.back(); + } else { + setLoading(false); + ComponentGlobal_NotifikasiGagal(res.message); + } + } catch (error) { + setLoading(false); + clientLogger.error("Error update logo", error); } } + return ( <> {file ? ( - {/*
{JSON.stringify(dataProfile, null, 2)}
*/} ); } diff --git a/src/app_modules/map/_component/button/comp_button_update_data_map.tsx b/src/app_modules/map/_component/button/comp_button_update_data_map.tsx index 749f0efa..e0d5f1fa 100644 --- a/src/app_modules/map/_component/button/comp_button_update_data_map.tsx +++ b/src/app_modules/map/_component/button/comp_button_update_data_map.tsx @@ -8,12 +8,16 @@ import { } from "@/app_modules/_global/notif_global"; import { Button } from "@mantine/core"; +import { DIRECTORY_ID } from "@/app/lib"; +import { + funGlobal_DeleteFileById, + funGlobal_UploadToStorage, +} from "@/app_modules/_global/fun"; +import { clientLogger } from "@/util/clientLogger"; +import { useRouter } from "next/navigation"; +import { useState } from "react"; import { map_funEditMap } from "../../fun/edit/fun_edit_map"; import { MODEL_MAP } from "../../lib/interface"; -import { useRouter } from "next/navigation"; -import { DIRECTORY_ID } from "@/app/lib"; -import { funGlobal_UploadToStorage } from "@/app_modules/_global/fun"; -import { useState } from "react"; export function ComponentMap_ButtonUpdateDataMap({ data, @@ -25,30 +29,59 @@ export function ComponentMap_ButtonUpdateDataMap({ const router = useRouter(); const [isLoading, setIsLoading] = useState(false); async function onSavePin() { - if (file !== null) { - const uploadFileToStorage = await funGlobal_UploadToStorage({ - file: file, - dirId: DIRECTORY_ID.map_image, - }); - if (!uploadFileToStorage.success) - return ComponentGlobal_NotifikasiPeringatan("Gagal upload gambar"); + try { + setIsLoading(true); - const res = await map_funEditMap({ - data: data, - fileId: uploadFileToStorage.data.id, - }); - res.status === 200 - ? (ComponentGlobal_NotifikasiBerhasil(res.message), router.back()) - : ComponentGlobal_NotifikasiGagal(res.message); - } else { - const res = await map_funEditMap({ - data: data, - }); - res.status === 200 - ? (ComponentGlobal_NotifikasiBerhasil(res.message), router.back()) - : ComponentGlobal_NotifikasiGagal(res.message); + if (file !== null) { + const deleteLogo = await funGlobal_DeleteFileById({ + fileId: data.imageId, + dirId: DIRECTORY_ID.map_image, + }); + + if (!deleteLogo.success) { + setIsLoading(false); + clientLogger.error("Error delete logo", deleteLogo.message); + } + + const uploadFileToStorage = await funGlobal_UploadToStorage({ + file: file, + dirId: DIRECTORY_ID.map_image, + }); + if (!uploadFileToStorage.success) { + setIsLoading(false); + ComponentGlobal_NotifikasiPeringatan("Gagal upload gambar"); + return; + } + + const res = await map_funEditMap({ + data: data, + fileId: uploadFileToStorage.data.id, + }); + + if (res.status === 200) { + ComponentGlobal_NotifikasiBerhasil(res.message); + router.back(); + } else { + setIsLoading(false); + ComponentGlobal_NotifikasiGagal(res.message); + } + } else { + const res = await map_funEditMap({ + data: data, + }); + + if (res.status === 200) { + ComponentGlobal_NotifikasiBerhasil(res.message); + router.back(); + } else { + setIsLoading(false); + ComponentGlobal_NotifikasiGagal(res.message); + } + } + } catch (error) { + setIsLoading(false); + clientLogger.error("Error update logo", error); } - setIsLoading(true); } return ( diff --git a/src/app_modules/map/lib/api_map.ts b/src/app_modules/map/lib/api_map.ts index e274279c..feb650b5 100644 --- a/src/app_modules/map/lib/api_map.ts +++ b/src/app_modules/map/lib/api_map.ts @@ -14,7 +14,17 @@ export const apiGetAllMap = async (path?: string) => { }; export const apiGetOneMapById = async (path: string) => { - - const response = await fetch(`/api/new/map/${path}`); + const { token } = await fetch("/api/get-cookie").then((res) => res.json()); + if (!token) return await token.json().catch(() => null); + + + const response = await fetch(`/api/new/map/${path}`, { + headers: { + "Content-Type": "application/json", + Accept: "application/json", + "Access-Control-Allow-Origin": "*", + Authorization: `Bearer ${token}`, + }, + }); return await response.json().catch(() => null); }; diff --git a/src/app_modules/map/ui/ui_custom_pin.tsx b/src/app_modules/map/ui/ui_custom_pin.tsx index d9799565..93ccb1b7 100644 --- a/src/app_modules/map/ui/ui_custom_pin.tsx +++ b/src/app_modules/map/ui/ui_custom_pin.tsx @@ -26,9 +26,13 @@ import { map_funCustomPinMap } from "../fun"; import { defaultMapZoom } from "../lib/default_lat_long"; import { MODEL_MAP } from "../lib/interface"; import { APIs, DIRECTORY_ID } from "@/app/lib"; -import { funGlobal_UploadToStorage } from "@/app_modules/_global/fun"; +import { + funGlobal_DeleteFileById, + funGlobal_UploadToStorage, +} from "@/app_modules/_global/fun"; import { MAX_SIZE } from "@/app_modules/_global/lib"; import { PemberitahuanMaksimalFile } from "@/app_modules/_global/lib/max_size"; +import { clientLogger } from "@/util/clientLogger"; export function UiMap_CustomPin({ dataMap, @@ -172,34 +176,67 @@ export function UiMap_CustomPin({ - + ); } -function ButtonSimpan({ mapId, filePin }: { mapId: string; filePin: File }) { +function ButtonSimpan({ + mapId, + filePin, + fileRemoveId, +}: { + mapId: string; + filePin: File; + fileRemoveId: string; +}) { const router = useRouter(); const [isLoading, setLoading] = useState(false); async function onCustom() { - const uploadFileToStorage = await funGlobal_UploadToStorage({ - file: filePin, - dirId: DIRECTORY_ID.map_pin, - }); + try { + setLoading(true); + const deletePin = await funGlobal_DeleteFileById({ + fileId: fileRemoveId, + dirId: DIRECTORY_ID.map_pin, + }); + if (!deletePin.success) { + setLoading(false); + clientLogger.error("Error delete logo", deletePin.message); + } - if (!uploadFileToStorage.success) - return ComponentGlobal_NotifikasiPeringatan("Gagal upload gambar"); + const uploadFileToStorage = await funGlobal_UploadToStorage({ + file: filePin, + dirId: DIRECTORY_ID.map_pin, + }); - const res = await map_funCustomPinMap({ - mapId: mapId, - fileId: uploadFileToStorage.data.id, - }); - res.status === 200 - ? (ComponentGlobal_NotifikasiBerhasil(res.message), - setLoading(true), - router.back()) - : ComponentGlobal_NotifikasiGagal(res.message); + if (!uploadFileToStorage.success) { + setLoading(false); + ComponentGlobal_NotifikasiPeringatan("Gagal upload gambar"); + return; + } + + const res = await map_funCustomPinMap({ + mapId: mapId, + fileId: uploadFileToStorage.data.id, + }); + + if (res.status === 200) { + ComponentGlobal_NotifikasiBerhasil(res.message); + router.back(); + } else { + setLoading(false); + ComponentGlobal_NotifikasiGagal(res.message); + } + } catch (error) { + setLoading(false); + clientLogger.error("Error custom pin", error); + } } return (