310 lines
10 KiB
TypeScript
310 lines
10 KiB
TypeScript
'use client'
|
|
|
|
import { useState, useEffect } from 'react'
|
|
import {
|
|
Dialog,
|
|
DialogContent,
|
|
DialogDescription,
|
|
DialogFooter,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
} from '@/components/ui/dialog'
|
|
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 { UserInfoSaveRequest } from '@/types/user'
|
|
import KriPassword from "@/components/common/KriPassword";
|
|
import {KriSelect, KriSelectItem} from "@/components/common/KriSelect";
|
|
import {DataVerification} from "@/types/common";
|
|
import {apiError} from "@kri/common/http";
|
|
import {KriSingleSelect} from "@/components/common/KriAutoComplete";
|
|
import {FW_TEAM, USER_NAME_REGEX} from "@/const/userConst";
|
|
import {NONE_HTTP_ERROR, UNKNOWN_HTTP_ERROR} from "@kri/common/http";
|
|
|
|
export type UserUpsertDialogProps = {
|
|
/** Jotai로 관리할 다이얼로그 ID */
|
|
dialogId?: DialogId
|
|
|
|
/** 필요한 경우 부모에서 직접 open 제어 */
|
|
open?: boolean
|
|
onOpenChange?: (open: boolean) => void
|
|
|
|
mode?: 'create' | 'edit'
|
|
initialData?: Partial<UserInfoSaveRequest>
|
|
isInternal: boolean
|
|
userCmpn: string
|
|
userCmpnArray: string[]
|
|
userRoleArray: KriSelectItem[]
|
|
onSubmit: (data: UserInfoSaveRequest) => void | Promise<void>
|
|
onCheckDuplicate?: (userId: string) => DataVerification | Promise<DataVerification>
|
|
}
|
|
|
|
const UserUpsertDialog = ({
|
|
dialogId = 'userUpsertDialog',
|
|
open,
|
|
onOpenChange,
|
|
mode = 'create',
|
|
initialData,
|
|
isInternal,
|
|
userCmpn,
|
|
userCmpnArray,
|
|
userRoleArray,
|
|
onSubmit,
|
|
onCheckDuplicate,
|
|
}: UserUpsertDialogProps) => {
|
|
const defaultUserInfo:UserInfoSaveRequest = {
|
|
userId: '',
|
|
userPw: '',
|
|
userName: '',
|
|
userCmpn: userCmpn,
|
|
userTeam: '',
|
|
userRole: 9002,
|
|
memo: '',
|
|
}
|
|
|
|
const { isDialogOpen, showDialog, hideDialog } = useDialog();
|
|
|
|
const [formData, setFormData] = useState<UserInfoSaveRequest>(defaultUserInfo);
|
|
|
|
const [isCheckedDuplicate, setIsCheckedDuplicate] = useState(false);
|
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
|
|
// 실제 Dialog에 전달할 open 값
|
|
const resolvedOpen = open ?? isDialogOpen(dialogId);
|
|
|
|
// 부모 콜백과 jotai 상태를 함께 처리
|
|
const handleOpenChange = (nextOpen: boolean) => {
|
|
onOpenChange?.(nextOpen);
|
|
|
|
if (nextOpen) {
|
|
showDialog(dialogId);
|
|
} else {
|
|
hideDialog(dialogId);
|
|
}
|
|
}
|
|
|
|
// initialData / mode / open 변경 시 폼 세팅
|
|
useEffect(() => {
|
|
if (initialData) {
|
|
setFormData(prev => ({
|
|
...prev,
|
|
...initialData,
|
|
}));
|
|
|
|
if (mode === 'edit'){
|
|
setIsCheckedDuplicate(true);
|
|
}
|
|
} else {
|
|
setFormData(defaultUserInfo);
|
|
setIsCheckedDuplicate(false);
|
|
}
|
|
}, [initialData, mode, resolvedOpen])
|
|
|
|
const handleInputChange = (field: keyof UserInfoSaveRequest, value: any) => {
|
|
setFormData(prev => ({
|
|
...prev,
|
|
[field]: value,
|
|
}));
|
|
|
|
if (field === 'userId' && mode === 'create') {
|
|
setIsCheckedDuplicate(false);
|
|
}
|
|
}
|
|
|
|
const handleCheckDuplicate = async () => {
|
|
if (!formData.userId.trim()) {
|
|
alert("아이디를 입력해주세요.");
|
|
return
|
|
}
|
|
|
|
if (onCheckDuplicate) {
|
|
const isDuplicate = await onCheckDuplicate(formData.userId);
|
|
if (!isDuplicate.success) {
|
|
alert(isDuplicate.errMessage);
|
|
setIsCheckedDuplicate(false);
|
|
} else {
|
|
alert("사용 가능한 아이디입니다.");
|
|
setIsCheckedDuplicate(true);
|
|
}
|
|
} else {
|
|
setIsCheckedDuplicate(true);
|
|
}
|
|
}
|
|
|
|
const handleSubmit = async () => {
|
|
if (!formData.userId.trim()) {
|
|
alert("아이디를 입력해주세요.");
|
|
return;
|
|
}
|
|
if (mode === 'create' && !isCheckedDuplicate) {
|
|
alert("아이디 중복체크를 해주세요.");
|
|
return;
|
|
}
|
|
if (!formData.userPw.trim()) {
|
|
alert("비밀번호를 입력해주세요.");
|
|
return;
|
|
}
|
|
if (!formData.userName.trim()) {
|
|
alert("이름을 입력해주세요.");
|
|
return;
|
|
}
|
|
if(!USER_NAME_REGEX.test(formData.userName)){
|
|
alert("올바르지 않은 이름입니다.");
|
|
return;
|
|
}
|
|
|
|
setIsSubmitting(true);
|
|
|
|
formData.userTeam = FW_TEAM;
|
|
|
|
try {
|
|
await onSubmit(formData as UserInfoSaveRequest);
|
|
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 (
|
|
<Dialog open={resolvedOpen} onOpenChange={handleOpenChange}>
|
|
<DialogContent className="sm:max-w-[500px] text-sm">
|
|
<DialogHeader>
|
|
<DialogTitle className="text-xl">
|
|
{mode === 'create' ? '계정 등록' : '계정 수정'}
|
|
</DialogTitle>
|
|
<DialogDescription>면접원 계정 정보를 입력해주세요.</DialogDescription>
|
|
</DialogHeader>
|
|
<div className="grid gap-4 py-4">
|
|
<div className="grid gap-2">
|
|
<Label htmlFor="userId">
|
|
ID<span className="text-red-500 ml-1">*</span>
|
|
</Label>
|
|
<div className="flex gap-2">
|
|
<Input
|
|
id="userId"
|
|
placeholder="Enter ID"
|
|
value={formData.userId}
|
|
onChange={(e) => handleInputChange('userId', e.target.value)}
|
|
disabled={mode === 'edit'}
|
|
/>
|
|
{mode === 'create' && (
|
|
<Button
|
|
variant="outline"
|
|
onClick={handleCheckDuplicate}
|
|
disabled={!formData.userId.trim() || isCheckedDuplicate}
|
|
>
|
|
{isCheckedDuplicate ? "확인완료" : "중복체크"}
|
|
</Button>
|
|
)}
|
|
</div>
|
|
</div>
|
|
<div className="grid gap-2">
|
|
<Label htmlFor="password">
|
|
PW<span className="text-red-500 ml-1">*</span>
|
|
</Label>
|
|
<KriPassword
|
|
id="password"
|
|
value={formData.userPw}
|
|
onChange={(v) => handleInputChange('userPw', v)}
|
|
placeholder="Enter password"
|
|
/>
|
|
</div>
|
|
<div className="grid gap-2">
|
|
<Label htmlFor="userName">
|
|
이름<span className="text-red-500 ml-1">*</span>
|
|
</Label>
|
|
<Input
|
|
id="userName"
|
|
placeholder="Enter name"
|
|
value={formData.userName}
|
|
onChange={(e) => handleInputChange('userName', e.target.value)}
|
|
/>
|
|
</div>
|
|
<div className="grid gap-2">
|
|
<Label>계정 권한</Label>
|
|
<KriSingleSelect
|
|
items={userRoleArray}
|
|
value={String(formData.userRole)}
|
|
onChange={(e) => {
|
|
if(mode !== 'create'){
|
|
alert("권한이 변경되면 해당 계정에 배정된 리스트 정보가 초기화됩니다.");
|
|
}
|
|
|
|
handleInputChange('userRole', Number(e));
|
|
}}
|
|
getLabel={(item) => item.label}
|
|
getValue={(item) => String(item.value)}
|
|
placeholder="사용자 권한"
|
|
widthClassName="w-full"
|
|
heightClassName="h-9"
|
|
/>
|
|
</div>
|
|
<div className="grid gap-2">
|
|
<Label>소속 회사</Label>
|
|
{
|
|
isInternal && formData.userRole > 9000 ?
|
|
<KriSingleSelect
|
|
items={userCmpnArray}
|
|
value={formData.userCmpn}
|
|
onChange={(e) => handleInputChange('userCmpn', e)}
|
|
placeholder="소속 회사"
|
|
widthClassName="w-full"
|
|
heightClassName="h-9"
|
|
/>
|
|
:
|
|
<Input
|
|
id="userCmpn"
|
|
placeholder="Enter company"
|
|
value={formData.userCmpn}
|
|
disabled={!isInternal}
|
|
onChange={(e) => handleInputChange('userCmpn', 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>
|
|
</div>
|
|
<DialogFooter>
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
onClick={() => handleOpenChange(false)}
|
|
>
|
|
취소
|
|
</Button>
|
|
<Button
|
|
type="submit"
|
|
onClick={handleSubmit}
|
|
disabled={isSubmitting}
|
|
>
|
|
{mode === 'create' ? "등록" : "수정"}
|
|
</Button>
|
|
</DialogFooter>
|
|
</DialogContent>
|
|
</Dialog>
|
|
)
|
|
}
|
|
|
|
export default UserUpsertDialog
|