Commit 5984db4a authored by Phạm Quang Bảo's avatar Phạm Quang Bảo

fix: search param

parent 1094de1d
"use client"; "use client";
import { useMemo, useState } from "react"; import { useMemo } from "react";
import Link from "next/link"; import Link from "next/link";
import Image from "next/image"; import Image from "next/image";
import { usePathname, useRouter, useSearchParams } from "next/navigation";
import { useGetApiV10Post } from "@/api/vcci-news/endpoints/post"; import { useGetApiV10Post } from "@/api/vcci-news/endpoints/post";
import type { DynamicPostItem } from "@/api/vcci-news/types/post"; import type { DynamicPostItem } from "@/api/vcci-news/types/post";
import { Pagination } from "@/components/base/pagination"; import { Pagination } from "@/components/base/pagination";
...@@ -19,7 +20,7 @@ import { ...@@ -19,7 +20,7 @@ import {
const PAGE_SIZE = 10; const PAGE_SIZE = 10;
const CURRENT_YEAR = new Date().getFullYear(); const CURRENT_YEAR = new Date().getFullYear();
const START_YEAR = 2020; const START_YEAR = 2009;
const YEAR_OPTIONS = Array.from( const YEAR_OPTIONS = Array.from(
{ length: CURRENT_YEAR - START_YEAR + 1 }, { length: CURRENT_YEAR - START_YEAR + 1 },
...@@ -40,8 +41,40 @@ const formatPostDate = (value?: string | null) => { ...@@ -40,8 +41,40 @@ const formatPostDate = (value?: string | null) => {
}; };
export default function NewsListing() { export default function NewsListing() {
const [year, setYear] = useState<number | null>(null); const router = useRouter();
const [page, setPage] = useState(1); const pathname = usePathname();
const searchParams = useSearchParams();
const searchParamsString = searchParams.toString();
const yearParam = Number(searchParams.get("year"));
const year = YEAR_OPTIONS.includes(yearParam) ? yearParam : null;
const pageParam = Number(searchParams.get("page") ?? "1");
const page =
Number.isFinite(pageParam) && pageParam > 0 ? Math.floor(pageParam) : 1;
const updateSearchParams = (next: { year?: number | null; page?: number }) => {
const params = new URLSearchParams(searchParamsString);
const nextYear = next.year === undefined ? year : next.year;
const nextPage = next.page ?? page;
if (nextYear) {
params.set("year", String(nextYear));
} else {
params.delete("year");
}
if (nextPage > 1) {
params.set("page", String(nextPage));
} else {
params.delete("page");
}
const queryString = params.toString();
router.replace(queryString ? `${pathname}?${queryString}` : pathname, {
scroll: false,
});
};
const filterParts = useMemo(() => { const filterParts = useMemo(() => {
const parts = ["is_hidden==false", "is_active==true", "type==news"]; const parts = ["is_hidden==false", "is_active==true", "type==news"];
...@@ -70,8 +103,11 @@ export default function NewsListing() { ...@@ -70,8 +103,11 @@ export default function NewsListing() {
); );
const handleSelectYear = (value: number | null) => { const handleSelectYear = (value: number | null) => {
setYear(value); updateSearchParams({ year: value, page: 1 });
setPage(1); };
const handleChangePage = (nextPage: number) => {
updateSearchParams({ page: nextPage });
}; };
return ( return (
...@@ -159,12 +195,12 @@ export default function NewsListing() { ...@@ -159,12 +195,12 @@ export default function NewsListing() {
<Pagination <Pagination
pageCount={totalPages} pageCount={totalPages}
page={currentPage} page={currentPage}
onChangePage={setPage} onChangePage={handleChangePage}
onGoToPreviousPage={() => onGoToPreviousPage={() =>
setPage(Math.max(1, currentPage - 1)) handleChangePage(Math.max(1, currentPage - 1))
} }
onGoToNextPage={() => onGoToNextPage={() =>
setPage(Math.min(totalPages, currentPage + 1)) handleChangePage(Math.min(totalPages, currentPage + 1))
} }
/> />
</div> </div>
......
import { Suspense } from "react";
import type { Metadata } from "next"; import type { Metadata } from "next";
import links from "@links/index"; import links from "@links/index";
import NewsListing from "./news-listing"; import NewsListing from "./news-listing";
...@@ -12,5 +13,15 @@ export const metadata: Metadata = { ...@@ -12,5 +13,15 @@ export const metadata: Metadata = {
}; };
export default function NewsPage() { export default function NewsPage() {
return <NewsListing />; return (
<Suspense
fallback={
<div className="flex min-h-[60vh] w-full items-center justify-center">
<span className="text-sm text-gray-500">Đang tải...</span>
</div>
}
>
<NewsListing />
</Suspense>
);
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment