diff --git a/src/app/api/mypage/gathering/route.ts b/src/app/api/mypage/gathering/route.ts index 86ebdd6d..870320f4 100644 --- a/src/app/api/mypage/gathering/route.ts +++ b/src/app/api/mypage/gathering/route.ts @@ -12,20 +12,27 @@ export async function GET(request: NextRequest) { return new NextResponse("Refresh token not found", { status: 401 }); } - const { searchParams } = new URL(request.url); - const category = searchParams.get("category"); - const page = searchParams.get("page") || "1"; // 기본값을 1로 설정 + const url = new URL(request.url); + const params = new URLSearchParams(url.search); + const category = params.get("category"); + const page = Number(params.get("page")); + if (page < 0 || !Number.isSafeInteger(page)) { + throw new Error("Invalid Page Number"); + } + params.delete("category"); + url.search = params.toString(); try { const response = await fetch( - `${process.env.BACKEND_URL}/api/users/mypage/gathering/${category}${+page > 1 ? "?page=" + page : ""}`, + `${process.env.BACKEND_URL}/api/users/mypage/gathering/${category}` + + url.search, { method: "GET", headers: { Cookie: `${access_cookie?.name}=${access_cookie?.value}`, "Content-Type": "application/json", }, - cache: "no-store" + cache: "no-store", }, ); diff --git a/src/app/api/mypage/information/route.ts b/src/app/api/mypage/information/route.ts index 34f302d1..b5184136 100644 --- a/src/app/api/mypage/information/route.ts +++ b/src/app/api/mypage/information/route.ts @@ -12,27 +12,33 @@ export async function GET(request: NextRequest) { return new NextResponse("Refresh token not found", { status: 401 }); } - const { searchParams } = new URL(request.url); - const category = searchParams.get("category"); - const page = searchParams.get("page") || "1"; // 기본값을 1로 설정 + const url = new URL(request.url); + const params = new URLSearchParams(url.search); + const category = params.get("category"); + const page = Number(params.get("page")); + if (page < 0 || !Number.isSafeInteger(page)) { + throw new Error("Invalid Page Number"); + } + params.delete("category"); + url.search = params.toString(); try { const response = await fetch( - `${process.env.BACKEND_URL}/api/users/mypage/information/${category}${+page > 1 ? "?page=" + page : ""}`, + `${process.env.BACKEND_URL}/api/users/mypage/information/${category}` +url.search, { method: "GET", headers: { Cookie: `${access_cookie?.name}=${access_cookie?.value}`, "Content-Type": "application/json", }, - cache: "no-store" + cache: "no-store", }, ); if (!response.ok) { throw new Error(response.statusText); } - + return response; // return NextResponse.json({ status: 200, message: "마이페이지 게시물 조회 성공" }); } catch (err) { diff --git a/src/app/mypage/page.tsx b/src/app/mypage/page.tsx index a039ec7b..06228ae6 100644 --- a/src/app/mypage/page.tsx +++ b/src/app/mypage/page.tsx @@ -36,7 +36,7 @@ export default async function page() { return (
diff --git a/src/containers/mypage/MyPageGatheringContainer.tsx b/src/containers/mypage/MyPageGatheringContainer.tsx index 5ce250a1..f12499ed 100644 --- a/src/containers/mypage/MyPageGatheringContainer.tsx +++ b/src/containers/mypage/MyPageGatheringContainer.tsx @@ -51,13 +51,20 @@ interface Gathering { const MyPageGatheringContainer = (props: IMyPageGatheringContainer) => { const searchParams = useSearchParams(); const [activeCategory, setActiveCategory] = useState(""); - const [currentPage, setCurrentPage] = useState(1); + const [currentPage, setCurrentPage] = useState( + Number(searchParams.get("page")) || 1, + ); const [elements, setElements] = useState([]); const [totalElements, setTotalElements] = useState(0); const [isLoading, setIsLoading] = useState(false); - const pageHandler = (page: number) => { - setCurrentPage(page); - } + const pageHandler = (page: number) => { + const url = new URL(window.location.href); + const params = new URLSearchParams(url.search); + params.set("page", page + ""); + url.search = params.toString(); + setCurrentPage(page); + window.history.pushState({}, "", url.toString()); + }; const onClickCategoryHandler = (value: string) => { const url = new URL(window.location.href); const params = new URLSearchParams(url.search); @@ -91,6 +98,14 @@ const MyPageGatheringContainer = (props: IMyPageGatheringContainer) => { cache: "no-store", }, ); + + if (!res.ok) { + setElements([]); + setTotalElements(0); + setIsLoading(false); + return; + } + const data = await res.json(); setElements(data.content); setTotalElements(data.page.totalElements); @@ -107,10 +122,7 @@ const MyPageGatheringContainer = (props: IMyPageGatheringContainer) => { onClickHandler={onClickCategoryHandler} activeCategory={activeCategory} /> - + { const [elements, setElements] = useState([]); const [totalElements, setTotalElements] = useState(0); const [isLoading, setIsLoading] = useState(false); - const pageHandler = (page: number) => { - setCurrentPage(page); - } + const pageHandler = (page: number) => { + const url = new URL(window.location.href); + const params = new URLSearchParams(url.search); + params.set("page", page + ""); + url.search = params.toString(); + setCurrentPage(page); + window.history.pushState({}, "", url.toString()); + }; const onClickCategoryHandler = (value: string) => { const url = new URL(window.location.href); const params = new URLSearchParams(url.search); @@ -150,7 +155,7 @@ const onBookMarkClick = async (id: number) => { />