260303_01_ENH: TanStack Query 적용
This commit is contained in:
230
src/query/project.ts
Normal file
230
src/query/project.ts
Normal file
@@ -0,0 +1,230 @@
|
||||
"use client";
|
||||
|
||||
import {useQuery, keepPreviousData, useQueryClient, useMutation} from "@tanstack/react-query";
|
||||
import { nextApi } from "@/lib/http-client";
|
||||
import { qk } from "@/query/keys";
|
||||
import type {
|
||||
KonKiwiProjectInfo,
|
||||
ProjectCreateRequest,
|
||||
ProjectInfo,
|
||||
ProjectListQuery,
|
||||
ProjectListRequest,
|
||||
ProjectOwnerResponse, ProjectUpdateRequest, ResultCode
|
||||
} from "@/types/project";
|
||||
import {PageRequest, PageResponse} from "@/types/common";
|
||||
|
||||
export function useProjectOwnerListQuery(enabled: boolean = true) {
|
||||
return useQuery({
|
||||
queryKey: qk.projectOwnerList(),
|
||||
queryFn: () => nextApi.get<ProjectOwnerResponse>("/project/owner-list"),
|
||||
enabled,
|
||||
staleTime: 10 * 60_000,
|
||||
});
|
||||
}
|
||||
|
||||
export type ProjectListArgs = {
|
||||
query?: ProjectListQuery;
|
||||
pageIndex: number;
|
||||
pageSize: number;
|
||||
sortBy?: string;
|
||||
sortDir?: "asc" | "desc";
|
||||
enabled?: boolean;
|
||||
};
|
||||
|
||||
export function useProjectListQuery(args: ProjectListArgs, ignoreGlobalLoading: boolean = false) {
|
||||
const {
|
||||
query,
|
||||
pageIndex,
|
||||
pageSize,
|
||||
sortBy = "prjctIdx",
|
||||
sortDir = "desc",
|
||||
enabled = true,
|
||||
} = args;
|
||||
|
||||
const pageReq: PageRequest = { pageIndex, pageSize, sortBy, sortDir };
|
||||
|
||||
const body: ProjectListRequest = {
|
||||
prjctText: query?.prjctText || "",
|
||||
prjctStatusList: query?.prjctStatusList || [],
|
||||
ownerDeptList: query?.ownerDeptList || [],
|
||||
ownerFwList: query?.ownerFwList || [],
|
||||
pageRequest: pageReq,
|
||||
};
|
||||
|
||||
return useQuery({
|
||||
queryKey: qk.projectList(body, pageIndex, pageSize, sortBy, sortDir),
|
||||
queryFn: () => nextApi.post<PageResponse<ProjectInfo>>("/project/list", body),
|
||||
enabled,
|
||||
staleTime: 0, // 항상 오래된 데이터 취급
|
||||
refetchOnMount: "always", // 컴포넌트 다시 붙을 때마다
|
||||
placeholderData: keepPreviousData, // 페이지/정렬 바뀔 때 빈 배열로 깜빡임 방지
|
||||
meta: { ignoreGlobalLoading: ignoreGlobalLoading },
|
||||
});
|
||||
}
|
||||
|
||||
export function useProjectTopQuery(count: number, keyword: string, enabled: boolean) {
|
||||
const kw = keyword.trim();
|
||||
|
||||
return useQuery({
|
||||
queryKey: qk.projectTop(count, kw),
|
||||
queryFn: () =>
|
||||
nextApi.get<ProjectInfo[]>(
|
||||
`/project/top/${count}`,
|
||||
{ query: { keyword: kw } }
|
||||
),
|
||||
enabled,
|
||||
staleTime: 0, // 항상 오래된 데이터 취급
|
||||
refetchOnMount: "always", // 컴포넌트 다시 붙을 때마다
|
||||
placeholderData: keepPreviousData,
|
||||
meta: { ignoreGlobalLoading: true },
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 프로젝트 상세
|
||||
* - "자동 호출"은 아니고, 화면 들어올 때 최신 1회 확보 용도로 staleTime 0 + refetchOnMount always
|
||||
*/
|
||||
export function useProjectInfoQuery(prjctIdx: number, enabled: boolean) {
|
||||
return useQuery({
|
||||
queryKey: qk.projectInfo(prjctIdx),
|
||||
queryFn: () => nextApi.get<ProjectInfo>(`/project/info/${prjctIdx}`),
|
||||
enabled,
|
||||
staleTime: 0,
|
||||
refetchOnMount: "always",
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 결과 코드 목록
|
||||
*/
|
||||
export function useResultCodesQuery(prjctIdx: number, enabled: boolean) {
|
||||
return useQuery({
|
||||
queryKey: qk.resultCodes(prjctIdx),
|
||||
queryFn: () => nextApi.get<ResultCode[]>(`/result-code/${prjctIdx}`),
|
||||
enabled,
|
||||
staleTime: 0,
|
||||
refetchOnMount: "always",
|
||||
});
|
||||
}
|
||||
|
||||
export function useKartProjectListQuery(args: {
|
||||
itrntPrjctCd: string;
|
||||
userTeam: string;
|
||||
isKiwi: number; // KON=0, KIWI=1
|
||||
keyword: string;
|
||||
enabled: boolean;
|
||||
}) {
|
||||
const kw = args.keyword.trim();
|
||||
|
||||
return useQuery({
|
||||
queryKey: qk.kartProjectList(args.itrntPrjctCd, args.userTeam, args.isKiwi, kw),
|
||||
queryFn: () =>
|
||||
nextApi.get<KonKiwiProjectInfo[]>(
|
||||
`/kart/project-list/${args.itrntPrjctCd}/${args.userTeam}/${args.isKiwi}`,
|
||||
{ query: kw ? { keyword: kw } : {} }
|
||||
),
|
||||
enabled: args.enabled,
|
||||
staleTime: 0,
|
||||
placeholderData: keepPreviousData, // 검색 중 빈 배열로 깜빡임 방지
|
||||
meta: { ignoreGlobalLoading: true }, // 원하면 전역 오버레이 제외(네가 이미 메타 방식 쓰고 있었지)
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 프로젝트에 배정된 외주 userId 목록
|
||||
*/
|
||||
export function useAssignedOutsourceIdsQuery(prjctIdx: number, enabled: boolean) {
|
||||
return useQuery({
|
||||
queryKey: qk.assignOutsource(prjctIdx),
|
||||
queryFn: () => nextApi.get<string[]>(`/assign/prjct-outsource/${prjctIdx}`),
|
||||
enabled,
|
||||
staleTime: 0,
|
||||
refetchOnMount: "always",
|
||||
});
|
||||
}
|
||||
|
||||
export function useAssignedFieldworkIdsQuery(prjctIdx: number, enabled: boolean) {
|
||||
return useQuery({
|
||||
queryKey: qk.assignFieldwork(prjctIdx),
|
||||
queryFn: () => nextApi.get<string[]>(`/assign/prjct-fieldwork/${prjctIdx}`),
|
||||
enabled,
|
||||
staleTime: 0,
|
||||
refetchOnMount: "always",
|
||||
});
|
||||
}
|
||||
|
||||
export function useCreateProjectMutation() {
|
||||
const qc = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (data: ProjectCreateRequest) => nextApi.put<ProjectInfo>("/project", data),
|
||||
onSuccess: () => {
|
||||
// 목록 쿼리 전부 무효화(필터/페이지/정렬이 queryKey에 들어가서 prefix로)
|
||||
qc.invalidateQueries({ queryKey: ["project", "list"] });
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 프로젝트 수정
|
||||
*/
|
||||
export function useUpdateProjectMutation(prjctIdx: number) {
|
||||
const qc = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (payload: ProjectUpdateRequest) =>
|
||||
nextApi.patch<void>(`/project/${prjctIdx}`, payload),
|
||||
onSuccess: () => {
|
||||
qc.invalidateQueries({ queryKey: qk.projectInfo(prjctIdx) });
|
||||
// 목록 화면도 최신화 필요하면 prefix invalidate 추가 가능
|
||||
// qc.invalidateQueries({ queryKey: ["project", "list"] });
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useSaveAssignedFieldworkMutation(prjctIdx: number) {
|
||||
const qc = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (userIds: string[]) =>
|
||||
nextApi.put<void>(`/assign/prjct-fieldwork/${prjctIdx}`, userIds),
|
||||
onSuccess: () => {
|
||||
qc.invalidateQueries({ queryKey: qk.assignFieldwork(prjctIdx) });
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 프로젝트 삭제
|
||||
*/
|
||||
export function useDeleteProjectMutation(prjctIdx: number) {
|
||||
const qc = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: () => nextApi.del<void>(`/project/${prjctIdx}`),
|
||||
onSuccess: () => {
|
||||
qc.invalidateQueries({ queryKey: ["project", "list"] });
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 시나리오 이미지 업로드 (엔드포인트 spelling은 너 서버 기준으로)
|
||||
*/
|
||||
export function useUploadScenarioImageMutation(prjctIdx: number, prjctCd: string) {
|
||||
const qc = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (file: File): Promise<string> => {
|
||||
const formData = new FormData();
|
||||
formData.append("file", file);
|
||||
|
||||
// 서버가 업로드된 이미지 URL을 문자열로 반환한다는 전제
|
||||
return await nextApi.postForm<string>(`/project/senario-image/${prjctCd}`, formData);
|
||||
},
|
||||
onSuccess: () => {
|
||||
// 업로드 결과가 projectInfo에 반영된다면 해당 프로젝트만 갱신
|
||||
qc.invalidateQueries({ queryKey: qk.projectInfo(prjctIdx) });
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user