251201_01_ENH : datatable, autocomplete, user관리 완성
This commit is contained in:
299
src/components/dialog/UserUpsertDialog.tsx
Normal file
299
src/components/dialog/UserUpsertDialog.tsx
Normal file
@@ -0,0 +1,299 @@
|
||||
'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 commonConst from "@/config/commonConst";
|
||||
import {apiError} from "@/lib/http-util";
|
||||
import {KriSingleSelect} from "@/components/common/KriAutoComplete";
|
||||
|
||||
export type UserUpsertDialogProps = {
|
||||
/** Jotai로 관리할 다이얼로그 ID (기본값: 'userUpsertDialog') */
|
||||
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 값 (외부 open 있으면 우선)
|
||||
const resolvedOpen = open ?? isDialogOpen(dialogId);
|
||||
|
||||
// 🔹 실제 onOpenChange 핸들러 (외부 콜백 + jotai 둘 다 처리)
|
||||
const handleOpenChange = (nextOpen: boolean) => {
|
||||
// 1) 외부에서 넘겨준 onOpenChange 먼저 호출
|
||||
onOpenChange?.(nextOpen);
|
||||
|
||||
// 2) jotai 전역 상태도 함께 업데이트
|
||||
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(!commonConst.nameRegex.test(formData.userName)){
|
||||
alert('올바르지 않은 이름입니다.');
|
||||
return;
|
||||
}
|
||||
|
||||
setIsSubmitting(true)
|
||||
try {
|
||||
await onSubmit(formData as UserInfoSaveRequest);
|
||||
handleOpenChange(false);
|
||||
} catch (e) {
|
||||
const err = apiError(e);
|
||||
console.error(err.status, err.code, err.message);
|
||||
if(err.code !== commonConst.noneHttpError && err.code !== commonConst.unknownHttpError){
|
||||
alert(err.message);
|
||||
}
|
||||
else{
|
||||
alert('저장에 실패했습니다.');
|
||||
}
|
||||
} finally {
|
||||
setIsSubmitting(false);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Dialog open={resolvedOpen} onOpenChange={handleOpenChange}>
|
||||
<DialogContent className="sm:max-w-[500px] content-text-sm">
|
||||
<DialogHeader>
|
||||
<DialogTitle className="content-text-sxl">
|
||||
{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="아이디를 입력하세요"
|
||||
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="비밀번호를 입력하세요"
|
||||
/>
|
||||
</div>
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="userName">
|
||||
이름<span className="text-red-500 ml-1">*</span>
|
||||
</Label>
|
||||
<Input
|
||||
id="userName"
|
||||
placeholder="이름을 입력하세요"
|
||||
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) => 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="소속 회사를 입력하세요"
|
||||
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="메모를 입력하세요"
|
||||
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
|
||||
Reference in New Issue
Block a user