280 lines
8.4 KiB
TypeScript
280 lines
8.4 KiB
TypeScript
'use client'
|
|
|
|
import { useEffect, useRef, 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 { COMMON_TEXT_REGEX } from "@/const/validationConst";
|
|
import { validateValue } from "@/utils/validation";
|
|
import { formatKoreanPhoneNumber } from "@/utils/phone";
|
|
|
|
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 [invalidField, setInvalidField] = useState<'cmpnId' | 'cmpnAlias' | 'cmpnTel' | 'memo' | null>(null);
|
|
|
|
const cmpnIdRef = useRef<HTMLInputElement>(null);
|
|
const cmpnAliasRef = useRef<HTMLInputElement>(null);
|
|
const cmpnTelRef = useRef<HTMLInputElement>(null);
|
|
const memoRef = useRef<HTMLTextAreaElement>(null);
|
|
|
|
const resolvedOpen = open ?? isDialogOpen(dialogId);
|
|
|
|
const handleOpenChange = (nextOpen: boolean) => {
|
|
onOpenChange?.(nextOpen);
|
|
|
|
if (nextOpen) {
|
|
showDialog(dialogId);
|
|
} else {
|
|
hideDialog(dialogId);
|
|
}
|
|
}
|
|
|
|
useEffect(() => {
|
|
setInvalidField(null);
|
|
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를 입력해주세요.");
|
|
setInvalidField('cmpnId');
|
|
cmpnIdRef.current?.focus();
|
|
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 () => {
|
|
setInvalidField(null);
|
|
|
|
if (!formData.cmpnId.trim()) {
|
|
alert("회사 ID를 입력해주세요.");
|
|
setInvalidField('cmpnId');
|
|
cmpnIdRef.current?.focus();
|
|
return;
|
|
}
|
|
|
|
if (mode === 'create' && !isCheckedDuplicate) {
|
|
alert("회사 ID 중복체크를 해주세요.");
|
|
setInvalidField('cmpnId');
|
|
cmpnIdRef.current?.focus();
|
|
return;
|
|
}
|
|
|
|
const cmpnAliasCheck = validateValue(formData.cmpnAlias, {
|
|
required: true,
|
|
requiredMessage: "별칭을 입력해주세요.",
|
|
maxLength: 32,
|
|
pattern: COMMON_TEXT_REGEX,
|
|
patternMessage: "별칭에 허용되지 않는 문자가 포함되어 있습니다.",
|
|
});
|
|
if (!cmpnAliasCheck.valid) {
|
|
alert(cmpnAliasCheck.message);
|
|
setInvalidField('cmpnAlias');
|
|
cmpnAliasRef.current?.focus();
|
|
return;
|
|
}
|
|
|
|
const cmpnTelCheck = validateValue(formData.cmpnTel ?? '', {
|
|
maxLength: 16,
|
|
pattern: COMMON_TEXT_REGEX,
|
|
patternMessage: "전화번호에 허용되지 않는 문자가 포함되어 있습니다.",
|
|
});
|
|
if (!cmpnTelCheck.valid) {
|
|
alert(cmpnTelCheck.message);
|
|
setInvalidField('cmpnTel');
|
|
cmpnTelRef.current?.focus();
|
|
return;
|
|
}
|
|
|
|
const memoCheck = validateValue(formData.memo ?? '', { maxLength: 2048 });
|
|
if (!memoCheck.valid) {
|
|
alert(memoCheck.message);
|
|
setInvalidField('memo');
|
|
memoRef.current?.focus();
|
|
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) {
|
|
console.error(e);
|
|
} 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
|
|
ref={cmpnIdRef}
|
|
id="cmpnId"
|
|
placeholder="회사 ID를 입력하세요"
|
|
value={formData.cmpnId}
|
|
onChange={(e) => handleInputChange('cmpnId', e.target.value)}
|
|
disabled={mode === 'edit'}
|
|
aria-invalid={invalidField === 'cmpnId'}
|
|
/>
|
|
{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
|
|
ref={cmpnAliasRef}
|
|
id="cmpnAlias"
|
|
placeholder="별칭을 입력하세요"
|
|
value={formData.cmpnAlias ?? ''}
|
|
onChange={(e) => handleInputChange('cmpnAlias', e.target.value)}
|
|
aria-invalid={invalidField === 'cmpnAlias'}
|
|
/>
|
|
</div>
|
|
<div className="grid gap-2">
|
|
<Label htmlFor="cmpnTel">전화번호</Label>
|
|
<Input
|
|
ref={cmpnTelRef}
|
|
id="cmpnTel"
|
|
placeholder="전화번호를 입력하세요"
|
|
value={formatKoreanPhoneNumber(formData.cmpnTel ?? '')}
|
|
onChange={(e) => handleInputChange('cmpnTel', e.target.value.replace(/\D/g, ''))}
|
|
aria-invalid={invalidField === 'cmpnTel'}
|
|
/>
|
|
</div>
|
|
<div className="grid gap-2">
|
|
<Label htmlFor="memo">메모</Label>
|
|
<textarea
|
|
ref={memoRef}
|
|
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 aria-invalid:border-destructive aria-invalid:ring-destructive/20"
|
|
rows={3}
|
|
placeholder="메모를 입력하세요"
|
|
value={formData.memo ?? ''}
|
|
onChange={(e) => handleInputChange('memo', e.target.value)}
|
|
aria-invalid={invalidField === 'memo'}
|
|
/>
|
|
</div>
|
|
</KriDialog>
|
|
)
|
|
}
|
|
|
|
export default CmpnUpsertDialog
|