-
Notifications
You must be signed in to change notification settings - Fork 0
/
location.ts
36 lines (31 loc) · 1.06 KB
/
location.ts
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 { GetStaticPaths, GetStaticProps } from "next";
import laLocationData from "./data/locations/la.json";
import fs from "fs";
import path from "path";
export type LocationData = typeof laLocationData;
const allLocationData = () => {
const locationDataPath = "./data/locations";
return fs
.readdirSync(locationDataPath)
.filter((filename) => path.extname(filename) == ".json")
.map((filename) => fs.readFileSync(`${locationDataPath}/${filename}`))
.map((file) => {
const locationData = JSON.parse(`${file}`) as LocationData;
return locationData;
});
};
// Only use from getStaticPaths or getStaticProps
export const ALL_LOCATION_DATA = allLocationData();
export const getLocationProps: GetStaticProps = async (context) => {
return {
props: {
location: ALL_LOCATION_DATA.find((data) => data.title == context.params?.location)!,
}
}
}
export const getLocationPaths: GetStaticPaths = async () => {
return {
paths: ALL_LOCATION_DATA.map(location => ({ params: { location: location.title } })),
fallback: false,
}
}