-
-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathPageList.tsx
36 lines (31 loc) · 1.61 KB
/
PageList.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { NextComponentType, NextPageContext } from "next";
import { useRouter } from "next/router";
import Head from "next/head";
import { useQuery } from "@tanstack/react-query";
import Pagination from "../common/Pagination";
import { List } from "./List";
import { PagedCollection } from "../../types/collection";
import { {{{ucf}}} } from "../../types/{{{ucf}}}";
import { fetchApi, FetchResponse, parsePage } from "../../utils/dataAccess";
import { useMercure } from "../../utils/mercure";
export const get{{{ucf}}}sPath = (page?: string | string[] | undefined) => `/{{{name}}}${typeof page === 'string' ? `?page=${page}` : ''}`;
export const get{{{ucf}}}s = (page?: string | string[] | undefined) => async () => await fetchApi<PagedCollection<{{{ucf}}}>>(get{{{ucf}}}sPath(page));
const getPagePath = (path: string) => `/{{{lc}}}s/page/${parsePage("{{{name}}}", path)}`;
export const PageList: NextComponentType<NextPageContext> = () => {
const { query: { page } } = useRouter();
const { data: { data: {{lc}}s, hubURL } = { hubURL: null } } =
useQuery<FetchResponse<PagedCollection<{{{ucf}}}>> | undefined, Error, FetchResponse<PagedCollection<{{{ucf}}}>> | undefined>({queryKey: [get{{{ucf}}}sPath(page)], queryFn: get{{{ucf}}}s(page)});
const collection = useMercure({{lc}}s, hubURL);
if (!collection || !collection["{{{hydraPrefix}}}member"]) return null;
return (
<div>
<div>
<Head>
<title>{{{ucf}}} List</title>
</Head>
</div>
<List {{{lc}}}s={collection["{{{hydraPrefix}}}member"]} />
<Pagination collection={collection} getPagePath={getPagePath} />
</div>
);
};