260303_01_ENH: TanStack Query 적용
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
'use client'
|
||||
|
||||
import { useState, useEffect } from 'react'
|
||||
import {useState, useEffect, useMemo} from 'react'
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
@@ -24,6 +24,7 @@ import {PROJECT_NAME_REGEX} from "@/const/projectConst";
|
||||
import {FW_TEAM, USER_NAME_REGEX} from "@/const/userConst";
|
||||
import {MultiSelectOption} from "@/components/ui/multi-select";
|
||||
import {ItrntUserInfo} from "@/types/user";
|
||||
import {useIntranetProjectListQuery} from "@/query/intranet";
|
||||
|
||||
export type ProjectCreateDialogProps = {
|
||||
/** Jotai로 관리할 다이얼로그 ID (기본값: 'userUpsertDialog') */
|
||||
@@ -37,8 +38,6 @@ export type ProjectCreateDialogProps = {
|
||||
onSubmit: (data: ProjectCreateRequest) => void | Promise<void>
|
||||
}
|
||||
|
||||
|
||||
|
||||
const ProjectCreateDialog = ({
|
||||
dialogId = 'projectCreateDialog',
|
||||
open,
|
||||
@@ -59,14 +58,16 @@ const ProjectCreateDialog = ({
|
||||
const [isSubmitting, setIsSubmitting] = useState(false);
|
||||
const [formData, setFormData] = useState<ProjectCreateRequest>(defaultProjectInfo);
|
||||
|
||||
const [itrntKeyword, setItrntKeyword] = useState('');
|
||||
const debouncedKeyword = useDebouncedValue(itrntKeyword, 500);
|
||||
const [itrntPrjctArray, setItrntPrjctArray] = useState<IntranetProjectSearchResponse[]>([]);
|
||||
|
||||
// 🔹 실제로 Dialog에 넘길 open 값 (외부 open 있으면 우선)
|
||||
// 실제로 Dialog에 넘길 open 값 (외부 open 있으면 우선)
|
||||
const resolvedOpen = open ?? isDialogOpen(dialogId);
|
||||
|
||||
// 🔹 실제 onOpenChange 핸들러 (외부 콜백 + jotai 둘 다 처리)
|
||||
const [itrntKeyword, setItrntKeyword] = useState('');
|
||||
const debouncedKeyword = useDebouncedValue(itrntKeyword, 500);
|
||||
|
||||
const itrntPrjctListQuery = useIntranetProjectListQuery(debouncedKeyword, resolvedOpen);
|
||||
const itrntPrjctArray = useMemo(() => itrntPrjctListQuery.data ?? [] ,[itrntPrjctListQuery.data]);
|
||||
|
||||
// 실제 onOpenChange 핸들러 (외부 콜백 + jotai 둘 다 처리)
|
||||
const handleOpenChange = (nextOpen: boolean) => {
|
||||
// 1) 외부에서 넘겨준 onOpenChange 먼저 호출
|
||||
onOpenChange?.(nextOpen);
|
||||
@@ -143,20 +144,6 @@ const ProjectCreateDialog = ({
|
||||
}
|
||||
}
|
||||
|
||||
const searchItrntPrjct = async (keyword:string): Promise<void> => {
|
||||
const res = await nextApi.get<IntranetProjectSearchResponse[]>(
|
||||
"/intranet/project",
|
||||
{ query: keyword ? { keyword } : {} }
|
||||
);
|
||||
|
||||
setItrntPrjctArray(res);
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
const keyword = debouncedKeyword.trim();
|
||||
searchItrntPrjct(keyword).then();
|
||||
}, [debouncedKeyword]);
|
||||
|
||||
useEffect(() => {
|
||||
setFormData(defaultProjectInfo);
|
||||
}, [resolvedOpen])
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
'use client'
|
||||
|
||||
import {useEffect, useState} from 'react'
|
||||
import {useEffect, useMemo, useState} from 'react'
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
@@ -12,14 +12,12 @@ import { Button } from '@/components/ui/button'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import {Trash2, Plus, Download} from 'lucide-react'
|
||||
import {ProjectInfo, ResultCode, ResultCodeType} from "@/types/project";
|
||||
import {nextApi} from "@/lib/http-client";
|
||||
import {PROJECT_SEARCH_COUNT, RESULT_CODE_TYPE_OPTIONS} from "@/const/projectConst";
|
||||
import {useDebouncedValue} from "@/hooks/useDebouncedValue";
|
||||
import {SelectedProject} from "@/store/jotai/project";
|
||||
import {KriSingleSelect} from "@/components/common/KriAutoComplete";
|
||||
import {useSelectedProject} from "@/hooks/useSelectedProject";
|
||||
import {useMounted} from "@/hooks/useMounted";
|
||||
import _ from "lodash";
|
||||
import {useProjectTopQuery, useResultCodesQuery} from "@/query/project";
|
||||
|
||||
type ResultCodeDialogProps = {
|
||||
open?: boolean
|
||||
@@ -38,37 +36,39 @@ const ResultCodeDialog = ({
|
||||
sourceArray,
|
||||
onSave,
|
||||
}: ResultCodeDialogProps) => {
|
||||
const mounted = useMounted();
|
||||
const { selectedProject } = useSelectedProject();
|
||||
const [importMode, setImportMode] = useState(false)
|
||||
const [resultCodeType, setResultCodeType] = useState<ResultCodeType>(ResultCodeType.CONTACT)
|
||||
const [resultCodeArray, setResultCodeArray] = useState<ResultCode[]>([])
|
||||
const [importCodeArray, setImportCodeArray] = useState<ResultCode[]>([])
|
||||
const [importMode, setImportMode] = useState(false);
|
||||
const [resultCodeType, setResultCodeType] = useState<ResultCodeType>(ResultCodeType.CONTACT);
|
||||
const [resultCodeArray, setResultCodeArray] = useState<ResultCode[]>([]);
|
||||
const [importProject, setImportProject] = useState<SelectedProject|undefined>(undefined);
|
||||
|
||||
const [searchKeyword, setSearchKeyword] = useState('')
|
||||
const [searchKeyword, setSearchKeyword] = useState('');
|
||||
const debouncedKeyword = useDebouncedValue(searchKeyword, 500);
|
||||
const [projectArray, setProjectArray] = useState<SelectedProject[]>([]);
|
||||
const [importProject, setImportProject] = useState<SelectedProject|undefined>(undefined)
|
||||
|
||||
const searchTopProject = async (keyword: string) => {
|
||||
const res = await nextApi.get<ProjectInfo[]>(
|
||||
`/project/top/${PROJECT_SEARCH_COUNT}`,
|
||||
{ query: { keyword } }
|
||||
);
|
||||
const topQuery = useProjectTopQuery(PROJECT_SEARCH_COUNT, debouncedKeyword, !!open && importMode);
|
||||
|
||||
setProjectArray(res.filter((prjct) => prjct.prjctCd !== selectedProject?.prjctCd));
|
||||
};
|
||||
const projectArray = useMemo(() => {
|
||||
const arr = topQuery.data ?? [];
|
||||
return arr.filter((p) => p.prjctCd !== selectedProject?.prjctCd);
|
||||
}, [topQuery.data, selectedProject?.prjctCd]);
|
||||
|
||||
const handleSelectProject = (info:SelectedProject|null) => {
|
||||
if(info === null) return;
|
||||
const importPrjctIdx = importProject?.prjctIdx ?? 0;
|
||||
const importCodesQuery = useResultCodesQuery(importPrjctIdx, !!open && importMode && importPrjctIdx > 0);
|
||||
const importCodeArray = importCodesQuery.data ?? [];
|
||||
|
||||
const handleSelectProject = (info: ProjectInfo | null) => {
|
||||
if (!info) return;
|
||||
|
||||
setImportProject(info);
|
||||
}
|
||||
};
|
||||
|
||||
const handleAddCode = () => {
|
||||
if(resultCodeArray.length === 0) return setResultCodeArray([]);
|
||||
if(!resultCodeArray || resultCodeArray.length === 0){
|
||||
setResultCodeArray([]);
|
||||
return;
|
||||
}
|
||||
|
||||
const lastCode = resultCodeArray[resultCodeArray.length - 1].resultCd;
|
||||
const lastCode = resultCodeArray.at(-1)!.resultCd;
|
||||
setResultCodeArray(prev => [
|
||||
...prev,
|
||||
{
|
||||
@@ -174,7 +174,7 @@ const ResultCodeDialog = ({
|
||||
|
||||
const handleImportApply = () => {
|
||||
if (importProject) {
|
||||
setResultCodeArray(_.cloneDeep(importCodeArray.filter(rc => rc.resultCdTy === resultCodeType)));
|
||||
setResultCodeArray(structuredClone(importCodeArray.filter(rc => rc.resultCdTy === resultCodeType)));
|
||||
|
||||
if (!changeCodeType.includes(resultCodeType)) {
|
||||
onChangeCodeType([...changeCodeType, resultCodeType]);
|
||||
@@ -219,18 +219,6 @@ const ResultCodeDialog = ({
|
||||
|
||||
}, [open]);
|
||||
|
||||
useEffect(() => {
|
||||
if(importProject){
|
||||
nextApi.get<ResultCode[]>(`/result-code/${importProject.prjctIdx}`)
|
||||
.then((res) => setImportCodeArray(res));
|
||||
}
|
||||
}, [importProject]);
|
||||
|
||||
useEffect(() => {
|
||||
const keyword = debouncedKeyword.trim();
|
||||
searchTopProject(keyword);
|
||||
}, [debouncedKeyword]);
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={handleClose}>
|
||||
{/* importMode일 때 더 넓게(그리고 애니메이션으로 부드럽게) */}
|
||||
@@ -264,8 +252,7 @@ const ResultCodeDialog = ({
|
||||
) : (
|
||||
<div className="flex items-center gap-2 w-130">
|
||||
<div className="relative flex-1">
|
||||
<KriSingleSelect
|
||||
<SelectedProject>
|
||||
<KriSingleSelect<ProjectInfo>
|
||||
items={projectArray}
|
||||
value={String(importProject?.prjctIdx || "")}
|
||||
onChange={() => {}}
|
||||
|
||||
@@ -8,10 +8,8 @@ import {
|
||||
DialogTitle,
|
||||
} from '@/components/ui/dialog'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Label } from '@/components/ui/label'
|
||||
import { useState, useRef } from 'react'
|
||||
import {Download, Upload, X} from 'lucide-react'
|
||||
import { useLoading } from '@/hooks/useLoading'
|
||||
import { useDialog } from '@/hooks/useDialog'
|
||||
import type { DialogId } from '@/store/jotai/dialog'
|
||||
import {apiError} from "@/lib/http-util";
|
||||
@@ -35,14 +33,14 @@ const UserUploadDialog = ({
|
||||
onUpload,
|
||||
onDownloadTemplate,
|
||||
}: UserUploadDialogProps) => {
|
||||
const { withLoadingAsync } = useLoading()
|
||||
const { isDialogOpen, showDialog, hideDialog } = useDialog()
|
||||
const { isDialogOpen, showDialog, hideDialog } = useDialog();
|
||||
|
||||
const [selectedFile, setSelectedFile] = useState<File | null>(null)
|
||||
const fileInputRef = useRef<HTMLInputElement>(null)
|
||||
const [submitting, setSubmitting] = useState(false);
|
||||
const [selectedFile, setSelectedFile] = useState<File | null>(null);
|
||||
const fileInputRef = useRef<HTMLInputElement>(null);
|
||||
|
||||
//실제 Dialog에 넘길 open값 (props 우선, 없으면 jotai 사용)
|
||||
const resolvedOpen = open ?? isDialogOpen(dialogId)
|
||||
const resolvedOpen = open ?? isDialogOpen(dialogId);
|
||||
|
||||
// open change 공통 처리 (부모 콜백 + jotai 동시 반영)
|
||||
const handleOpenChange = (nextOpen: boolean) => {
|
||||
@@ -92,23 +90,23 @@ const UserUploadDialog = ({
|
||||
|
||||
const handleSubmit = async () => {
|
||||
if (!selectedFile) {
|
||||
alert('파일을 선택해주세요.')
|
||||
return
|
||||
alert("파일을 선택해주세요.");
|
||||
return;
|
||||
}
|
||||
|
||||
withLoadingAsync(async () => {
|
||||
try {
|
||||
await onUpload(selectedFile)
|
||||
// 성공 시 초기화 + 닫기
|
||||
handleRemoveFile()
|
||||
handleOpenChange(false)
|
||||
} catch (e) {
|
||||
const err = apiError(e);
|
||||
console.error(err.status, err.code, err.message);
|
||||
alert('업로드에 실패했습니다.')
|
||||
}
|
||||
})
|
||||
}
|
||||
setSubmitting(true);
|
||||
try {
|
||||
await onUpload(selectedFile);
|
||||
handleRemoveFile();
|
||||
handleOpenChange(false);
|
||||
} catch (e) {
|
||||
const err = apiError(e);
|
||||
console.error(err.status, err.code, err.message);
|
||||
alert("업로드에 실패했습니다.");
|
||||
} finally {
|
||||
setSubmitting(false);
|
||||
}
|
||||
};
|
||||
|
||||
// ✅ Dialog에서 닫힐 때: 파일도 초기화 + 상태 닫기
|
||||
const handleDialogOpenChange = (nextOpen: boolean) => {
|
||||
@@ -171,11 +169,7 @@ const UserUploadDialog = ({
|
||||
|
||||
<DialogFooter>
|
||||
<div className="flex items-center justify-end w-full gap-2">
|
||||
<Button
|
||||
type="button"
|
||||
onClick={handleSubmit}
|
||||
disabled={!selectedFile}
|
||||
>
|
||||
<Button type="button" onClick={handleSubmit} disabled={!selectedFile || submitting}>
|
||||
확인
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
@@ -20,7 +20,7 @@ import {KriSelect, KriSelectItem} from "@/components/common/KriSelect";
|
||||
import {DataVerification} from "@/types/common";
|
||||
import {apiError} from "@/lib/http-util";
|
||||
import {KriSingleSelect} from "@/components/common/KriAutoComplete";
|
||||
import {USER_NAME_REGEX} from "@/const/userConst";
|
||||
import {FW_TEAM, USER_NAME_REGEX} from "@/const/userConst";
|
||||
import {NONE_HTTP_ERROR, UNKNOWN_HTTP_ERROR} from "@/const/commonConst";
|
||||
|
||||
export type UserUpsertDialogProps = {
|
||||
@@ -159,6 +159,8 @@ const UserUpsertDialog = ({
|
||||
|
||||
setIsSubmitting(true);
|
||||
|
||||
formData.userTeam = FW_TEAM;
|
||||
|
||||
try {
|
||||
await onSubmit(formData as UserInfoSaveRequest);
|
||||
handleOpenChange(false);
|
||||
|
||||
Reference in New Issue
Block a user