From 94a1ccd6d702eb9b35add35d0ee60c3632a082b2 Mon Sep 17 00:00:00 2001 From: Bagasbanuna02 Date: Wed, 12 Feb 2025 17:07:59 +0800 Subject: [PATCH 1/5] fix portofolio deskripsi: - edit data portofolio --- run.build.dev | 1 + src/app/api/portofolio/[id]/route.ts | 126 +++++++- .../dev/portofolio/edit/data/[id]/page.tsx | 2 +- src/app/zCoba/skeleton/page.tsx | 101 +----- .../component/api_fetch_portofolio.ts | 58 +++- .../portofolio/component/skeleton_view.tsx | 22 +- .../katalog/portofolio/create/view.tsx | 3 +- .../portofolio/edit/data/ui_edit_data.tsx | 288 +++++++++++++----- 8 files changed, 410 insertions(+), 191 deletions(-) create mode 100644 run.build.dev diff --git a/run.build.dev b/run.build.dev new file mode 100644 index 00000000..57e0310d --- /dev/null +++ b/run.build.dev @@ -0,0 +1 @@ +nice -n 19 bun --env-file=.env.build run build diff --git a/src/app/api/portofolio/[id]/route.ts b/src/app/api/portofolio/[id]/route.ts index 0e5e4947..9222acb9 100644 --- a/src/app/api/portofolio/[id]/route.ts +++ b/src/app/api/portofolio/[id]/route.ts @@ -1,7 +1,131 @@ import { prisma } from "@/lib"; +import backendLogger from "@/util/backendLogger"; import { NextResponse } from "next/server"; -export { POST }; +export { GET, POST, PUT }; + +async function GET(request: Request, { params }: { params: { id: string } }) { + try { + const { id } = params; + + const data = await prisma.portofolio.findUnique({ + where: { + id: id, + }, + include: { + MasterBidangBisnis: { + select: { + id: true, + name: true, + active: true, + }, + }, + Portofolio_MediaSosial: true, + Profile: { + select: { + userId: true, + User: { + select: { + id: true, + }, + }, + }, + }, + BusinessMaps: { + include: { + Author: true, + }, + }, + }, + }); + + if (!data) + return NextResponse.json( + { + success: false, + message: "Data tidak ditemukan", + }, + { status: 404 } + ); + + return NextResponse.json( + { + success: true, + message: "Berhasil mendapatkan data", + data: data, + }, + { status: 200 } + ); + } catch (error) { + backendLogger.error("API Error Get Data Portofolio", error); + return NextResponse.json( + { + success: false, + message: "API Error Get Data Potofolio", + reason: (error as Error).message, + }, + { status: 500 } + ); + } +} + +async function PUT(request: Request, { params }: { params: { id: string } }) { + if (request.method !== "PUT") { + return NextResponse.json( + { + success: false, + message: "Method not allowed", + }, + { status: 405 } + ); + } + + try { + const { id } = params; + const { data } = await request.json(); + + const udpateData = await prisma.portofolio.update({ + where: { + id: id, + }, + data: { + namaBisnis: data.namaBisnis, + alamatKantor: data.alamatKantor, + tlpn: data.tlpn, + deskripsi: data.deskripsi, + masterBidangBisnisId: data.masterBidangBisnisId, + }, + }); + + if (!udpateData) + return NextResponse.json( + { + success: false, + message: "Gagal update data", + }, + { status: 400 } + ); + + return NextResponse.json( + { + success: true, + message: "Berhasil mendapatkan data", + data: udpateData, + }, + + { status: 200 } + ); + } catch (error) { + return NextResponse.json( + { + success: false, + message: "Error update data", + reason: (error as Error).message, + }, + { status: 500 } + ); + } +} async function POST(request: Request, { params }: { params: { id: string } }) { if (request.method !== "POST") { diff --git a/src/app/dev/portofolio/edit/data/[id]/page.tsx b/src/app/dev/portofolio/edit/data/[id]/page.tsx index 6d23121d..6325f831 100644 --- a/src/app/dev/portofolio/edit/data/[id]/page.tsx +++ b/src/app/dev/portofolio/edit/data/[id]/page.tsx @@ -18,7 +18,7 @@ export default async function Page({ params }: { params: { id: string } }) { return ( <> - + ); } diff --git a/src/app/zCoba/skeleton/page.tsx b/src/app/zCoba/skeleton/page.tsx index a5b62d10..83058314 100644 --- a/src/app/zCoba/skeleton/page.tsx +++ b/src/app/zCoba/skeleton/page.tsx @@ -5,6 +5,7 @@ import { UIGlobal_LayoutHeaderTamplate, UIGlobal_LayoutTamplate, } from "@/app_modules/_global/ui"; +import CustomSkeleton from "@/app_modules/components/CustomSkeleton"; import { Button, Grid, Skeleton, Stack } from "@mantine/core"; import Link from "next/link"; @@ -14,100 +15,12 @@ export default function Voting_ComponentSkeletonViewPuh() { } > - - - - - - - - {Array.from(new Array(2)).map((e, i) => ( - - - - - - - - - ))} - - - - {/* - - - - - - - - - -
- -
- - - - - - - - - - - - - - - - - - - - - - - -
-
*/} - - {/* - -
- -
- - - - - - - - - - - -
-
*/} + + {Array.from({ length: 4 }).map((_, i) => ( + + ))} + +
diff --git a/src/app_modules/katalog/portofolio/component/api_fetch_portofolio.ts b/src/app_modules/katalog/portofolio/component/api_fetch_portofolio.ts index 14f8f677..699249c9 100644 --- a/src/app_modules/katalog/portofolio/component/api_fetch_portofolio.ts +++ b/src/app_modules/katalog/portofolio/component/api_fetch_portofolio.ts @@ -1,8 +1,12 @@ -export { - apiCreatePortofolio, -}; +export { apiCreatePortofolio, apiGetPortofolioById, apiUpdatePortofolioById }; -const apiCreatePortofolio = async ({ profileId, data }: { profileId: string, data: any }) => { +const apiCreatePortofolio = async ({ + profileId, + data, +}: { + profileId: string; + data: any; +}) => { const { token } = await fetch("/api/get-cookie").then((res) => res.json()); if (!token) return await token.json().catch(() => null); @@ -19,3 +23,49 @@ const apiCreatePortofolio = async ({ profileId, data }: { profileId: string, dat return await res.json().catch(() => null); }; + +const apiGetPortofolioById = async ({ id }: { id: string }) => { + const { token } = await fetch("/api/get-cookie").then((res) => res.json()); + if (!token) return await token.json().catch(() => null); + + const res = await fetch(`/api/portofolio/${id}`, { + method: "GET", + headers: { + "Content-Type": "application/json", + Accept: "application/json", + "Access-Control-Allow-Origin": "*", + Authorization: `Bearer ${token}`, + }, + }); + + return await res.json().catch(() => null); +}; + +const apiUpdatePortofolioById = async ({ + id, + data, +}: { + id: string; + data: any; +}) => { + const { token } = await fetch("/api/get-cookie").then((res) => res.json()); + if (!token) return await token.json().catch(() => null); + + const respone = await fetch(`/api/portofolio/${id}`, { + method: "PUT", + body: JSON.stringify({ data }), + headers: { + "Content-Type": "application/json", + Accept: "application/json", + "Access-Control-Allow-Origin": "*", + Authorization: `Bearer ${token}`, + }, + }); + + if (!respone.ok) { + console.error("Failed to send logs:", respone.statusText); + return null; + } + + return await respone.json(); +}; diff --git a/src/app_modules/katalog/portofolio/component/skeleton_view.tsx b/src/app_modules/katalog/portofolio/component/skeleton_view.tsx index 293075fa..fc8e114b 100644 --- a/src/app_modules/katalog/portofolio/component/skeleton_view.tsx +++ b/src/app_modules/katalog/portofolio/component/skeleton_view.tsx @@ -1,8 +1,18 @@ -export { +import CustomSkeleton from "@/app_modules/components/CustomSkeleton"; +import { Stack } from "@mantine/core"; +export { Portofolio_SkeletonEditDataBisnis }; + +function Portofolio_SkeletonEditDataBisnis() { + return ( + <> + + {Array.from({ length: 4 }).map((_, i) => ( + + ))} + + + + + ); } - -function Portofolio_SkeletonListPorto() { - - -} \ No newline at end of file diff --git a/src/app_modules/katalog/portofolio/create/view.tsx b/src/app_modules/katalog/portofolio/create/view.tsx index 244dadf4..2b622d15 100644 --- a/src/app_modules/katalog/portofolio/create/view.tsx +++ b/src/app_modules/katalog/portofolio/create/view.tsx @@ -184,12 +184,13 @@ export default function CreatePortofolio() { backgroundColor: MainColor.login, }, }} - inputStyle={{ width: "100%", backgroundColor: MainColor.login }} + inputStyle={{ width: "100%", backgroundColor: MainColor.white }} defaultCountry="id" onChange={(val) => { const valPhone = val.substring(1); setDataPortofolio({ ...dataPortofolio, + tlpn: valPhone, }); }} diff --git a/src/app_modules/katalog/portofolio/edit/data/ui_edit_data.tsx b/src/app_modules/katalog/portofolio/edit/data/ui_edit_data.tsx index 887bab2e..d8c6261e 100644 --- a/src/app_modules/katalog/portofolio/edit/data/ui_edit_data.tsx +++ b/src/app_modules/katalog/portofolio/edit/data/ui_edit_data.tsx @@ -1,66 +1,179 @@ "use client"; -import { - MainColor -} from "@/app_modules/_global/color/color_pallet"; +import { MainColor } from "@/app_modules/_global/color/color_pallet"; import ComponentGlobal_ErrorInput from "@/app_modules/_global/component/error_input"; import ComponentGlobal_InputCountDown from "@/app_modules/_global/component/input_countdown"; +import { apiGetMasterBidangBisnis } from "@/app_modules/_global/lib/api_master"; +import { ComponentGlobal_NotifikasiPeringatan } from "@/app_modules/_global/notif_global"; import { ComponentGlobal_NotifikasiBerhasil } from "@/app_modules/_global/notif_global/notifikasi_berhasil"; import { ComponentGlobal_NotifikasiGagal } from "@/app_modules/_global/notif_global/notifikasi_gagal"; -import { Button, Select, Stack, TextInput, Textarea } from "@mantine/core"; +import { clientLogger } from "@/util/clientLogger"; +import { + Button, + Select, + Stack, + Text, + TextInput, + Textarea, +} from "@mantine/core"; +import { useShallowEffect } from "@mantine/hooks"; import _ from "lodash"; -import { AppRouterInstance } from "next/dist/shared/lib/app-router-context.shared-runtime"; -import { useRouter } from "next/navigation"; +import { useParams, useRouter } from "next/navigation"; import { useState } from "react"; -import { Portofolio_funEditDataBisnis } from "../../fun/edit/fun_edit_data_bisnis_by_id"; +import { PhoneInput } from "react-international-phone"; +import { + apiGetPortofolioById, + apiUpdatePortofolioById, +} from "../../component/api_fetch_portofolio"; +import { Portofolio_SkeletonEditDataBisnis } from "../../component/skeleton_view"; import { MODEL_PORTOFOLIO, MODEL_PORTOFOLIO_BIDANG_BISNIS, } from "../../model/interface"; -export default function Portofolio_EditDataBisnis({ - dataPorto, - listBidang, -}: { - dataPorto: MODEL_PORTOFOLIO; - listBidang: MODEL_PORTOFOLIO_BIDANG_BISNIS[]; -}) { +interface IUpdatePortofoli { + namaBisnis: string; + alamatKantor: string; + tlpn: string; + deskripsi: string; + masterBidangBisnisId: string; +} + +export default function Portofolio_EditDataBisnis() { const router = useRouter(); - const [value, setValue] = useState(dataPorto); const [loading, setLoading] = useState(false); + const params = useParams<{ id: string }>(); + const portofolioId = params.id; + const [data, setData] = useState(null); + const [listBidang, setListBidang] = useState< + MODEL_PORTOFOLIO_BIDANG_BISNIS[] + >([]); + + useShallowEffect(() => { + onLoadBidang(); + onLoadData(); + }, []); + + const onLoadData = async () => { + try { + const respone = await apiGetPortofolioById({ + id: portofolioId, + }); + + if (respone.success) { + setData(respone.data); + } else { + setData(null); + } + } catch (error) { + clientLogger.error("Error get data portofolio", error); + } + }; + + const onLoadBidang = async () => { + try { + const respone = await apiGetMasterBidangBisnis(); + if (respone.success) { + setListBidang(respone.data); + } else { + setListBidang([]); + } + } catch (error) { + clientLogger.error("Error get data master bidang bisnis", error); + } + }; + + const validateData = async (data: any) => { + if (_.values(data).includes("")) { + // VALIDASI NOMOR TELEPON + return "Lengkapi data"; + } + + if (data?.tlpn.length < 8) { + return "Nomor telepon minimal 8 digit"; + } + }; + + const hanldeUpadteData = async (data: any) => { + try { + const newData: IUpdatePortofoli = { + namaBisnis: data?.namaBisnis, + alamatKantor: data?.alamatKantor, + tlpn: data?.tlpn, + deskripsi: data?.deskripsi, + masterBidangBisnisId: data?.MasterBidangBisnis.id, + }; + + const respone = await apiUpdatePortofolioById({ + data: newData, + id: portofolioId, + }); + + return respone; + } catch (error) { + console.error("Error update data portofolio", error); + return null; + } + }; + + const submitUpdate = async () => { + const validate = await validateData(data); + if (validate) { + ComponentGlobal_NotifikasiPeringatan(validate); + return; + } + + try { + setLoading(true); + const updateData = await hanldeUpadteData(data); + + if (updateData.success) { + ComponentGlobal_NotifikasiBerhasil(updateData.message); + router.back(); + } else { + setLoading(false); + ComponentGlobal_NotifikasiGagal(updateData.message); + } + } catch (error) { + setLoading(false); + clientLogger.error("Error update data portofolio", error); + } + }; + + if (!data) return ; + return ( <> - {/*
{JSON.stringify(porto, null, 2)}
*/} ) : ( "" ) } onChange={(val) => { - setValue({ - ...value, + setData({ + ...data, namaBisnis: val.target.value, }); }} @@ -68,17 +181,17 @@ export default function Portofolio_EditDataBisnis({