From aa456f68d76292611c422caeb0483775cf1248fd Mon Sep 17 00:00:00 2001 From: JIeunhuh Date: Wed, 7 May 2025 16:08:07 +0900 Subject: [PATCH] =?UTF-8?q?api=20=EC=9E=91=EC=97=85=EC=A7=84=ED=96=89?= =?UTF-8?q?=EC=A4=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/sn/app/components/lib/api/index.ts | 61 ++++++++++++++++++++++ apps/sn/app/map/featureClickEventPopup.tsx | 15 +++--- 2 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 apps/sn/app/components/lib/api/index.ts diff --git a/apps/sn/app/components/lib/api/index.ts b/apps/sn/app/components/lib/api/index.ts new file mode 100644 index 000000000..a171844a1 --- /dev/null +++ b/apps/sn/app/components/lib/api/index.ts @@ -0,0 +1,61 @@ +const BASE_URL = process.env.NEXT_PUBLIC_SEWERAGE_API_URL || ''; + +export const postData = async (url: string, data: object): Promise => { + const options: RequestInit = { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(data), + }; + const res = await fetch(`${BASE_URL}/${url}`, options); + const resData = await res.json(); + return resData; +}; + +export const postMutiPart = async (url: string, data: FormData): Promise => { + const options: RequestInit = { + method: "POST", + body: data, + }; + const res = await fetch(`${BASE_URL}/${url}`, options); + const resData = await res.json(); + return resData; +}; + +export const putData = async (url: string, data: object): Promise => { + const options: RequestInit = { + method: "PUT", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(data), + }; + const res = await fetch(`${BASE_URL}/${url}`, options); + const resData = await res.json(); + return resData; +}; + +export const getData = async (url: string): Promise => { + const res = await fetch(`${BASE_URL}/${url}`, { + method: "GET", + }); + if (res.ok) { + const data = await res.json(); + return data; + } + throw new Error('Failed to fetch data'); +}; + +export const deleteData = async (url: string, data?: object): Promise => { + const options: RequestInit = { + method: "DELETE", + headers: { + "Content-Type": "application/json", + }, + body: data ? JSON.stringify(data) : undefined, + }; + const res = await fetch(`${BASE_URL}/${url}`, options); + const resData = await res.json(); + return resData; +}; \ No newline at end of file diff --git a/apps/sn/app/map/featureClickEventPopup.tsx b/apps/sn/app/map/featureClickEventPopup.tsx index 6ac476eb2..6dd573732 100644 --- a/apps/sn/app/map/featureClickEventPopup.tsx +++ b/apps/sn/app/map/featureClickEventPopup.tsx @@ -4,6 +4,7 @@ import { Button, Table, TableBody, TableCell, TableHead, TableHeader, TableRow } import { use, useEffect, useState } from "react"; import { SwtrmtFcltCol } from "./table"; import { useGisContext } from "@common/business_components/ui/map/Gis"; +import { getData } from "@/components/lib/api"; const inputColumn = [ { id: "title", title: "설치주제", type: "select", options: ["BTL", "재정사업"] }, { id: "guyeok", title: "처리구역", type: "select", options: ["구역1", "구역2"] }, @@ -16,7 +17,7 @@ const inputColumn = [ // TODO: const FeatureClickEventPopup = ({ onClose, featureData }: { onClose: () => void, featureData: any }) => { const { gisMap } = useGisContext() as unknown as { checkedLayerIds: React.MutableRefObject; treeData: { lyrSn: number }[] | null; gisCore: any; gisMap: any; }; - const featureProperties = featureData.getProperties(); // 피처의 속성 가져오기 + const featureId = featureData.getId(); // 피처의 속성 가져오기 const [layerNm, setLayerNm] = useState(""); // 레이어 이름 상태 // 피쳐데이터의 현재 레이어 이름 가져오는 함수 const getSelectedLayers = () => { @@ -61,10 +62,12 @@ const FeatureClickEventPopup = ({ onClose, featureData }: { onClose: () => void, updateData(inspectionData, setInspectionData); } }; - function transformFeatureDataToArray(featureProperties: any, keys: string[]) { + async function transformFeatureDataToArray(featureId: any, keys: string[]) { + const data = await getData(`v1/map/features/${featureId}`); // 필요한 키만 추출하여 새로운 객체 생성 + console.log("data", data); const transformedData = keys.reduce((acc, key) => { - acc[key] = featureProperties[key] !== undefined ? featureProperties[key] : "N/A"; // 값이 없으면 "N/A"로 처리 + acc[key] = data[key] !== undefined ? data[key] : "N/A"; // 값이 없으면 "N/A"로 처리 return acc; }, {} as Record); @@ -72,8 +75,8 @@ const FeatureClickEventPopup = ({ onClose, featureData }: { onClose: () => void, return [transformedData]; } const keys = SwtrmtFcltCol.map((column) => column.id); - const transformedFeatureData = transformFeatureDataToArray(featureProperties, keys); - useEffect(() => { setLayerNm(getSelectedLayers()) }, [featureProperties]); + const transformedFeatureData = transformFeatureDataToArray(featureId, keys); + useEffect(() => { setLayerNm(getSelectedLayers()) }, [featureId]); const renderSection = (title: string, data: { year: string; type: string }[], sectionKey: string) => (
{/* 섹션 제목 */} @@ -124,7 +127,7 @@ const FeatureClickEventPopup = ({ onClose, featureData }: { onClose: () => void,
-