249 lines
8.3 KiB
TypeScript
249 lines
8.3 KiB
TypeScript
'use client'
|
|
import {useMemo, useState} from "react";
|
|
import AppPageTitle from "@/components/app-shell/AppPageTitle";
|
|
import {Button} from "@/components/ui/button";
|
|
import {DataTable} from "@/components/data-table/DataTable";
|
|
import {getProjectColumns} from "@/components/table-column/ProjectTableColumn";
|
|
import {
|
|
ProjectCreateRequest,
|
|
ProjectInfo,
|
|
ProjectListQuery,
|
|
ProjectStatus
|
|
} from "@/types/project";
|
|
import {Input} from "@/components/ui/input";
|
|
import {KriMultiSelect} from "@/components/common/KriAutoComplete";
|
|
import type { MultiSelectOption } from "@/components/common/KriAutoComplete";
|
|
import {PaginationState, SortingState} from "@tanstack/react-table";
|
|
import ProjectCreateDialog from "@/components/dialog/ProjectCreateDialog";
|
|
import {useDialog} from "@/hooks/useDialog";
|
|
import {useAuth} from "@/hooks/useAuth";
|
|
import {useSelectedProject} from "@/hooks/useSelectedProject";
|
|
import {SelectedProject} from "@/store/jotai/project";
|
|
import { useMounted } from "@kri/common/hooks";
|
|
import { LoadingOverlayAdapter } from "@/components/app-shell/LoadingOverlayAdapter";
|
|
import {useItrntUserList} from "@/hooks/useItrntUserList";
|
|
import {useRouter} from "next/navigation";
|
|
import {useCreateProjectMutation, useProjectListQuery, useProjectOwnerListQuery} from "@/query/project";
|
|
import {useGroupCodeListQuery} from "@/query/intranet";
|
|
import {PROJECT_STATUS_LABEL} from "@/const/projectConst";
|
|
import {isManager} from "@/utils/userRole";
|
|
|
|
const MainPage = () => {
|
|
const router = useRouter();
|
|
|
|
const mounted = useMounted();
|
|
const { users, isLoading } = useItrntUserList();
|
|
const { showDialog } = useDialog();
|
|
const { auth } = useAuth();
|
|
const { setSelectedProject } = useSelectedProject();
|
|
|
|
const [pagination, setPagination] = useState<PaginationState>({ pageIndex: 0, pageSize: 10 });
|
|
const [sorting, setSorting] = useState<SortingState>([]);
|
|
const [query, setQuery] = useState<ProjectListQuery>();
|
|
|
|
|
|
const statusArray: MultiSelectOption[] = useMemo(() => {
|
|
return (Object.values(ProjectStatus).filter((v) => typeof v === "number") as number[])
|
|
.sort((a, b) => a - b)
|
|
.map((v) => ({
|
|
value: String(v),
|
|
label: PROJECT_STATUS_LABEL[v as ProjectStatus],
|
|
}));
|
|
}, []);
|
|
|
|
const enabled = mounted && !isLoading; // intranet user list 준비된 뒤에만 호출
|
|
const ownerQuery = useProjectOwnerListQuery(enabled);
|
|
const groupCodeListQuery = useGroupCodeListQuery(enabled);
|
|
|
|
const groupCodeLabelMap = useMemo(() => {
|
|
return new Map((groupCodeListQuery.data ?? []).map((g) => [g.groupCode, g.groupCodeLabel]));
|
|
}, [groupCodeListQuery.data]);
|
|
|
|
const deptArray = useMemo(() => {
|
|
const ownerDeptList = ownerQuery.data?.ownerDeptList ?? [];
|
|
|
|
return ownerDeptList.map((code) => ({
|
|
value: code,
|
|
label: groupCodeLabelMap.get(code) ?? code,
|
|
}));
|
|
}, [ownerQuery.data?.ownerDeptList, groupCodeLabelMap]);
|
|
|
|
const fwArray = useMemo(() => {
|
|
const ownerFwIDList = ownerQuery.data?.ownerFwList ?? [];
|
|
|
|
return ownerFwIDList.map((fwID) => ({
|
|
value: fwID,
|
|
label: users.find(u => u.userId === fwID)?.userNm ?? "",
|
|
}));
|
|
|
|
}, [ownerQuery.data?.ownerFwList]);
|
|
|
|
const sort = sorting?.[0] ?? { id: "prjctIdx", desc: true as const };
|
|
|
|
const listQuery = useProjectListQuery({
|
|
query,
|
|
pageIndex: pagination.pageIndex,
|
|
pageSize: pagination.pageSize,
|
|
sortBy: sort.id,
|
|
sortDir: sort.desc ? "desc" : "asc",
|
|
enabled,
|
|
});
|
|
|
|
const viewList = listQuery.data?.content ?? [];
|
|
const totalCount = listQuery.data?.totalElements ?? 0;
|
|
const pageCount = listQuery.data?.totalPages ?? 0;
|
|
|
|
const createMutation = useCreateProjectMutation?.();
|
|
|
|
const [searchText, setSearchText] = useState('');
|
|
const [searchStatus, setSearchStatus] = useState<string[]>([]);
|
|
const [searchDept, setSearchDept] = useState<string[]>([]);
|
|
const [searchFw, setSearchFw] = useState<string[]>([]);
|
|
|
|
const columns = getProjectColumns(users, groupCodeLabelMap);
|
|
const userId = auth.user?.userId ?? "";
|
|
const userRole = auth.user?.userRole;
|
|
|
|
const handleSearchClick = () => {
|
|
const searchStatusCode:ProjectStatus[] = searchStatus
|
|
.map((value) => Number(value))
|
|
.filter((v): v is ProjectStatus => v !== undefined);
|
|
|
|
setQuery({
|
|
prjctText: searchText,
|
|
prjctStatusList: searchStatusCode,
|
|
ownerDeptList: searchDept,
|
|
ownerFwList: searchFw,
|
|
});
|
|
|
|
setPagination(prev => ({ ...prev, pageIndex: 0 }))
|
|
}
|
|
|
|
const handleCreateClick = () => {
|
|
showDialog('projectCreateDialog');
|
|
}
|
|
|
|
const handleSubmit = async (data: ProjectCreateRequest): Promise<void> => {
|
|
const projectInfo = await createMutation.mutateAsync(data);
|
|
|
|
setSelectedProject({
|
|
prjctIdx: projectInfo.prjctIdx,
|
|
prjctCd: projectInfo.prjctCd,
|
|
prjctName: projectInfo.prjctName,
|
|
itrntPrjctCd: projectInfo.itrntPrjctCd,
|
|
ownerDept: projectInfo.ownerDept,
|
|
ownerFw: projectInfo.ownerFw,
|
|
ff: projectInfo.ff
|
|
} as SelectedProject);
|
|
|
|
router.push("/project/info");
|
|
};
|
|
|
|
const handleClick = (projectInfo:ProjectInfo) => {
|
|
setSelectedProject({
|
|
prjctIdx: projectInfo.prjctIdx,
|
|
prjctCd: projectInfo.prjctCd,
|
|
prjctName: projectInfo.prjctName,
|
|
itrntPrjctCd: projectInfo.itrntPrjctCd,
|
|
ownerDept: projectInfo.ownerDept,
|
|
ownerFw: projectInfo.ownerFw,
|
|
ff: projectInfo.ff
|
|
} as SelectedProject);
|
|
|
|
router.push(`/list/manage`);
|
|
}
|
|
|
|
if (!mounted || isLoading) return <LoadingOverlayAdapter force />;
|
|
|
|
return (
|
|
<div className="space-y-6 text-sm">
|
|
<AppPageTitle titleText={"프로젝트"}/>
|
|
|
|
<div className="flex gap-2 items-center">
|
|
<div className="flex flex-1 items-center gap-2">
|
|
<Input
|
|
placeholder="프로젝트 코드 Or 프로젝트 명"
|
|
className="w-120 h-9 bg-white"
|
|
value={searchText}
|
|
onChange={(e) => setSearchText(e.target.value)}
|
|
onKeyDown={(e) => {
|
|
if (e.key === "Enter") {
|
|
handleSearchClick();
|
|
}
|
|
}}
|
|
/>
|
|
{
|
|
isManager(userRole) &&
|
|
<KriMultiSelect
|
|
items={statusArray}
|
|
value={searchStatus}
|
|
onChange={setSearchStatus}
|
|
getValue={(item) => item.value}
|
|
getLabel={(item) => item.label}
|
|
placeholder="진행 상태"
|
|
widthClassName="w-60"
|
|
heightClassName="h-9"
|
|
/>
|
|
}
|
|
<KriMultiSelect
|
|
items={deptArray}
|
|
value={searchDept}
|
|
onChange={setSearchDept}
|
|
getValue={(item) => item.value}
|
|
getLabel={(item) => item.label}
|
|
placeholder="담당 그룹"
|
|
widthClassName="w-60"
|
|
heightClassName="h-9"
|
|
/>
|
|
<KriMultiSelect
|
|
items={fwArray}
|
|
value={searchFw}
|
|
onChange={setSearchFw}
|
|
getValue={(item) => item.value}
|
|
getLabel={(item) => item.label}
|
|
placeholder="담당 실사"
|
|
widthClassName="w-60"
|
|
heightClassName="h-9"
|
|
/>
|
|
<Button
|
|
onClick={handleSearchClick}
|
|
>
|
|
검색
|
|
</Button>
|
|
</div>
|
|
<Button
|
|
onClick={handleCreateClick}
|
|
>
|
|
프로젝트 등록
|
|
</Button>
|
|
</div>
|
|
|
|
<div className="bg-white rounded-lg border border-gray-200 overflow-hidden">
|
|
<DataTable
|
|
columns={columns}
|
|
data={viewList}
|
|
isClient={false}
|
|
pageCount={pageCount}
|
|
totalCount={totalCount}
|
|
pagination={pagination}
|
|
onPaginationChange={setPagination}
|
|
onSortingChange={setSorting}
|
|
selectMode='click-event'
|
|
onRowClick={handleClick}
|
|
pageSizeOptions={[10, 20, 30, 50]}
|
|
getRowId={(row) => row.prjctCd} // row.id = userId
|
|
maxBodyHeight="71vh"
|
|
/>
|
|
</div>
|
|
|
|
<ProjectCreateDialog
|
|
users={users}
|
|
ownerFw={userId}
|
|
onSubmit={handleSubmit}
|
|
/>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export default MainPage;
|