[as] feat: 설치인가 보완요청 및 완료 기능 구현
This commit is contained in:
@@ -35,4 +35,5 @@ export {
|
||||
type SplmntDmndDialogConfirmItem,
|
||||
type SplmntDmndDialogConfirmPayload,
|
||||
} from "./ui/splmnt-dmnd-dialog";
|
||||
export { type SplmntDmndFormItem, SplmntDmndFormSection } from "./ui/splmnt-dmnd-form-section";
|
||||
export { TaskSePickerDialog } from "./ui/task-se-picker-dialog";
|
||||
|
||||
@@ -0,0 +1,324 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useId, useRef, useState } from "react";
|
||||
import { Controller, useFormContext, useWatch } from "react-hook-form";
|
||||
|
||||
import { Paperclip } from "lucide-react";
|
||||
|
||||
import { dayjs, getDefaultFileKey, useTus } from "@/shared/lib";
|
||||
import { attachService, useDownloadAttachmentByAtflCd } from "@/entities/attach";
|
||||
import {
|
||||
Badge,
|
||||
Button,
|
||||
Table,
|
||||
TableBody,
|
||||
TableCell,
|
||||
TableHead,
|
||||
TableHeader,
|
||||
TableRow,
|
||||
Textarea,
|
||||
} from "@common/components";
|
||||
|
||||
import { SectionCollapsible } from "./section-collapsible";
|
||||
|
||||
const SPLMNT_CMPTN_FILE_KEY_PREFIX = "splmnt-cmptn::";
|
||||
|
||||
interface UploadedSplmntFile {
|
||||
fileName: string;
|
||||
fileKey: string;
|
||||
atflId?: string;
|
||||
atflCd?: string;
|
||||
}
|
||||
|
||||
interface SplmntDmndFormItem {
|
||||
splmntDmndSn?: number;
|
||||
splmntArtclComCd?: string;
|
||||
splmntArtclCdNm?: string | null;
|
||||
splmntDmndCn?: string;
|
||||
splmntDmndDt?: string;
|
||||
splmntDmndAtchFileGroupId?: string;
|
||||
splmntDmndAtchFileNm?: string;
|
||||
splmntCn?: string;
|
||||
splmntPicNm?: string;
|
||||
splmntCmptnDt?: string;
|
||||
splmntAtchFileGroupId?: string;
|
||||
splmntAtchFileNm?: string;
|
||||
splmntAtchFileAtflId?: string;
|
||||
cmptnYn?: string;
|
||||
}
|
||||
|
||||
interface SplmntDmndFormData {
|
||||
splmntDmndList: SplmntDmndFormItem[];
|
||||
}
|
||||
|
||||
interface SplmntDmndFormSectionProps {
|
||||
readOnly?: boolean;
|
||||
}
|
||||
|
||||
function createUploadKey(rowKey: string, file: File) {
|
||||
return `${SPLMNT_CMPTN_FILE_KEY_PREFIX}${rowKey}::${getDefaultFileKey(file)}`;
|
||||
}
|
||||
|
||||
function getRowKeyFromFileKey(fileKey: string): string | null {
|
||||
if (!fileKey.startsWith(SPLMNT_CMPTN_FILE_KEY_PREFIX)) return null;
|
||||
const rest = fileKey.slice(SPLMNT_CMPTN_FILE_KEY_PREFIX.length);
|
||||
const separatorIndex = rest.indexOf("::");
|
||||
if (separatorIndex < 0) return null;
|
||||
return rest.slice(0, separatorIndex);
|
||||
}
|
||||
|
||||
function getRowKey(item: SplmntDmndFormItem, index: number) {
|
||||
return item.splmntDmndSn != null ? String(item.splmntDmndSn) : `idx-${index}`;
|
||||
}
|
||||
|
||||
function SplmntDmndFormSection({ readOnly = false }: SplmntDmndFormSectionProps) {
|
||||
const formId = useId();
|
||||
const { control, setValue, getValues } = useFormContext<SplmntDmndFormData>();
|
||||
const splmntDmndList = useWatch({ control, name: "splmntDmndList" }) ?? [];
|
||||
const [uploadedFiles, setUploadedFiles] = useState<Record<string, UploadedSplmntFile | null>>({});
|
||||
const initialFilesMappedRef = useRef(false);
|
||||
const { mutate: downloadAttachment } = useDownloadAttachmentByAtflCd();
|
||||
|
||||
const { uploadFileAsync, isUploading, uploads } = useTus({
|
||||
onSuccess: (atflCd, atflId, _file, fileKey) => {
|
||||
if (!atflCd || !atflId) return;
|
||||
const rowKey = getRowKeyFromFileKey(fileKey);
|
||||
if (!rowKey) return;
|
||||
|
||||
setUploadedFiles((prev) => {
|
||||
const current = prev[rowKey];
|
||||
if (!current || current.fileKey !== fileKey) return prev;
|
||||
return {
|
||||
...prev,
|
||||
[rowKey]: { ...current, atflCd, atflId },
|
||||
};
|
||||
});
|
||||
|
||||
const list = getValues("splmntDmndList") ?? [];
|
||||
const index = list.findIndex((item, idx) => getRowKey(item, idx) === rowKey);
|
||||
if (index < 0) return;
|
||||
|
||||
setValue(`splmntDmndList.${index}.splmntAtchFileGroupId`, atflCd, { shouldDirty: true });
|
||||
setValue(`splmntDmndList.${index}.splmntAtchFileAtflId`, atflId, { shouldDirty: true });
|
||||
},
|
||||
onError: (_error, _file, fileKey) => {
|
||||
const rowKey = getRowKeyFromFileKey(fileKey);
|
||||
if (!rowKey) return;
|
||||
|
||||
setUploadedFiles((prev) => {
|
||||
const current = prev[rowKey];
|
||||
if (!current || current.fileKey !== fileKey) return prev;
|
||||
return { ...prev, [rowKey]: null };
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (initialFilesMappedRef.current) return;
|
||||
if (splmntDmndList.length === 0) return;
|
||||
|
||||
const initial: Record<string, UploadedSplmntFile | null> = {};
|
||||
for (let index = 0; index < splmntDmndList.length; index += 1) {
|
||||
const item = splmntDmndList[index];
|
||||
const rowKey = getRowKey(item, index);
|
||||
if (item.splmntAtchFileGroupId && item.splmntAtchFileNm) {
|
||||
initial[rowKey] = {
|
||||
fileName: item.splmntAtchFileNm,
|
||||
fileKey: `${SPLMNT_CMPTN_FILE_KEY_PREFIX}${rowKey}::initial`,
|
||||
atflCd: item.splmntAtchFileGroupId,
|
||||
atflId: item.splmntAtchFileAtflId,
|
||||
};
|
||||
} else {
|
||||
initial[rowKey] = null;
|
||||
}
|
||||
}
|
||||
|
||||
setUploadedFiles(initial);
|
||||
initialFilesMappedRef.current = true;
|
||||
}, [splmntDmndList]);
|
||||
|
||||
if (splmntDmndList.length === 0) return null;
|
||||
|
||||
async function handleFileUpload(index: number, files: FileList | null) {
|
||||
if (!files || files.length === 0) return;
|
||||
|
||||
const item = splmntDmndList[index];
|
||||
const rowKey = getRowKey(item, index);
|
||||
const file = files[0];
|
||||
const fileKey = createUploadKey(rowKey, file);
|
||||
const existingAtflCd = item.splmntAtchFileGroupId;
|
||||
|
||||
setUploadedFiles((prev) => ({
|
||||
...prev,
|
||||
[rowKey]: { fileName: file.name, fileKey },
|
||||
}));
|
||||
setValue(`splmntDmndList.${index}.splmntAtchFileNm`, file.name, { shouldDirty: true });
|
||||
|
||||
try {
|
||||
await uploadFileAsync(file, {
|
||||
fileKey,
|
||||
metadata: existingAtflCd ? { atflCd: existingAtflCd } : undefined,
|
||||
});
|
||||
} catch {
|
||||
// onError callback handles cleanup
|
||||
}
|
||||
}
|
||||
|
||||
async function handleFileRemove(index: number) {
|
||||
const item = splmntDmndList[index];
|
||||
const rowKey = getRowKey(item, index);
|
||||
const uploaded = uploadedFiles[rowKey];
|
||||
if (!uploaded) return;
|
||||
|
||||
const { fileKey, atflId } = uploaded;
|
||||
uploads[fileKey]?.abort();
|
||||
|
||||
if (atflId) {
|
||||
try {
|
||||
await attachService.deleteAttachmentByAtflId(atflId);
|
||||
} catch (error) {
|
||||
console.error("보완완료 첨부파일 삭제 실패:", error);
|
||||
}
|
||||
}
|
||||
|
||||
setUploadedFiles((prev) => ({ ...prev, [rowKey]: null }));
|
||||
setValue(`splmntDmndList.${index}.splmntAtchFileGroupId`, "", { shouldDirty: true });
|
||||
setValue(`splmntDmndList.${index}.splmntAtchFileNm`, "", { shouldDirty: true });
|
||||
setValue(`splmntDmndList.${index}.splmntAtchFileAtflId`, "", { shouldDirty: true });
|
||||
}
|
||||
|
||||
return (
|
||||
<SectionCollapsible title="보완요청">
|
||||
<div className="overflow-x-auto">
|
||||
<Table variant="secondary">
|
||||
<colgroup>
|
||||
<col width="12%" />
|
||||
<col width="14%" />
|
||||
<col width="22%" />
|
||||
<col width="22%" />
|
||||
<col width="18%" />
|
||||
<col width="12%" />
|
||||
</colgroup>
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
<TableHead>보완항목</TableHead>
|
||||
<TableHead>보완요청일시</TableHead>
|
||||
<TableHead>보완요청내용</TableHead>
|
||||
<TableHead>보완완료내용</TableHead>
|
||||
<TableHead>파일첨부</TableHead>
|
||||
<TableHead>완료여부</TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{splmntDmndList.map((item, index) => {
|
||||
const rowKey = getRowKey(item, index);
|
||||
const uploaded = uploadedFiles[rowKey] ?? null;
|
||||
const uploading = uploaded ? (isUploading[uploaded.fileKey] ?? false) : false;
|
||||
const fileInputId = `${formId}-${rowKey}-file`;
|
||||
const isRowReadOnly = readOnly || item.cmptnYn === "Y";
|
||||
|
||||
return (
|
||||
<TableRow key={rowKey}>
|
||||
<TableCell align="center" className="align-top">
|
||||
{item.splmntArtclCdNm || "-"}
|
||||
</TableCell>
|
||||
<TableCell align="center" className="whitespace-nowrap align-top">
|
||||
{item.splmntDmndDt ? dayjs(item.splmntDmndDt).format("YYYY-MM-DD HH:mm") : "-"}
|
||||
</TableCell>
|
||||
<TableCell className="align-top">
|
||||
<div className="space-y-2">
|
||||
<div className="whitespace-pre-wrap">{item.splmntDmndCn || "-"}</div>
|
||||
{item.splmntDmndAtchFileGroupId ? (
|
||||
<a
|
||||
className="inline-flex cursor-pointer items-start gap-1 text-blue-600 hover:underline"
|
||||
onClick={() => downloadAttachment(item.splmntDmndAtchFileGroupId!)}
|
||||
>
|
||||
<Paperclip className="my-1 size-5 shrink-0" />
|
||||
{item.splmntDmndAtchFileNm || "다운로드"}
|
||||
</a>
|
||||
) : null}
|
||||
</div>
|
||||
</TableCell>
|
||||
<TableCell className="align-top">
|
||||
{isRowReadOnly ? (
|
||||
<span className="whitespace-pre-wrap">{item.splmntCn || "-"}</span>
|
||||
) : (
|
||||
<Controller
|
||||
control={control}
|
||||
name={`splmntDmndList.${index}.splmntCn`}
|
||||
render={({ field }) => (
|
||||
<Textarea
|
||||
value={field.value ?? ""}
|
||||
onChange={field.onChange}
|
||||
onBlur={field.onBlur}
|
||||
placeholder="보완완료 내용을 입력하세요."
|
||||
className="min-h-[6rem] !text-xl"
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
)}
|
||||
</TableCell>
|
||||
<TableCell className="align-top">
|
||||
{uploaded ? (
|
||||
<div className="flex items-stretch gap-2">
|
||||
{uploaded.atflCd || item.splmntAtchFileGroupId ? (
|
||||
<a
|
||||
className="inline-flex cursor-pointer items-start gap-1 text-blue-600 hover:underline"
|
||||
onClick={() =>
|
||||
downloadAttachment((uploaded.atflCd || item.splmntAtchFileGroupId)!)
|
||||
}
|
||||
>
|
||||
<Paperclip className="my-1 size-5 shrink-0" />
|
||||
{uploaded.fileName}
|
||||
</a>
|
||||
) : (
|
||||
<span className="text-xl">{uploaded.fileName}</span>
|
||||
)}
|
||||
{!isRowReadOnly ? (
|
||||
<Button
|
||||
type="button"
|
||||
size="icon"
|
||||
className="size-8 shrink-0 !bg-destructive"
|
||||
disabled={uploading}
|
||||
onClick={() => void handleFileRemove(index)}
|
||||
>
|
||||
<i className="diveicon di-x text-xs" aria-label="삭제" />
|
||||
</Button>
|
||||
) : null}
|
||||
</div>
|
||||
) : isRowReadOnly ? (
|
||||
<span>-</span>
|
||||
) : (
|
||||
<Button type="button" size="sm" disabled={uploading} asChild>
|
||||
<label htmlFor={fileInputId} className="cursor-pointer">
|
||||
파일추가
|
||||
<input
|
||||
id={fileInputId}
|
||||
type="file"
|
||||
className="hidden"
|
||||
onChange={(event) => {
|
||||
void handleFileUpload(index, event.target.files);
|
||||
event.target.value = "";
|
||||
}}
|
||||
/>
|
||||
</label>
|
||||
</Button>
|
||||
)}
|
||||
</TableCell>
|
||||
<TableCell align="center" className="align-top">
|
||||
<Badge size="lg" color={item.cmptnYn === "Y" ? "green" : "gray"}>
|
||||
{item.cmptnYn === "Y" ? "완료" : "미완료"}
|
||||
</Badge>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
);
|
||||
})}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</div>
|
||||
</SectionCollapsible>
|
||||
);
|
||||
}
|
||||
|
||||
export { SplmntDmndFormSection };
|
||||
export type { SplmntDmndFormItem };
|
||||
Reference in New Issue
Block a user