Skip to content

Commit

Permalink
Merge pull request #361 from Namchee/feat/provincee-static-slug
Browse files Browse the repository at this point in the history
feat: Add static slug for province detail page
  • Loading branch information
zainfathoni authored Jul 25, 2021
2 parents cf1637d + 5fe61d8 commit ebb6ee3
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 11 deletions.
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

0 comments on commit ebb6ee3

Please sign in to comment.