276 lines
8.6 KiB
TypeScript
276 lines
8.6 KiB
TypeScript
"use client";
|
|
|
|
import {useQuery, keepPreviousData, useQueryClient, useMutation} from "@tanstack/react-query";
|
|
import { nextApi } from "@/lib/http-client";
|
|
import { qk } from "@/query/key/keys";
|
|
import type {
|
|
KonKiwiProjectInfo,
|
|
ProjectCreateRequest,
|
|
ProjectInfo,
|
|
ProjectListQuery,
|
|
ProjectListRequest,
|
|
ProjectOwnerResponse, ProjectUpdateRequest, ResultCode
|
|
} from "@/types/project";
|
|
import type { ListHeaderAssignType, ProjectCompanyAssignSummary, ProjectFieldworkAssignSummary } from "@/types/list";
|
|
import type { PageRequest, PageResponse } from "@kri/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/${encodeURIComponent(args.itrntPrjctCd)}/${args.isKiwi}`,
|
|
{ query: kw ? { keyword: kw } : {}, skipForbiddenHandler: true }
|
|
),
|
|
enabled: args.enabled,
|
|
staleTime: 0,
|
|
placeholderData: keepPreviousData, // 검색 중 빈 배열로 깜빡임 방지
|
|
meta: { ignoreGlobalLoading: true }, // 원하면 전역 오버레이 제외(네가 이미 메타 방식 쓰고 있었지)
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 프로젝트에 배정된 회사 ID 목록
|
|
*/
|
|
export function useAssignedCmpnIdsQuery(prjctIdx: number, enabled: boolean) {
|
|
return useQuery({
|
|
queryKey: qk.projectAssignCmpn(prjctIdx),
|
|
queryFn: () => nextApi.get<string[]>(`/assign/prjct-cmpn/${prjctIdx}`),
|
|
enabled,
|
|
staleTime: 0,
|
|
refetchOnMount: "always",
|
|
});
|
|
}
|
|
|
|
export function useAssignedFieldworkIdsQuery(prjctIdx: number, enabled: boolean) {
|
|
return useQuery({
|
|
queryKey: qk.projectAssignFieldwork(prjctIdx),
|
|
queryFn: () => nextApi.get<string[]>(`/assign/prjct-fieldwork/${prjctIdx}`),
|
|
enabled,
|
|
staleTime: 0,
|
|
refetchOnMount: "always",
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 프로젝트의 외주사별 배정 현황(총 개수/미배정 개수/외주사별 배정 개수)
|
|
*/
|
|
export function useAssignedCmpnSummaryQuery(prjctIdx: number, enabled: boolean) {
|
|
return useQuery({
|
|
queryKey: qk.projectAssignCmpnSummary(prjctIdx),
|
|
queryFn: () => nextApi.get<ProjectCompanyAssignSummary>(`/assign/prjct-cmpn-summary/${prjctIdx}`),
|
|
enabled,
|
|
staleTime: 0,
|
|
refetchOnMount: "always",
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 프로젝트의 컨택원/면접원별 배정 현황(총 개수/미배정 개수/사람별 배정 개수)
|
|
* cmpnIds를 지정하면 해당 외주사 소속 인원 범위로 카운트가 재계산된다(외주사 계정은 서버에서 본인 회사로 강제됨).
|
|
*/
|
|
export function useAssignedFieldworkSummaryQuery(
|
|
prjctIdx: number,
|
|
assignType: ListHeaderAssignType,
|
|
cmpnIds: string[],
|
|
enabled: boolean,
|
|
) {
|
|
return useQuery({
|
|
queryKey: qk.projectAssignFieldworkSummary(prjctIdx, assignType, cmpnIds),
|
|
queryFn: () => {
|
|
const query = `?assignType=${assignType}${cmpnIds.length > 0 ? `&cmpnIds=${cmpnIds.join(",")}` : ""}`;
|
|
return nextApi.get<ProjectFieldworkAssignSummary>(`/assign/prjct-fieldwork-summary/${prjctIdx}${query}`);
|
|
},
|
|
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.projectAssignFieldwork(prjctIdx) });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useSaveAssignedCmpnMutation(prjctIdx: number) {
|
|
const qc = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: (cmpnIds: string[]) =>
|
|
nextApi.put<void>(`/assign/prjct-cmpn/${prjctIdx}`, cmpnIds),
|
|
onSuccess: () => {
|
|
qc.invalidateQueries({ queryKey: qk.projectAssignCmpn(prjctIdx) });
|
|
qc.invalidateQueries({ queryKey: qk.projectInfo(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(prjctCd: string) {
|
|
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);
|
|
},
|
|
// 업로드는 URL만 돌려받는 동작이라 projectInfo를 실제로 바꾸지 않는다.
|
|
// 여기서 invalidate하면 편집 중(저장 전) projectInfo refetch가 발생해 미저장 폼 값을 덮어쓴다.
|
|
});
|
|
}
|