234 lines
6.7 KiB
TypeScript
234 lines
6.7 KiB
TypeScript
'use client'
|
|
|
|
import { useEffect, useState } from 'react'
|
|
import { KriDialog } from '@/components/common/KriDialog'
|
|
import { Button } from '@/components/ui/button'
|
|
import { Input } from '@/components/ui/input'
|
|
import { Label } from '@/components/ui/label'
|
|
import { useDialog } from '@/hooks/useDialog'
|
|
import type { DialogId } from '@/store/jotai/dialog'
|
|
import type { CmpnInfoSaveRequest } from '@/types/cmpn'
|
|
import type { DataVerification } from "@kri/common";
|
|
import { apiError, NONE_HTTP_ERROR, UNKNOWN_HTTP_ERROR } from "@kri/common/http";
|
|
|
|
export type CmpnUpsertDialogProps = {
|
|
dialogId?: DialogId
|
|
open?: boolean
|
|
onOpenChange?: (open: boolean) => void
|
|
mode?: 'create' | 'edit'
|
|
initialData?: Partial<CmpnInfoSaveRequest>
|
|
onSubmit: (data: CmpnInfoSaveRequest) => void | Promise<void>
|
|
onCheckDuplicate?: (cmpnId: string) => DataVerification | Promise<DataVerification>
|
|
}
|
|
|
|
const createDefaultCmpnInfo = (): CmpnInfoSaveRequest => ({
|
|
cmpnId: '',
|
|
cmpnAlias: '',
|
|
cmpnTel: '',
|
|
ownCmpn: false,
|
|
memo: '',
|
|
})
|
|
|
|
const CmpnUpsertDialog = ({
|
|
dialogId = 'cmpnUpsertDialog',
|
|
open,
|
|
onOpenChange,
|
|
mode = 'create',
|
|
initialData,
|
|
onSubmit,
|
|
onCheckDuplicate,
|
|
}: CmpnUpsertDialogProps) => {
|
|
const { isDialogOpen, showDialog, hideDialog } = useDialog();
|
|
|
|
const [formData, setFormData] = useState<CmpnInfoSaveRequest>(createDefaultCmpnInfo);
|
|
const [isCheckedDuplicate, setIsCheckedDuplicate] = useState(false);
|
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
|
|
const resolvedOpen = open ?? isDialogOpen(dialogId);
|
|
|
|
const handleOpenChange = (nextOpen: boolean) => {
|
|
onOpenChange?.(nextOpen);
|
|
|
|
if (nextOpen) {
|
|
showDialog(dialogId);
|
|
} else {
|
|
hideDialog(dialogId);
|
|
}
|
|
}
|
|
|
|
useEffect(() => {
|
|
if (initialData) {
|
|
setFormData({
|
|
...createDefaultCmpnInfo(),
|
|
...initialData,
|
|
});
|
|
|
|
if (mode === 'edit') {
|
|
setIsCheckedDuplicate(true);
|
|
}
|
|
} else {
|
|
setFormData(createDefaultCmpnInfo());
|
|
setIsCheckedDuplicate(false);
|
|
}
|
|
}, [initialData, mode, resolvedOpen]);
|
|
|
|
const handleInputChange = (field: keyof CmpnInfoSaveRequest, value: string) => {
|
|
setFormData((prev) => ({
|
|
...prev,
|
|
[field]: value,
|
|
}));
|
|
|
|
if (field === 'cmpnId' && mode === 'create') {
|
|
setIsCheckedDuplicate(false);
|
|
}
|
|
}
|
|
|
|
const handleCheckDuplicate = async () => {
|
|
const cmpnId = formData.cmpnId.trim();
|
|
if (!cmpnId) {
|
|
alert("회사 ID를 입력해주세요.");
|
|
return;
|
|
}
|
|
|
|
if (onCheckDuplicate) {
|
|
const result = await onCheckDuplicate(cmpnId);
|
|
if (!result.success) {
|
|
alert(result.errMessage);
|
|
setIsCheckedDuplicate(false);
|
|
} else {
|
|
alert("사용 가능한 회사 ID입니다.");
|
|
setIsCheckedDuplicate(true);
|
|
}
|
|
} else {
|
|
setIsCheckedDuplicate(true);
|
|
}
|
|
}
|
|
|
|
const handleSubmit = async () => {
|
|
if (!formData.cmpnId.trim()) {
|
|
alert("회사 ID를 입력해주세요.");
|
|
return;
|
|
}
|
|
|
|
if (mode === 'create' && !isCheckedDuplicate) {
|
|
alert("회사 ID 중복체크를 해주세요.");
|
|
return;
|
|
}
|
|
|
|
if (!formData.cmpnAlias.trim()) {
|
|
alert("별칭을 입력해주세요.");
|
|
return;
|
|
}
|
|
|
|
setIsSubmitting(true);
|
|
|
|
try {
|
|
await onSubmit({
|
|
...formData,
|
|
cmpnId: formData.cmpnId.trim(),
|
|
cmpnAlias: formData.cmpnAlias.trim(),
|
|
cmpnTel: formData.cmpnTel?.trim(),
|
|
ownCmpn: mode === 'create' ? false : formData.ownCmpn,
|
|
memo: formData.memo?.trim(),
|
|
});
|
|
handleOpenChange(false);
|
|
} catch (e) {
|
|
const err = apiError(e, {
|
|
fallbackMessage: "Save failed.",
|
|
});
|
|
console.error(err.status, err.code, err.message);
|
|
if (err.code !== NONE_HTTP_ERROR && err.code !== UNKNOWN_HTTP_ERROR) {
|
|
alert(err.message);
|
|
} else {
|
|
alert("저장에 실패했습니다.");
|
|
}
|
|
} finally {
|
|
setIsSubmitting(false);
|
|
}
|
|
}
|
|
|
|
return (
|
|
<KriDialog
|
|
open={resolvedOpen}
|
|
onOpenChange={handleOpenChange}
|
|
title={<span className="text-xl">{mode === 'create' ? '회사 등록' : '회사 수정'}</span>}
|
|
description="회사 정보를 입력해주세요."
|
|
contentClassName="sm:max-w-[500px] text-sm"
|
|
bodyClassName="grid gap-4 py-4"
|
|
footer={
|
|
<>
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
onClick={() => handleOpenChange(false)}
|
|
>
|
|
취소
|
|
</Button>
|
|
<Button
|
|
type="submit"
|
|
onClick={handleSubmit}
|
|
disabled={isSubmitting}
|
|
>
|
|
{mode === 'create' ? "등록" : "수정"}
|
|
</Button>
|
|
</>
|
|
}
|
|
>
|
|
<div className="grid gap-2">
|
|
<Label htmlFor="cmpnId">
|
|
회사 ID<span className="text-red-500 ml-1">*</span>
|
|
</Label>
|
|
<div className="flex gap-2">
|
|
<Input
|
|
id="cmpnId"
|
|
placeholder="Enter company ID"
|
|
value={formData.cmpnId}
|
|
onChange={(e) => handleInputChange('cmpnId', e.target.value)}
|
|
disabled={mode === 'edit'}
|
|
/>
|
|
{mode === 'create' && (
|
|
<Button
|
|
variant="outline"
|
|
onClick={handleCheckDuplicate}
|
|
disabled={!formData.cmpnId.trim() || isCheckedDuplicate}
|
|
>
|
|
{isCheckedDuplicate ? "확인완료" : "중복체크"}
|
|
</Button>
|
|
)}
|
|
</div>
|
|
</div>
|
|
<div className="grid gap-2">
|
|
<Label htmlFor="cmpnAlias">별칭</Label>
|
|
<Input
|
|
id="cmpnAlias"
|
|
placeholder="Enter alias"
|
|
value={formData.cmpnAlias ?? ''}
|
|
onChange={(e) => handleInputChange('cmpnAlias', e.target.value)}
|
|
/>
|
|
</div>
|
|
<div className="grid gap-2">
|
|
<Label htmlFor="cmpnTel">전화번호</Label>
|
|
<Input
|
|
id="cmpnTel"
|
|
placeholder="Enter tel"
|
|
value={formData.cmpnTel ?? ''}
|
|
onChange={(e) => handleInputChange('cmpnTel', e.target.value)}
|
|
/>
|
|
</div>
|
|
<div className="grid gap-2">
|
|
<Label htmlFor="memo">메모</Label>
|
|
<textarea
|
|
id="memo"
|
|
className="flex min-h-[80px] w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50"
|
|
rows={3}
|
|
placeholder="Enter memo"
|
|
value={formData.memo ?? ''}
|
|
onChange={(e) => handleInputChange('memo', e.target.value)}
|
|
/>
|
|
</div>
|
|
</KriDialog>
|
|
)
|
|
}
|
|
|
|
export default CmpnUpsertDialog
|