Files
frontend/apps/st/src/components/statistics/verification/generalVerification/CopyDialog.tsx
T

149 lines
5.1 KiB
TypeScript

import { useEffect,useState } from "react";
import { useConfirm } from "@frontend-opensource/use-react-hooks";
import { useAlertStore } from "@/shared/lib";
import { CodeSelect } from "@/entities/code";
import { generalverificationApiService } from "@/entities/statistics/api/general-verification-service";
import type { GeneralverificationList } from "@/entities/statistics/model/general-verification-interface";
import {
Button,
Dialog,
DialogClose,
DialogContent,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
Input,
Label,
} from "@common/components";
import { LoadingWithAnimation } from "@common/components";
import { getUserInfo } from "../../../../../../../packages/common/components/src/api";
interface CopyDialogProps {
open: boolean;
onOpenChange: (open: boolean) => void;
children?: React.ReactNode;
row?: GeneralverificationList;
/** 수정/등록 성공 시 부모에서 목록 재조회 등 실행 */
onSuccess?: () => void;
}
const CopyDialog = ({
children,
open,
onOpenChange,
row,
onSuccess,
...props
}: CopyDialogProps) => {
const [selectedCode, setSelectedCode] = useState<string>('');
const [isLoading, setIsLoading] = useState<boolean>(false);
const { setMessage: alert } = useAlertStore((state) => state);
const { confirm } = useConfirm();
const handleChangeCode = (value: string) => {
if (row?.trgtStatsCd === value) {
alert('원본과 동일한 통계항목으로 복사할 경우\n복사 완료 후 검증 분류를 꼭 수정해주세요.');
}
setSelectedCode(value);
};
const handleCopyItem = async () => {
if (!selectedCode) {
alert('복사 대상 통계항목을 선택해주세요.');
return;
}
if (await confirm('복사하시겠습니까?')) {
try {
setIsLoading(true);
const userInfo = await getUserInfo();
const response = await generalverificationApiService.CopyVrfcData(
{
vrfcSn: row?.vrfcSn ?? null,
targetStatsCd: selectedCode ?? '',
rgtrUuid: userInfo.userUnqId,
},
userInfo.userUnqId,
);
if (response.status === 'success') {
alert('복사되었습니다.');
onOpenChange(false);
onSuccess?.();
} else {
alert(response.message || '데이터 복사 중 오류가 발생했습니다.');
}
} catch {
alert('데이터 복사 중 오류가 발생했습니다.')
} finally {
setIsLoading(false);
}
}
};
useEffect(() => {
if (!open) setSelectedCode('');
}, [open]);
return (
<Dialog open={open} onOpenChange={onOpenChange} {...props}>
<DialogTrigger asChild>{children}</DialogTrigger>
<DialogContent className="max-w-[60rem]" aria-describedby={undefined}>
<DialogHeader>
<DialogTitle>
{row?.vrfcNm}
</DialogTitle>
</DialogHeader>
{isLoading &&
<div className="absolute inset-0 z-50 [data-theme='black']:bg-transparent rounded-lg p-10 flex justify-center items-center">
<div className="relative size-[5rem]">
<LoadingWithAnimation color="primary" />
</div>
</div>
}
<div className="flex flex-col gap-4 m-2">
<div className="flex flex-col gap-2">
<Label className="text-label">
원본 통계항목
</Label>
<Input
value={row?.trgtStatsNm}
disabled
className="w-full"
/>
</div>
<div className="flex flex-col gap-2">
<Label className="text-label">
대상 통계항목
</Label>
<CodeSelect
upCd="STITEM"
value={selectedCode}
onValueChange={handleChangeCode}
placeholder="선택"
className="w-full"
/>
</div>
</div>
<DialogFooter>
<Button size="lg" type="button" onClick={handleCopyItem}>
복사
</Button>
<DialogClose asChild>
<Button size="lg" color="white">
취소
</Button>
</DialogClose>
</DialogFooter>
</DialogContent>
</Dialog>
);
}
export default CopyDialog;