diff --git a/app/page.tsx b/app/page.tsx index 0e68918..38113ba 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -1,14 +1,10 @@ import { Infographic } from "@/components/Infographic"; -import { getElectricCarRegistrationByMake } from "@/lib/getElectricCarRegistrationByMake"; -import { BASE_URL } from "@/config"; +import { API_URL, BASE_URL } from "@/config"; import { WebSite, WithContext } from "schema-dts"; import type { Car } from "@/types"; const Home = async () => { - // TODO: Temporary solution while building a more permanent one. - const electricCars: Car[] = await getElectricCarRegistrationByMake( - `https://raw.githubusercontent.com/ruchernchong/singapore-ev-trends/main/public/data/M03-Car_Regn_by_make.csv`, - ); + const electricCars: Car[] = await fetch(API_URL).then((res) => res.json()); const jsonLd: WithContext = { "@context": "https://schema.org", diff --git a/config/index.ts b/config/index.ts index 5ca0982..5eb7788 100644 --- a/config/index.ts +++ b/config/index.ts @@ -1,4 +1,5 @@ export const BASE_URL: string = `https://singapore-ev-trends.ruchern.xyz`; +export const API_URL: string = `https://api.singapore-ev-trends.ruchern.xyz`; export enum FUEL_TYPE { DIESEL = "Diesel", diff --git a/lib/getElectricCarRegistrationByMake.ts b/lib/getElectricCarRegistrationByMake.ts deleted file mode 100644 index 8603176..0000000 --- a/lib/getElectricCarRegistrationByMake.ts +++ /dev/null @@ -1,44 +0,0 @@ -import * as d3 from "d3"; -import { FUEL_TYPE } from "@/config"; -import { sortByMake } from "@/lib/sortByMake"; -import type { Car } from "@/types"; - -export const getElectricCarRegistrationByMake = async ( - filePath: string, -): Promise => { - const csvContent: string = await fetch(filePath, { cache: "no-store" }).then( - (res) => res.text(), - ); - - const carRegistrationByMake = d3.csvParse(csvContent); - - const electricCars: Car[] = carRegistrationByMake - .filter( - ({ fuel_type, number }) => - fuel_type === FUEL_TYPE.ELECTRIC && +number !== 0, - ) - .reduce((result: Car[], { month, make, fuel_type, number }) => { - const existingCar = result.find( - (car) => car.month === month && car.make === make, - ); - - if (existingCar) { - existingCar.number += Number(number); - } else { - result.push({ - month, - make, - fuel_type, - number: Number(number), - }); - } - - return result; - }, []) - .map((car) => ({ ...car, number: +car.number })) - .sort(sortByMake); - - console.table(electricCars); - - return electricCars; -};