Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add static slug for province detail page #361

Merged
merged 16 commits into from
Jul 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions __tests__/pages/provinces.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import provinces from "~/lib/provinces";
import { getInitial } from "~/lib/string-utils";
import { getStaticProps } from "~/pages/provinces";

describe("getStaticProps", () => {
it("returns the props from sheets correctly", () => {
const provincesList = provinces.map(({ name, slug, data }) => ({
initials: getInitial(name),
name,
slug,
count: data.length,
}));

expect(getStaticProps({})).toEqual({
props: { provincesList },
});
});
});
5 changes: 5 additions & 0 deletions etc/fetchers/fetch-sheets.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
allIsEmptyString,
getKebabCase,
getSlug,
toSnakeCase,
toTitleCase,
Expand Down Expand Up @@ -55,9 +56,11 @@ export async function fetchSheets() {
})
.toArray()
.filter((row) => !allIsEmptyString(row));

return {
id: sheetId,
name: sheetName,
slug: getKebabCase(sheetName),
data: sheetRows.map((row, rowIndex) => {
return sheetColumns.reduce(
(prev: Record<string, number | string>, col) => {
Expand All @@ -84,6 +87,8 @@ export async function fetchSheets() {
})
.toArray();

sheetList.shift();

fs.writeFileSync(
path.resolve(__dirname, "../../data/wbw-sheets.json"),
JSON.stringify(sheetList),
Expand Down
1 change: 1 addition & 0 deletions lib/__mocks__/builders/provinces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const provinceBuilder = build<Province>({
fields: {
id: sequence(),
name: fake((f) => f.address.state()),
slug: fake((f) => f.lorem.slug()),
data: [contactBuilder()],
},
});
11 changes: 8 additions & 3 deletions lib/provinces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type Provinces = Province[];
export type Province = {
readonly id: number;
readonly name: string;
readonly slug: string;
readonly data: Contact[];
};

Expand All @@ -32,13 +33,17 @@ export type ProvincePath = {
};
};

export const getProvincesPaths = (): ProvincePath[] =>
provinces.map((item, index) => {
const provinceSlug = getSlug(item.name, index);
export const getProvincesPaths = (): ProvincePath[] => {
const provincees = provinces as unknown as Province[];

return provincees.map((province) => {
const provinceSlug = province.slug;

return {
params: { provinceSlug },
};
});
};

export type ContactPath = {
params: {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"lint": "eslint \"**/*.{js,jsx,ts,tsx}\"",
"lint:fix": "eslint --fix \"**/*.{js,jsx,ts,tsx}\"",
"mirror-box": "ts-node etc/mirror-box.ts",
"netlify-export": "yarn mirror-box && yarn build && next export",
"netlify-export": "yarn fetch-wbw && yarn build && next export",
"prepare": "husky install",
"start": "next start",
"cypress:open": "cypress open",
Expand Down
4 changes: 1 addition & 3 deletions pages/provinces/[provinceSlug]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { getCurrentLongDate } from "~/lib/date-utils";
import { useSearch } from "~/lib/hooks/use-search";
import { getProvinceMeta } from "~/lib/meta";
import provinces, { Contact, getProvincesPaths } from "~/lib/provinces";
import { getTheLastSegmentFromKebabCase } from "~/lib/string-utils";

import { GetStaticPaths, GetStaticProps } from "next";
import { useRouter } from "next/router";
Expand Down Expand Up @@ -139,8 +138,7 @@ export const getStaticPaths: GetStaticPaths = () => {

export const getStaticProps: GetStaticProps = ({ params = {} }) => {
const { provinceSlug } = params;
const index = getTheLastSegmentFromKebabCase(provinceSlug as string);
const province = index ? provinces[index as unknown as number] : null;
const province = provinces.find((prov) => prov.slug === provinceSlug);
const provinceName = province ? province.name : "";
const contactList = province
? [...province.data].sort((a, b) => {
Expand Down
8 changes: 4 additions & 4 deletions pages/provinces/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { SeoText } from "~/components/seo-text";
import { getCurrentMonthAndYear } from "~/lib/date-utils";
import { useSearch } from "~/lib/hooks/use-search";
import provinces from "~/lib/provinces";
import { getInitial, getSlug } from "~/lib/string-utils";
import { getInitial } from "~/lib/string-utils";

import { GetStaticProps } from "next";
import { NextSeo } from "next-seo";
Expand Down Expand Up @@ -65,13 +65,13 @@ export default function ProvincesPage(props: ProvincesPageProps) {
}

export const getStaticProps: GetStaticProps = () => {
const provincesList = provinces.map(({ name, data }, index) => ({
const provincesList = provinces.map(({ name, slug, data }) => ({
initials: getInitial(name),
name,
slug: getSlug(name, index),
slug,
count: data.length,
}));
provincesList.shift();

return {
props: {
provincesList,
Expand Down