diff --git a/src/pages/JDListPage.tsx b/src/pages/JDListPage.tsx index 6ea10e7..95f597b 100644 --- a/src/pages/JDListPage.tsx +++ b/src/pages/JDListPage.tsx @@ -11,15 +11,15 @@ import nextbtn_v2 from "../assets/icons/icon_next_btn_v2.svg"; import { useNavigate } from "react-router-dom"; import PlusIcon from "../assets/icons/icon_plus_white.svg"; import { JobAnnouncement } from "../types/type"; -import { jobget } from "../services/jd"; +import { filteredjobget, jobget } from "../services/jd"; import { getCookie } from "../services/cookie"; const JDListPage: React.FC = () => { const Filterbuttons = ["전체", "작성전", "작성중", "작성완료", "마감"]; const [activeButton, setActiveButton] = useState("전체"); // "전체", "작성전", "작성중", "작성완료", "지원완료" - const [selectedSort, setSelectedSort] = useState("CREATED"); // CREATED, ENDED , 초기값은 등록순 + const [selectedSort, setSelectedSort] = useState(""); // CREATED, ENDED , 초기값은 등록순 const [currentPage, setCurrentPage] = useState(1); //현재 위치한 페이지 - const [pageTotal, setpageTotal] = useState(20); + const [pageTotal, setpageTotal] = useState(0); const [pages, setPages] = useState([]); const [jobsData, setJobsData] = useState([]); const [isLoading, setIsLoading] = useState(true); @@ -43,9 +43,6 @@ const JDListPage: React.FC = () => { top: 0, behavior: "auto", }); - let currentpage = (currentPage - 1).toString(); - getJobList(currentpage, null, selectedSort, user.token); - setIsLoading(false); }, []); //페이지네이션 @@ -93,21 +90,20 @@ const JDListPage: React.FC = () => { ) { tempPages.push(...); } - tempPages.push(renderPageNumber(pageTotal)); + if (pageTotal !== 0) { + tempPages.push(renderPageNumber(pageTotal)); + } }; generatePages(); setPages(tempPages); - }, [currentPage, pageTotal]); + console.log("curr" + currentPage); + console.log("total" + pageTotal); + }, [currentPage, pageTotal, activeButton]); - const getJobList = async ( - page: string, - writeStatus: string | null, - sortType: string, - token: string - ) => { + const getJobList = async (page: string, token: string) => { try { - const response = await jobget(page, writeStatus, sortType, token); + const response = await jobget(page, token); const mappedData = response.data.content.map((job: any) => ({ id: job.jobDescriptionId, title: job.title, @@ -117,14 +113,103 @@ const JDListPage: React.FC = () => { status: job.writeStatus, })); setJobsData(mappedData); - setpageTotal(response.data.totalPage); + if (response.data.totalPage !== 0) { + setpageTotal(response.data.totalPage); + } else { + setpageTotal(1); + } console.log(jobsData); + setIsLoading(false); } catch (error) { console.error(error); alert(JSON.stringify(error)); } }; + useEffect(() => { + let currentpage = (currentPage - 1).toString(); + if (activeButton === "작성전") { + setCurrentPage(1); + getFilteredJobList(currentpage, "NOT_APPLIED", selectedSort, user.token); + } else if (activeButton === "작성중") { + setCurrentPage(1); + getFilteredJobList(currentpage, "WRITING", selectedSort, user.token); + } else if (activeButton === "작성완료") { + setCurrentPage(1); + getFilteredJobList(currentpage, "WRITTEN", selectedSort, user.token); + } else if (activeButton === "마감") { + setCurrentPage(1); + getFilteredJobList(currentpage, "CLOSED", selectedSort, user.token); + } else if (activeButton === "전체") { + setCurrentPage(1); + getFilteredJobList(currentpage, "ALL", selectedSort, user.token); + } + }, [activeButton]); + + useEffect(() => { + let currentpage = (currentPage - 1).toString(); + if (activeButton === "작성전") { + getFilteredJobList(currentpage, "NOT_APPLIED", selectedSort, user.token); + } else if (activeButton === "작성중") { + getFilteredJobList(currentpage, "WRITING", selectedSort, user.token); + } else if (activeButton === "작성완료") { + getFilteredJobList(currentpage, "WRITTEN", selectedSort, user.token); + } else if (activeButton === "마감") { + getFilteredJobList(currentpage, "CLOSED", selectedSort, user.token); + } else if (activeButton === "전체") { + getFilteredJobList(currentpage, "ALL", selectedSort, user.token); + } + }, [selectedSort]); + + useEffect(() => { + let currentpage = (currentPage - 1).toString(); + if (activeButton === "작성전") { + getFilteredJobList(currentpage, "NOT_APPLIED", selectedSort, user.token); + } else if (activeButton === "작성중") { + getFilteredJobList(currentpage, "WRITING", selectedSort, user.token); + } else if (activeButton === "작성완료") { + getFilteredJobList(currentpage, "WRITTEN", selectedSort, user.token); + } else if (activeButton === "마감") { + getFilteredJobList(currentpage, "CLOSED", selectedSort, user.token); + } else if (activeButton === "전체") { + getJobList(currentpage, user.token); + } + }, [currentPage]); + + const getFilteredJobList = async ( + page: string, + writeStatus: string, + sortType: string, + token: string + ) => { + if (writeStatus !== "") { + try { + const response = await filteredjobget( + page, + writeStatus, + sortType, + token + ); + const mappedData = response.data.content.map((job: any) => ({ + id: job.jobDescriptionId, + title: job.title, + description: job.enterpriseName, + dday: job.remainingDate, + recruitmentPeriod: formatDateRange(job.startedAt, job.endedAt), + status: job.writeStatus, + })); + setJobsData(mappedData); + setpageTotal(response.data.totalPage); + console.log(jobsData); + setIsLoading(false); + } catch (error) { + console.error(error); + alert(JSON.stringify(error)); + } + } else { + } + }; + return ( {!isLoading ? ( @@ -147,7 +232,7 @@ const JDListPage: React.FC = () => { handleSortChange("CREATED")} > 등록순 @@ -162,7 +247,7 @@ const JDListPage: React.FC = () => { - {jobsData.length === 0 ? ( + {jobsData.length === 0 && activeButton === "전체" ? (
아직 등록된 공고가 없어요.
새 공고를 등록해주세요.
@@ -187,7 +272,11 @@ const JDListPage: React.FC = () => { /> {pages} setCurrentPage( diff --git a/src/services/jd.tsx b/src/services/jd.tsx index 18f1454..e970356 100644 --- a/src/services/jd.tsx +++ b/src/services/jd.tsx @@ -39,13 +39,21 @@ export const jobpatch = async (job: JobAPI, jdId: string, token: string) => { ); }; -export const jobget = async ( +export const jobget = async (page: string, token: string) => { + return await client.get(`/api/job-description?page=${page}&size=9`, { + headers: { + Authorization: `Bearer ${token}`, + }, + }); +}; + +export const filteredjobget = async ( page: string, - writeStatus: string | null, + writeStatus: string, sortType: string, token: string ) => { - if (writeStatus) { + if (writeStatus !== "ALL") { return await client.get( `/api/job-description?page=${page}&size=9&writeStatus=${writeStatus}&sortType=${sortType}`, { @@ -55,11 +63,14 @@ export const jobget = async ( } ); } else { - return await client.get(`/api/job-description?page=${page}&size=9`, { - headers: { - Authorization: `Bearer ${token}`, - }, - }); + return await client.get( + `/api/job-description?page=${page}&size=9&sortType=${sortType}`, + { + headers: { + Authorization: `Bearer ${token}`, + }, + } + ); } };