From 6d9d7492fc98cac3e1baac703f841d7a39163a8c Mon Sep 17 00:00:00 2001 From: Will Lin Date: Fri, 2 Jul 2021 16:17:00 -0400 Subject: [PATCH 1/2] Fix the metadata from the axios response. I had the incorrect type on the axios response, which led to the metadata being undefined, so no pages were being generated. --- sites/public/pages/listings/page/[page].tsx | 33 ++++++++++++++------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/sites/public/pages/listings/page/[page].tsx b/sites/public/pages/listings/page/[page].tsx index fefce437de..b3ba32226e 100644 --- a/sites/public/pages/listings/page/[page].tsx +++ b/sites/public/pages/listings/page/[page].tsx @@ -1,5 +1,5 @@ import Head from "next/head" -import axios from "axios" +import axios, { AxiosResponse } from "axios" import { ListingsList, PageHeader, t } from "@bloom-housing/ui-components" import { Listing, PaginatedListings } from "@bloom-housing/backend-core/types" import Layout from "../../../layouts/application" @@ -38,15 +38,28 @@ export default function ListingsPage(props: ListingsProps) { export async function getStaticPaths(context: { locales: Array }) { try { - const response: PaginatedListings = await axios.get(process.env.listingServiceUrl, { - params: { page: "1", limit: "10" }, - }) + const response: AxiosResponse = await axios.get( + process.env.listingServiceUrl, + { + // TODO: Think about making this limit configurable. + params: { page: "1", limit: "10" }, + } + ) + /** + * We want to return an array inside paths that contains an entry for each combination + * of locale and page number. So we create an array that goes from 1 .. totalPages and + * map that with locales. + */ return { - paths: context.locales.flatMap((locale: string) => ({ - params: { page: response.meta.totalPages }, - locale: locale, - })), + paths: context.locales.flatMap((locale: string) => + Array.from({ length: response.data.meta.totalPages }, (_, index) => index + 1).map( + (currentPage: number) => ({ + params: { page: currentPage.toString() }, + locale: locale, + }) + ) + ), fallback: false, } } catch (e) { @@ -62,10 +75,10 @@ export async function getStaticProps(context: { params: Record } const response = await axios.get(process.env.listingServiceUrl, { params: { page: context.params.page, limit: "10" }, }) - return { props: { listings: response.data.items }, revalidate: 120 } + return { props: { listings: response.data.items } } } catch (error) { console.error(error) } - return { props: { listings: [] }, revalidate: 120 } + return { props: { listings: [] } } } From f555f7b8bfa71d3274b4d8a8fa80a139ef3d06cc Mon Sep 17 00:00:00 2001 From: Will Lin Date: Tue, 13 Jul 2021 20:58:21 -0400 Subject: [PATCH 2/2] Remove the listings/[page] now that pagination is checked in. --- sites/public/pages/listings/page/[page].tsx | 84 --------------------- 1 file changed, 84 deletions(-) delete mode 100644 sites/public/pages/listings/page/[page].tsx diff --git a/sites/public/pages/listings/page/[page].tsx b/sites/public/pages/listings/page/[page].tsx deleted file mode 100644 index b3ba32226e..0000000000 --- a/sites/public/pages/listings/page/[page].tsx +++ /dev/null @@ -1,84 +0,0 @@ -import Head from "next/head" -import axios, { AxiosResponse } from "axios" -import { ListingsList, PageHeader, t } from "@bloom-housing/ui-components" -import { Listing, PaginatedListings } from "@bloom-housing/backend-core/types" -import Layout from "../../../layouts/application" -import { MetaTags } from "../../../src/MetaTags" - -export interface ListingsProps { - listings: Listing[] -} - -const openListings = (listings) => { - return listings.length > 0 ? ( - - ) : ( -
-

{t("listings.noOpenListings")}

-
- ) -} - -export default function ListingsPage(props: ListingsProps) { - const pageTitle = `${t("pageTitle.rent")} - ${t("nav.siteTitle")}` - const metaDescription = t("pageDescription.welcome", { regionName: t("region.name") }) - const metaImage = "" // TODO: replace with hero image - - return ( - - - {pageTitle} - - - -
{openListings(props.listings)}
-
- ) -} - -export async function getStaticPaths(context: { locales: Array }) { - try { - const response: AxiosResponse = await axios.get( - process.env.listingServiceUrl, - { - // TODO: Think about making this limit configurable. - params: { page: "1", limit: "10" }, - } - ) - - /** - * We want to return an array inside paths that contains an entry for each combination - * of locale and page number. So we create an array that goes from 1 .. totalPages and - * map that with locales. - */ - return { - paths: context.locales.flatMap((locale: string) => - Array.from({ length: response.data.meta.totalPages }, (_, index) => index + 1).map( - (currentPage: number) => ({ - params: { page: currentPage.toString() }, - locale: locale, - }) - ) - ), - fallback: false, - } - } catch (e) { - return { - paths: [], - fallback: false, - } - } -} - -export async function getStaticProps(context: { params: Record }) { - try { - const response = await axios.get(process.env.listingServiceUrl, { - params: { page: context.params.page, limit: "10" }, - }) - return { props: { listings: response.data.items } } - } catch (error) { - console.error(error) - } - - return { props: { listings: [] } } -}