244 lines
8.1 KiB
TypeScript
244 lines
8.1 KiB
TypeScript
'use client'
|
|
import {useEffect, useRef, useState} from "react";
|
|
import BodyTitle from "@/components/layout/BodyTitle";
|
|
import {Button} from "@/components/ui/button";
|
|
import {DataTable} from "@/components/data-table/DataTable";
|
|
import {getProjectColumns} from "./ProjectTableColumn";
|
|
import {
|
|
ProjectCreateRequest,
|
|
ProjectInfo,
|
|
ProjectListQuery,
|
|
ProjectListRequest, ProjectOwnerResponse,
|
|
} from "@/types/project";
|
|
import {Input} from "@/components/ui/input";
|
|
import {KriMultiSelect} from "@/components/common/KriAutoComplete";
|
|
import {MultiSelectOption} from "@/components/ui/multi-select";
|
|
import {PaginationState, SortingState} from "@tanstack/react-table";
|
|
import {PageResponse} from "@/types/common";
|
|
import ProjectCreateDialog from "@/components/dialog/ProjectCreateDialog";
|
|
import {useDialog} from "@/hooks/useDialog";
|
|
import {useLoading} from "@/hooks/useLoading";
|
|
import {useAuth} from "@/hooks/useAuth";
|
|
import {nextApi} from "@/lib/http-client";
|
|
import {useSelectedProject} from "@/hooks/useSelectedProject";
|
|
import {SelectedProject} from "@/store/jotai/project";
|
|
import {useMounted} from "@/hooks/useMounted";
|
|
import {LoadingOverlay} from "@/components/common/LoadingOverlay";
|
|
import {FW_USER_ROLE} from "@/const/userConst";
|
|
import {ProjectStatus} from "@/const/projectConst";
|
|
import {useItrntUserList} from "@/hooks/useItrntUserList";
|
|
|
|
const MainPage = () => {
|
|
const { users, isLoading } = useItrntUserList();
|
|
const { showDialog } = useDialog();
|
|
const { withLoadingAsync } = useLoading();
|
|
const mounted = useMounted();
|
|
const { auth } = useAuth();
|
|
const { clearSelectedProject, setSelectedProject } = useSelectedProject();
|
|
|
|
const [statusArray, setStatusArray] = useState<MultiSelectOption[]>([]);
|
|
const [deptArray, setDeptArray] = useState<string[]>([]);
|
|
const [fwArray, setFwArray] = useState<string[]>([]);
|
|
|
|
const [searchText, setSearchText] = useState('');
|
|
const [searchStatus, setSearchStatus] = useState<string[]>([]);
|
|
const [searchDept, setSearchDept] = useState<string[]>([]);
|
|
const [searchFw, setSearchFw] = useState<string[]>([]);
|
|
|
|
const [pagination, setPagination] = useState<PaginationState>({ pageIndex: 0, pageSize: 10 });
|
|
const [sorting, setSorting] = useState<SortingState>([]);
|
|
const [query, setQuery] = useState<ProjectListQuery>();
|
|
|
|
const [viewList, setViewList] = useState<ProjectInfo[]>([]);
|
|
const [pageCount, setPageCount] = useState(0);
|
|
const [totalCount, setTotalCount] = useState(0);
|
|
|
|
const columns = getProjectColumns(users);
|
|
const userId = auth.user?.userId ?? "";
|
|
const userRole = auth.user?.userRole ?? FW_USER_ROLE;
|
|
|
|
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 nextApi.put<ProjectInfo>("/project", data);
|
|
setSelectedProject({
|
|
prjctIdx: projectInfo.prjctIdx,
|
|
prjctCd: projectInfo.prjctCd,
|
|
prjctName: projectInfo.prjctName,
|
|
ownerDept: projectInfo.ownerDept,
|
|
ownerFw: projectInfo.ownerFw,
|
|
} as SelectedProject);
|
|
|
|
window.location.href = `/project/info`;
|
|
}
|
|
|
|
const handleClick = (projectInfo:ProjectInfo) => {
|
|
setSelectedProject({
|
|
prjctIdx: projectInfo.prjctIdx,
|
|
prjctCd: projectInfo.prjctCd,
|
|
prjctName: projectInfo.prjctName,
|
|
ownerDept: projectInfo.ownerDept,
|
|
ownerFw: projectInfo.ownerFw,
|
|
} as SelectedProject);
|
|
|
|
window.location.href = `/list/manage`;
|
|
}
|
|
|
|
// 중복 호출 방지(초기 mount에서 pagination/sorting 콜백이 각각 한번씩 날아오는 케이스 방어)
|
|
const lastReqKeyRef = useRef<string>("");
|
|
|
|
useEffect(() => {
|
|
const reqKey = JSON.stringify({ query, pagination, sorting});
|
|
if (lastReqKeyRef.current === reqKey) return;
|
|
lastReqKeyRef.current = reqKey;
|
|
|
|
const sort = sorting?.[0];
|
|
const searchBody: ProjectListRequest = {
|
|
prjctText: query?.prjctText || "",
|
|
prjctStatusList: query?.prjctStatusList || [],
|
|
ownerDeptList: query?.ownerDeptList || [],
|
|
ownerFwList: query?.ownerFwList || [],
|
|
pageIndex: pagination.pageIndex,
|
|
pageSize: pagination.pageSize,
|
|
sortBy: sort?.id ?? "prjctIdx",
|
|
sortDir: sort?.desc ? "desc" : "asc"
|
|
}
|
|
|
|
withLoadingAsync(() =>
|
|
nextApi.post<PageResponse<ProjectInfo>>("/project/list", searchBody)
|
|
.then((r) => {
|
|
setViewList(r.content);
|
|
setTotalCount(r.totalElements);
|
|
setPageCount(r.totalPages);
|
|
})
|
|
.catch(() => {
|
|
setViewList([]);
|
|
setTotalCount(0);
|
|
setPageCount(0);
|
|
})
|
|
);
|
|
|
|
}, [query, pagination, sorting]);
|
|
|
|
useEffect(() => {
|
|
clearSelectedProject();
|
|
|
|
const options: MultiSelectOption[] = Object.entries(ProjectStatus)
|
|
.sort(([a], [b]) => Number(a) - Number(b))
|
|
.map(([key, label]) => ({ value: key, label: String(label) }));
|
|
|
|
setStatusArray(options);
|
|
|
|
nextApi.get<ProjectOwnerResponse>("/project/owner-list")
|
|
.then((res) => {
|
|
setDeptArray(res.ownerDeptList);
|
|
setFwArray(res.ownerFwList);
|
|
});
|
|
}, []);
|
|
|
|
if (!mounted || isLoading) return <LoadingOverlay force />;
|
|
|
|
return (
|
|
<div className="space-y-6 content-text-sm">
|
|
<BodyTitle 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();
|
|
}
|
|
}}
|
|
/>
|
|
{
|
|
userRole < FW_USER_ROLE &&
|
|
<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}
|
|
placeholder="담당 그룹"
|
|
widthClassName="w-60"
|
|
heightClassName="h-9"
|
|
/>
|
|
<KriMultiSelect
|
|
items={fwArray}
|
|
value={searchFw}
|
|
onChange={setSearchFw}
|
|
placeholder="담당 실사"
|
|
widthClassName="w-60"
|
|
heightClassName="h-9"
|
|
/>
|
|
<Button
|
|
className="content-text-lg"
|
|
onClick={handleSearchClick}
|
|
>
|
|
검색
|
|
</Button>
|
|
</div>
|
|
<Button
|
|
className="content-text-lg"
|
|
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}
|
|
onPaginationChange={setPagination}
|
|
onSortingChange={setSorting}
|
|
selectMode='click-event'
|
|
onRowClick={handleClick}
|
|
pageSizeOptions={[10, 20, 30, 50]}
|
|
getRowId={(row) => row.prjctCd} // row.id = userId
|
|
/>
|
|
</div>
|
|
|
|
<ProjectCreateDialog
|
|
users={users}
|
|
ownerFw={userId}
|
|
onSubmit={handleSubmit}
|
|
/>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export default MainPage; |