공통코드 수정중

This commit is contained in:
kjs
2025-12-22 13:45:08 +09:00
parent ac526c8578
commit b01efd293c
9 changed files with 560 additions and 35 deletions

View File

@@ -9,6 +9,7 @@ import { Input } from "@/components/ui/input";
import { Textarea } from "@/components/ui/textarea";
import { Label } from "@/components/ui/label";
import { Switch } from "@/components/ui/switch";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
import { LoadingSpinner } from "@/components/common/LoadingSpinner";
import { ValidationMessage } from "@/components/common/ValidationMessage";
@@ -83,6 +84,9 @@ export function CodeFormModal({ isOpen, onClose, categoryCode, editingCode, code
// 폼 스키마 선택 (생성/수정에 따라)
const schema = isEditing ? updateCodeSchema : createCodeSchema;
// 부모 코드 선택 상태
const [parentCodeValue, setParentCodeValue] = useState<string>("");
const form = useForm({
resolver: zodResolver(schema),
mode: "onChange", // 실시간 검증 활성화
@@ -111,6 +115,9 @@ export function CodeFormModal({ isOpen, onClose, categoryCode, editingCode, code
// codeValue는 별도로 설정 (표시용)
form.setValue("codeValue" as any, editingCode.codeValue || editingCode.code_value);
// 부모 코드 설정
setParentCodeValue(editingCode.parentCodeValue || editingCode.parent_code_value || "");
} else {
// 새 코드 모드: 자동 순서 계산
const maxSortOrder = codes.length > 0 ? Math.max(...codes.map((c) => c.sortOrder || c.sort_order)) : 0;
@@ -122,6 +129,7 @@ export function CodeFormModal({ isOpen, onClose, categoryCode, editingCode, code
description: "",
sortOrder: maxSortOrder + 1,
});
setParentCodeValue("");
}
}
}, [isOpen, isEditing, editingCode, codes]);
@@ -129,22 +137,29 @@ export function CodeFormModal({ isOpen, onClose, categoryCode, editingCode, code
const handleSubmit = form.handleSubmit(async (data) => {
try {
if (isEditing && editingCode) {
// 수정
// 수정 - 부모 코드 포함
await updateCodeMutation.mutateAsync({
categoryCode,
codeValue: editingCode.codeValue || editingCode.code_value,
data: data as UpdateCodeData,
data: {
...data,
parentCodeValue: parentCodeValue || undefined,
} as UpdateCodeData,
});
} else {
// 생성
// 생성 - 부모 코드 포함
await createCodeMutation.mutateAsync({
categoryCode,
data: data as CreateCodeData,
data: {
...data,
parentCodeValue: parentCodeValue || undefined,
} as CreateCodeData,
});
}
onClose();
form.reset();
setParentCodeValue("");
} catch (error) {
console.error("코드 저장 실패:", error);
}
@@ -269,6 +284,43 @@ export function CodeFormModal({ isOpen, onClose, categoryCode, editingCode, code
)}
</div>
{/* 부모 코드 (계층 구조) */}
<div className="space-y-2">
<Label htmlFor="parentCodeValue" className="text-xs sm:text-sm"> ()</Label>
<Select
value={parentCodeValue || "_none_"}
onValueChange={(val) => setParentCodeValue(val === "_none_" ? "" : val)}
disabled={isLoading}
>
<SelectTrigger className="h-8 text-xs sm:h-10 sm:text-sm">
<SelectValue placeholder="부모 코드 선택 (최상위면 비워두세요)" />
</SelectTrigger>
<SelectContent>
<SelectItem value="_none_"> ()</SelectItem>
{codes
.filter((c) => {
// 자기 자신은 제외
const currentCodeValue = editingCode?.codeValue || editingCode?.code_value;
const codeValue = c.codeValue || c.code_value;
return codeValue !== currentCodeValue;
})
.map((c) => {
const codeValue = c.codeValue || c.code_value;
if (!codeValue) return null;
return (
<SelectItem key={codeValue} value={codeValue}>
{c.depth && c.depth > 1 ? "└".repeat(c.depth - 1) + " " : ""}
{c.codeName || c.code_name} ({codeValue})
</SelectItem>
);
})}
</SelectContent>
</Select>
<p className="text-[10px] sm:text-xs text-muted-foreground">
.
</p>
</div>
{/* 정렬 순서 */}
<div className="space-y-2">
<Label htmlFor="sortOrder" className="text-xs sm:text-sm"> </Label>