diff --git a/apps/cs/middleware.ts b/apps/cs/middleware.ts index 81039090b..493699bb2 100644 --- a/apps/cs/middleware.ts +++ b/apps/cs/middleware.ts @@ -1,4 +1,5 @@ import { NextRequest, NextResponse } from "next/server"; +import { CLAIM_NAME, AUTH_USER_ID } from "./src/shared/config"; const serverApiUrl = process.env.NEXT_PUBLIC_SERVER_API_URL ?? "http://localhost:3000"; @@ -16,8 +17,17 @@ export function middleware(req: NextRequest) { } if (pathname.startsWith("/attach-service")) { + const requestHeaders = new Headers(req.headers); + const token = req.cookies.get(CLAIM_NAME)?.value; + const userId = req.cookies.get(AUTH_USER_ID)?.value; const newUrl = `${serverApiUrl}${pathname}`; - return NextResponse.rewrite(new URL(newUrl)); + + if (token) requestHeaders.set(CLAIM_NAME, token); + if (userId) requestHeaders.set(AUTH_USER_ID, userId); + + return NextResponse.rewrite(new URL(newUrl), { + headers: requestHeaders, + }); } return NextResponse.next(); diff --git a/apps/cs/src/entities/attachment/api/attachment-service.ts b/apps/cs/src/entities/attachment/api/attachment-service.ts new file mode 100644 index 000000000..b0c613c5f --- /dev/null +++ b/apps/cs/src/entities/attachment/api/attachment-service.ts @@ -0,0 +1,17 @@ +import { client } from "@/shared/api"; +import { ENDPOINT } from "@/shared/config"; +import type { + Attachment, + AttachmentService, +} from "../model/attachment-interface"; + +export class AttachmentApiService implements AttachmentService { + public async getAttachments(atflCd: string): Promise { + const response = await client.get( + `${ENDPOINT.ATTACH_SERVICE.ATTACHMENTS}/${atflCd}`, + ); + return response.data; + } +} + +export const AttachmentApi = new AttachmentApiService(); diff --git a/apps/cs/src/entities/attachment/index.ts b/apps/cs/src/entities/attachment/index.ts new file mode 100644 index 000000000..182ef9fd6 --- /dev/null +++ b/apps/cs/src/entities/attachment/index.ts @@ -0,0 +1 @@ +export { AttachmentApi } from "./api/attachment-service"; diff --git a/apps/cs/src/entities/attachment/model/attachment-interface.ts b/apps/cs/src/entities/attachment/model/attachment-interface.ts new file mode 100644 index 000000000..367e54127 --- /dev/null +++ b/apps/cs/src/entities/attachment/model/attachment-interface.ts @@ -0,0 +1,18 @@ +export interface Attachment { + atflCd: string; + atflSn: number; + atflId: string; + physFileNm: string; + orgnlFileNm: string; + atflFlieSz: number; + atflType: string; + dwnldNocs: number; + delYn: string; + lnkgDmnId: string; + lnkgDmnNm: string; + regDt: string; +} + +export interface AttachmentService { + getAttachments: (atflCd: string) => Promise; +} diff --git a/apps/cs/src/pages/image/ui/image-form.tsx b/apps/cs/src/pages/image/ui/image-form.tsx index e9fcdc9a6..62f8ac41c 100644 --- a/apps/cs/src/pages/image/ui/image-form.tsx +++ b/apps/cs/src/pages/image/ui/image-form.tsx @@ -1,7 +1,9 @@ "use client"; -import { useState } from "react"; +import Image from "next/image"; +import { useEffect, useState } from "react"; import { useRouter } from "next/navigation"; +import { useQuery } from "@tanstack/react-query"; import type { UseFormReturn } from "react-hook-form"; import type { HttpResponse, Upload } from "tus-js-client"; import { useConfirm } from "@frontend-opensource/use-react-hooks"; @@ -20,10 +22,11 @@ import { TableRow, } from "@common/components"; import { CodeSelect } from "@/entities/code"; -import { ConfirmDialog } from "@/shared/ui/confirm-dialog"; +import { AttachmentApi } from "@/entities/attachment"; +import { ENDPOINT } from "@/shared/config"; import { Form, FormControl, FormField, FormItem } from "@/shared/ui/form"; -import { handleUpload } from "../lib/utils"; import type { ImageFormData } from "../model/image-interface"; +import { handleUpload } from "../lib/utils"; type ImageFormProps = { form: UseFormReturn; @@ -38,49 +41,50 @@ function ImageForm({ form, imgSn, handleSave, handleDelete }: ImageFormProps) { const router = useRouter(); const { confirm } = useConfirm(); - const [onConfirm, setOnConfirm] = useState(false); + const [attachmentFileId, setAttachmentFileId] = useState(""); const [upload, setUpload] = useState(null); const [progress, setProgress] = useState(0); - const handleConfirm = async (callback: () => Promise) => { - setOnConfirm(true); - await callback(); - setOnConfirm(false); - }; + const { data: atflId } = useQuery({ + queryKey: [ENDPOINT.ATTACH_SERVICE.ATTACHMENTS], + queryFn: () => AttachmentApi.getAttachments(form.getValues("atflCd")!), + enabled: !!form.getValues("atflCd"), + select: (data) => data?.[data.length - 1]?.atflId, + }); const onSubmit = async (data: ImageFormData) => { const confirmMessage = isModifyMode ? "수정하시겠습니까?" : "저장하시겠습니까?"; - await handleConfirm(async () => { - if (await confirm(confirmMessage)) { - setOnConfirm(false); - await handleSave(data); - } - }); + if (await confirm(confirmMessage)) { + await handleSave(data); + } }; const onDelete = async () => { - await handleConfirm(async () => { - if (await confirm("삭제하시겠습니까?")) { - setOnConfirm(false); - await handleDelete?.(); - } - }); + if (await confirm("삭제하시겠습니까?")) { + await handleDelete?.(); + } }; const handleProgress = (percentage: string) => { setProgress(Number(percentage)); }; - const handleUploadResponse = (res: HttpResponse) => { + const handleUploadResponse = async (res: HttpResponse) => { const fileInfo = res.getHeader("X-File-Info"); if (fileInfo === undefined || fileInfo === null) return; - form.setValue("atflCd", JSON.parse(fileInfo)?.atflCd); + const { atflCd, atflId } = JSON.parse(fileInfo) ?? {}; + if (atflCd) form.setValue("atflCd", atflCd); + if (atflId) setAttachmentFileId(atflId); }; + useEffect(() => { + if (atflId) setAttachmentFileId(atflId); + }, [atflId]); + return ( <>
@@ -204,6 +208,7 @@ function ImageForm({ form, imgSn, handleSave, handleDelete }: ImageFormProps) { { const files = e.target.files; if (!files) return; @@ -221,7 +226,16 @@ function ImageForm({ form, imgSn, handleSave, handleDelete }: ImageFormProps) {

{(upload?.file as File)?.name}

-
+
+ {attachmentFileId && ( + 업로드된 이미지 + )} +
- ); } diff --git a/apps/cs/src/shared/config/api.ts b/apps/cs/src/shared/config/api.ts index 0a1ec0122..c5d3ac937 100644 --- a/apps/cs/src/shared/config/api.ts +++ b/apps/cs/src/shared/config/api.ts @@ -5,6 +5,8 @@ export const BASE_URL = `${PROXY_HOST}/server`; export const ENDPOINT = { ATTACH_SERVICE: { TUS_UPLOAD: "/attach-service/api/v1/tus/upload", + IMAGES: "/attach-service/api/v1/images", + ATTACHMENTS: "/attach-service/api/v1/atfl/attachments", }, USER_SERVICE: { LOGIN: "/user-service/login", diff --git a/packages/common/components/src/ui/file-uploader/index.tsx b/packages/common/components/src/ui/file-uploader/index.tsx index da3e04b67..d86566492 100644 --- a/packages/common/components/src/ui/file-uploader/index.tsx +++ b/packages/common/components/src/ui/file-uploader/index.tsx @@ -26,8 +26,8 @@ export function FileUploader({ id={id} type="file" className="sr-only" - {...props} onChange={onUpload} + {...props} /> {customPreview || (