forked from api-platform/create-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(next): add static generation for the list (api-platform#359)
- Loading branch information
1 parent
271f654
commit 001d7f3
Showing
11 changed files
with
135 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { NextComponentType, NextPageContext } from "next"; | ||
import { useRouter } from "next/router"; | ||
import Head from "next/head"; | ||
import { useQuery } from "react-query"; | ||
|
||
import Pagination from "../common/Pagination"; | ||
import { List } from "./List"; | ||
import { PagedCollection } from "../../types/collection"; | ||
import { {{{ucf}}} } from "../../types/{{{ucf}}}"; | ||
import { fetch, 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 fetch<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>(get{{{ucf}}}sPath(page), 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> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,18 @@ | ||
import { GetServerSideProps, NextComponentType, NextPageContext } from "next"; | ||
import Head from "next/head"; | ||
import { dehydrate, QueryClient, useQuery } from "react-query"; | ||
import { GetStaticProps } from "next"; | ||
import { dehydrate, QueryClient } from "react-query"; | ||
|
||
import Pagination from "../../components/common/Pagination"; | ||
import { List } from "../../components/{{{lc}}}/List"; | ||
import { PagedCollection } from "../../types/collection"; | ||
import { {{{ucf}}} } from "../../types/{{{ucf}}}"; | ||
import { fetch, FetchResponse } from "../../utils/dataAccess"; | ||
import { useMercure } from "../../utils/mercure"; | ||
import { PageList, get{{{ucf}}}s, get{{{ucf}}}sPath } from "../../components/{{{lc}}}/PageList"; | ||
|
||
const get{{{ucf}}}s = async () => await fetch<PagedCollection<{{{ucf}}}>>('/{{{name}}}'); | ||
|
||
const Page: NextComponentType<NextPageContext> = () => { | ||
const { data: { data: {{lc}}s, hubURL } = { hubURL: null } } = | ||
useQuery<FetchResponse<PagedCollection<{{{ucf}}}>> | undefined>('{{{name}}}', get{{{ucf}}}s); | ||
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} /> | ||
</div> | ||
); | ||
}; | ||
|
||
export const getServerSideProps: GetServerSideProps = async () => { | ||
export const getStaticProps: GetStaticProps = async () => { | ||
const queryClient = new QueryClient(); | ||
await queryClient.prefetchQuery('{{{name}}}', get{{{ucf}}}s); | ||
await queryClient.prefetchQuery(get{{{ucf}}}sPath(), get{{{ucf}}}s()); | ||
|
||
return { | ||
props: { | ||
dehydratedState: dehydrate(queryClient), | ||
}, | ||
revalidate: 1, | ||
}; | ||
} | ||
}; | ||
|
||
export default Page; | ||
export default PageList; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { GetStaticPaths, GetStaticProps } from "next"; | ||
import { dehydrate, QueryClient } from "react-query"; | ||
|
||
import { PageList, get{{{ucf}}}s, get{{{ucf}}}sPath } from "../../../components/{{{lc}}}/PageList"; | ||
import { PagedCollection } from "../../../types/collection"; | ||
import { {{{ucf}}} } from "../../../types/{{{ucf}}}"; | ||
import { fetch, getCollectionPaths } from "../../../utils/dataAccess"; | ||
|
||
export const getStaticProps: GetStaticProps = async ({ params: { page } = {} }) => { | ||
const queryClient = new QueryClient(); | ||
await queryClient.prefetchQuery(get{{{ucf}}}sPath(page), get{{{ucf}}}s(page)); | ||
|
||
return { | ||
props: { | ||
dehydratedState: dehydrate(queryClient), | ||
}, | ||
revalidate: 1, | ||
}; | ||
}; | ||
|
||
export const getStaticPaths: GetStaticPaths = async () => { | ||
const response = await fetch<PagedCollection<{{{ucf}}}>>("/{{{name}}}"); | ||
const paths = await getCollectionPaths(response, "{{{name}}}", "/{{{lc}}}s/page/[page]"); | ||
|
||
return { | ||
paths, | ||
fallback: true, | ||
}; | ||
}; | ||
|
||
export default PageList; |
Oops, something went wrong.