"use client"; import Image from "next/image"; import { useEffect, useState } from "react"; import { useQuery } from "@tanstack/react-query"; import type { UseFormReturn } from "react-hook-form"; import type { HttpResponse } from "tus-js-client"; import { FileUploader, Input, Label, Progress, RadioGroup, RadioGroupItem, Table, TableBody, TableCell, TableHead, TableRow, } from "@common/components"; import { FormWrapper } from "@/widgets/form-wrapper"; import { CodeSelect } from "@/entities/code"; import { AttachmentApi } from "@/entities/attachment"; import { ENDPOINT } from "@/shared/config"; import { useTus } from "@/shared/lib/use-tus"; import { FormControl, FormField, FormItem } from "@/shared/ui/form"; import { useAttachmentStore } from "@/shared/lib/use-attachment-store"; import type { ImageFormData } from "../model/image-interface"; type ImageFormProps = { form: UseFormReturn; imgSn?: number; handleSave: (data: ImageFormData) => Promise; handleDelete?: () => Promise; }; function ImageForm({ form, imgSn, ...props }: ImageFormProps) { const atflYn = form.watch("atflYn"); const atflCd = form.watch("atflCd"); const { file: uploadFile, response: uploadResponse, progress, isValid, setFile, } = useTus({ accept: ".jpeg,.jpg,.png" }); const setAttachmentFileCode = useAttachmentStore( (state) => state.setAttachmentFileCode, ); const [imageUrl, setImageUrl] = useState(""); const { data: attachmentData } = useQuery({ queryKey: [ENDPOINT.ATTACH_SERVICE.ATTACHMENTS, atflCd], queryFn: () => AttachmentApi.getAttachmentsInfo(atflCd!), select: (data) => data?.[data.length - 1], enabled: !!atflCd, }); const handleUploadResponse = async (res: HttpResponse) => { const fileInfo = res.getHeader("X-File-Info"); if (fileInfo === undefined || fileInfo === null) return; const { atflCd, atflId } = JSON.parse(fileInfo) ?? {}; if (atflCd) form.setValue("atflCd", atflCd); if (atflId) setImageUrl(await AttachmentApi.getImageUrlByAtflId(atflId)); if (attachmentData) await AttachmentApi.deleteAttachmentByAtflCd(attachmentData.atflCd); }; useEffect(() => { if (!uploadResponse) return; handleUploadResponse(uploadResponse); }, [uploadResponse]); useEffect(() => { (async () => { setImageUrl( await AttachmentApi.getImageUrlByAtflCd(attachmentData?.atflCd ?? ""), ); })(); }, [attachmentData]); return (
이미지명 ( )} /> 정렬순서 ( field.onChange(parseInt(e.target.value)) } className="w-[12rem] !text-[1.3rem]" placeholder="정렬순서 입력" /> )} /> 이미지유형 ( )} /> 첨부파일 (
)} />
{atflYn === "Y" && ( 이미지파일 ( { const files = e.target.files; if (!files) return; setAttachmentFileCode(""); setFile(files[0] ?? null); }} customPreview={ <>
  • 권장 사이즈: 1920x500
  • 파일 최대 크기: 2MB
  • 확장자: jpg, jpeg, png 가능
} />
)} />
)} {atflYn === "N" && ( 외부링크 ( )} /> )} 사용여부 (
)} />
); } export { ImageForm };