Skip to content

Commit acd63dc

Browse files
committed
Paginate category images
1 parent 0e27cb1 commit acd63dc

File tree

3 files changed

+91
-3
lines changed

3 files changed

+91
-3
lines changed

src/app/apiSlice.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ interface GetCategoryImagesRequest {
7575
page: number;
7676
}
7777

78+
interface GetCategoryImagesResponse {
79+
images: Image[];
80+
total_images: number;
81+
page: number;
82+
total_pages: number;
83+
}
84+
7885
interface GetSessionRequest {
7986
category: string;
8087
tags: TagMap;
@@ -231,7 +238,7 @@ export const api = createApi({
231238
}),
232239
invalidatesTags: ["category-images"],
233240
}),
234-
getCategoryImages: build.query<Image[], GetCategoryImagesRequest>({
241+
getCategoryImages: build.query<GetCategoryImagesResponse, GetCategoryImagesRequest>({
235242
query: ({ category, page }) => ({
236243
url: `categories/${category}/images`,
237244
method: "GET",

src/components/Pagination.tsx

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
interface Props {
2+
totalPages: number;
3+
currentPage: number;
4+
onPageSelected?: (newPage: number) => void;
5+
}
6+
7+
function Pagination({ totalPages, currentPage, onPageSelected }: Props) {
8+
const selectPage = (page: number) => {
9+
if (onPageSelected) {
10+
onPageSelected(page);
11+
}
12+
};
13+
14+
return (
15+
<div className="flex rounded-lg bg-slate-800 px-2">
16+
{currentPage > 1 && (
17+
<a
18+
href="#"
19+
className="px-2 py-1.5"
20+
onClick={(e) => {
21+
e.preventDefault();
22+
selectPage(1);
23+
}}
24+
>
25+
{1}
26+
</a>
27+
)}
28+
{currentPage - 1 > 2 && <span className="px-1 py-1.5"></span>}
29+
{currentPage - 1 > 1 && (
30+
<a
31+
href="#"
32+
className="px-2 py-1.5"
33+
onClick={(e) => {
34+
e.preventDefault();
35+
selectPage(currentPage - 1);
36+
}}
37+
>
38+
{currentPage - 1}
39+
</a>
40+
)}
41+
<span className="cursor-default px-2 py-1.5 font-bold opacity-40">{currentPage}</span>
42+
{currentPage + 1 < totalPages && (
43+
<a
44+
href="#"
45+
className="px-2 py-1.5"
46+
onClick={(e) => {
47+
e.preventDefault();
48+
selectPage(currentPage + 1);
49+
}}
50+
>
51+
{currentPage + 1}
52+
</a>
53+
)}
54+
{currentPage + 1 < totalPages - 1 && <span className="px-1 py-1.5"></span>}
55+
{currentPage < totalPages && (
56+
<a
57+
href="#"
58+
className="px-2 py-1.5"
59+
onClick={(e) => {
60+
e.preventDefault();
61+
selectPage(totalPages);
62+
}}
63+
>
64+
{totalPages}
65+
</a>
66+
)}
67+
</div>
68+
);
69+
}
70+
71+
export default Pagination;

src/routes/AdminEditCategory.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { useUploadImageMutation } from "../app/uploadSlice";
2222
import NotFound from "./NotFound";
2323
import { TagMap } from "../types/drawref";
2424
import { parseError } from "../app/utilities";
25+
import Pagination from "../components/Pagination";
2526

2627
type Params = {
2728
categoryId: string;
@@ -35,12 +36,14 @@ function AdminEditCategory() {
3536
return <NotFound />;
3637
}
3738

39+
const [imagesPage, setImagesPage] = useState(1);
40+
3841
const { data: categoryData, isLoading } = useGetCategoryQuery(categoryId);
3942
const {
4043
data: categoryImages,
4144
isLoading: isLoadingCategoryImages,
4245
error: getCategoryImagesError,
43-
} = useGetCategoryImagesQuery({ category: categoryId, page: 0 });
46+
} = useGetCategoryImagesQuery({ category: categoryId, page: imagesPage - 1 });
4447

4548
const [existingAuthor, setExistingAuthor] = useState("");
4649
const [typeOfUpload, setTypeOfUpload] = useState("upload");
@@ -285,9 +288,16 @@ function AdminEditCategory() {
285288
{!isLoadingCategoryImages && (
286289
<div className="box-border flex w-[28em] max-w-full flex-col gap-3 border-[5px] border-primary-700 bg-primary-900 px-4 py-6">
287290
<h2 className="text-xl font-medium">Images</h2>
291+
<div className="flex justify-center">
292+
<Pagination
293+
totalPages={categoryImages?.total_pages || 0}
294+
currentPage={imagesPage}
295+
onPageSelected={(newPage) => setImagesPage(newPage)}
296+
/>
297+
</div>
288298
<div className="flex flex-wrap items-center justify-center gap-4">
289299
{categoryImages &&
290-
categoryImages.map((img) => (
300+
categoryImages.images.map((img) => (
291301
<button
292302
key={img.id}
293303
className="h-20 w-20 rounded-lg bg-cover hover:border-8 hover:border-red-500 hover:blur"

0 commit comments

Comments
 (0)