코드 관리 페이지 추가

This commit is contained in:
2025-05-20 10:18:23 +09:00
parent 0d9d3c2fc5
commit b4f4839f99
21 changed files with 1269 additions and 3 deletions
+256
View File
@@ -0,0 +1,256 @@
import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { useMutation, useQuery } from "@tanstack/react-query";
import { useConfirm } from "@frontend-opensource/use-react-hooks";
import { ENDPOINT } from "@/shared/config";
import {
Button,
Dialog,
DialogClose,
DialogContent,
DialogFooter,
DialogHeader,
DialogTitle,
Table,
TableBody,
TableCell,
TableHead,
TableRow,
Input,
RadioGroup,
RadioGroupItem,
Label,
} from "@common/components";
import { Form, FormField, FormItem, FormControl } from "@/shared/ui/form";
import { useAlertStore } from "@/shared/lib/use-alert-store";
import { upCodeApi } from "@/entities/code/api/upcode-service";
import { upCodeFormSchema, type UpCodeFormData } from "@/entities/code/model/upcode-interface";
import { CodeSelect } from "@/entities/code";
import { SiteSelect } from "@/entities/site";
type ManagementUpCodeDialogProps = {
upCd?: string;
open?: boolean;
onOpenChange?: (open: boolean) => void;
isRegistration?: boolean;
} & Omit<React.ComponentProps<typeof Dialog>, 'open' | 'onOpenChange'>;
function ManagementUpCodeDialog({
upCd,
open,
onOpenChange,
isRegistration = false,
...props
}: ManagementUpCodeDialogProps) {
const { confirm } = useConfirm();
const { setMessage: alert } = useAlertStore((state) => state);
const { data: detailData } = useQuery({
queryKey: [ENDPOINT.CMS_SERVICE.UP_CODE, upCd],
queryFn: () => upCodeApi.upCodeDetail(upCd as string),
enabled: !!upCd && !isRegistration,
});
const { mutateAsync: saveUpCode } = useMutation({
mutationFn: (data: UpCodeFormData) =>
isRegistration ? upCodeApi.createUpCode(data) : upCodeApi.updateUpCode(upCd as string, data),
onSuccess: () => {
alert(isRegistration ? "등록되었습니다." : "수정되었습니다.");
onOpenChange?.(false);
},
});
const form = useForm<UpCodeFormData>({
resolver: zodResolver(upCodeFormSchema),
values: {
upCd: detailData?.upCd ?? "",
upCdNm: detailData?.upCdNm ?? "",
cdSe: detailData?.cdSe ?? "",
siteId: detailData?.siteId ?? "",
upCdExpln: detailData?.upCdExpln ?? "",
useYn: detailData?.useYn ?? "Y",
delYn: "N",
},
});
const handleSave = async () => {
if (await confirm(isRegistration ? "등록하시겠습니까?" : "수정하시겠습니까?")) {
const data = form.getValues();
await saveUpCode(data);
}
};
return (
<Dialog open={open} onOpenChange={onOpenChange} {...props}>
<DialogContent className="max-w-[60rem]" aria-describedby={undefined}>
<DialogHeader>
<DialogTitle>{isRegistration ? "상위코드 등록" : "상위코드 관리"}</DialogTitle>
</DialogHeader>
<Form {...form}>
<form onSubmit={(e) => {
e.preventDefault();
handleSave();
}} className="space-y-4">
<Table variant="secondary">
<colgroup>
<col width="15%" />
<col width="85%" />
</colgroup>
<TableBody>
<TableRow>
<TableHead>상위코드</TableHead>
<TableCell className="border">
<FormField
control={form.control}
name="upCd"
render={({ field }) => (
<FormItem>
<FormControl>
<Input
{...field}
value={field.value ?? ""}
className="!text-[1.3rem]"
placeholder="상위코드를를 입력해주세요"
disabled={!isRegistration}
/>
</FormControl>
</FormItem>
)}
/>
</TableCell>
</TableRow>
<TableRow>
<TableHead>상위코드명</TableHead>
<TableCell className="border">
<FormField
control={form.control}
name="upCdNm"
render={({ field }) => (
<FormItem>
<FormControl>
<Input
{...field}
value={field.value ?? ""}
className="!text-[1.3rem]"
placeholder="상위코드명을 입력해주세요"
/>
</FormControl>
</FormItem>
)}
/>
</TableCell>
</TableRow>
<TableRow>
<TableHead>상위코드설명</TableHead>
<TableCell className="border">
<FormField
control={form.control}
name="upCdExpln"
render={({ field }) => (
<FormItem>
<FormControl>
<Input
{...field}
value={field.value ?? ""}
className="!text-[1.3rem]"
placeholder="상위코드설명을 입력해주세요"
/>
</FormControl>
</FormItem>
)}
/>
</TableCell>
</TableRow>
<TableRow>
<TableHead>코드구분</TableHead>
<TableCell className="border">
<FormField
control={form.control}
name="cdSe"
render={({ field }) => (
<FormItem className="flex flex-col">
<FormControl>
<CodeSelect
upCd="CODECL"
value={field.value || ""}
onValueChange={field.onChange}
/>
</FormControl>
</FormItem>
)}
/>
</TableCell>
</TableRow>
<TableRow>
<TableHead>사이트</TableHead>
<TableCell className="border">
<FormField
control={form.control}
name="siteId"
render={({ field }) => (
<FormItem className="flex flex-col">
<FormControl>
<SiteSelect
siteId={field.value || ""}
value={field.value || ""}
onValueChange={field.onChange}
/>
</FormControl>
</FormItem>
)}
/>
</TableCell>
</TableRow>
<TableRow>
<TableHead>사용여부</TableHead>
<TableCell className="border">
<FormField
control={form.control}
name="useYn"
render={({ field }) => (
<FormItem>
<FormControl>
<RadioGroup
{...field}
name="useYn"
onValueChange={field.onChange}
>
<div className="flex items-center space-x-2">
<RadioGroupItem value="Y" id="useY" />
<Label htmlFor="useY">사용</Label>
</div>
<div className="flex items-center space-x-2">
<RadioGroupItem value="N" id="useN" />
<Label htmlFor="useN">사용안함</Label>
</div>
</RadioGroup>
</FormControl>
</FormItem>
)}
/>
</TableCell>
</TableRow>
</TableBody>
</Table>
<DialogFooter className="!justify-center">
<Button size="lg" type="submit" onClick={(e) => {
e.preventDefault();
handleSave();
}}>
{isRegistration ? "등록" : "저장"}
</Button>
<DialogClose asChild>
<Button size="lg" color="white">
취소
</Button>
</DialogClose>
</DialogFooter>
</form>
</Form>
</DialogContent>
</Dialog>
);
}
export { ManagementUpCodeDialog };