Skip to content

Commit

Permalink
Merge pull request #129 from sgcarstrends/128-duplicated-makes-in-veh…
Browse files Browse the repository at this point in the history
…icle-type-page

Fix duplicated makes in vehicle type page
  • Loading branch information
ruchernchong authored Sep 20, 2024
2 parents c96587a + 5e0adc8 commit 64645d5
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 4 deletions.
137 changes: 137 additions & 0 deletions __mocks__/mock-cars.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
[
{
"_id": "66e29f9807b823f9e46e52ce",
"month": "2024-08",
"make": "BMW",
"importer_type": "PI",
"fuel_type": "Electric",
"vehicle_type": "Sports Utility Vehicle",
"number": ""
},
{
"_id": "66e29f9807b823f9e46e52cd",
"month": "2024-08",
"make": "BMW",
"importer_type": "PI",
"fuel_type": "Petrol-Electric",
"vehicle_type": "Sports Utility Vehicle",
"number": ""
},
{
"_id": "66e29f9807b823f9e46e52cc",
"month": "2024-08",
"make": "BMW",
"importer_type": "PI",
"fuel_type": "Petrol",
"vehicle_type": "Sports Utility Vehicle",
"number": ""
},
{
"_id": "66e29f9807b823f9e46e52cb",
"month": "2024-08",
"make": "BMW",
"importer_type": "AMD",
"fuel_type": "Electric",
"vehicle_type": "Sports Utility Vehicle",
"number": 113
},
{
"_id": "66e29f9807b823f9e46e52ca",
"month": "2024-08",
"make": "BMW",
"importer_type": "AMD",
"fuel_type": "Petrol-Electric",
"vehicle_type": "Sports Utility Vehicle",
"number": 17
},
{
"_id": "66e29f9807b823f9e46e52c9",
"month": "2024-08",
"make": "BMW",
"importer_type": "AMD",
"fuel_type": "Petrol",
"vehicle_type": "Sports Utility Vehicle",
"number": 117
},
{
"_id": "66e29f9807b823f9e46e52d0",
"month": "2024-08",
"make": "BYD",
"importer_type": "AMD",
"fuel_type": "Electric",
"vehicle_type": "Sports Utility Vehicle",
"number": 363
},
{
"_id": "66e29f9807b823f9e46e52fe",
"month": "2024-08",
"make": "MERCEDES BENZ",
"importer_type": "PI",
"fuel_type": "Electric",
"vehicle_type": "Sports Utility Vehicle",
"number": ""
},
{
"_id": "66e29f9807b823f9e46e52fd",
"month": "2024-08",
"make": "MERCEDES BENZ",
"importer_type": "PI",
"fuel_type": "Petrol-Electric",
"vehicle_type": "Sports Utility Vehicle",
"number": 2
},
{
"_id": "66e29f9807b823f9e46e52fc",
"month": "2024-08",
"make": "MERCEDES BENZ",
"importer_type": "PI",
"fuel_type": "Diesel",
"vehicle_type": "Sports Utility Vehicle",
"number": ""
},
{
"_id": "66e29f9807b823f9e46e52fb",
"month": "2024-08",
"make": "MERCEDES BENZ",
"importer_type": "PI",
"fuel_type": "Petrol",
"vehicle_type": "Sports Utility Vehicle",
"number": 3
},
{
"_id": "66e29f9807b823f9e46e52fa",
"month": "2024-08",
"make": "MERCEDES BENZ",
"importer_type": "AMD",
"fuel_type": "Electric",
"vehicle_type": "Sports Utility Vehicle",
"number": 39
},
{
"_id": "66e29f9807b823f9e46e52f9",
"month": "2024-08",
"make": "MERCEDES BENZ",
"importer_type": "AMD",
"fuel_type": "Petrol-Electric (Plug-In)",
"vehicle_type": "Sports Utility Vehicle",
"number": 1
},
{
"_id": "66e29f9807b823f9e46e52f8",
"month": "2024-08",
"make": "MERCEDES BENZ",
"importer_type": "AMD",
"fuel_type": "Petrol-Electric",
"vehicle_type": "Sports Utility Vehicle",
"number": 192
},
{
"_id": "66e29f9807b823f9e46e52f7",
"month": "2024-08",
"make": "MERCEDES BENZ",
"importer_type": "AMD",
"fuel_type": "Petrol",
"vehicle_type": "Sports Utility Vehicle",
"number": ""
}
]
7 changes: 3 additions & 4 deletions app/vehicle-type/[type]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ import {
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { ScrollArea, ScrollBar } from "@/components/ui/scroll-area";
import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { API_URL, EXCLUSION_LIST, SITE_TITLE, SITE_URL } from "@/config";
import { API_URL, SITE_TITLE, SITE_URL } from "@/config";
import { type Car, type LatestMonth, RevalidateTags } from "@/types";
import { capitaliseWords } from "@/utils/capitaliseWords";
import { fetchApi } from "@/utils/fetchApi";
import { mergeCarsByVehicleType } from "@/utils/mergeCarsByVehicleType";
import type { Metadata } from "next";
import type { Dataset, WithContext } from "schema-dts";

Expand Down Expand Up @@ -90,9 +91,7 @@ const CarsByVehicleTypePage = async ({ params, searchParams }: Props) => {
},
);

const filteredCars = cars.filter(
({ make, number }) => !EXCLUSION_LIST.includes(make) && number > 0,
);
const filteredCars = mergeCarsByVehicleType(cars);

const structuredData: WithContext<Dataset> = {
"@context": "https://schema.org",
Expand Down
37 changes: 37 additions & 0 deletions utils/mergeCarsByVehicleType.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import mockCars from "@/__mocks__/mock-cars.json";
import { mergeCarsByVehicleType } from "@/utils/mergeCarsByVehicleType";

describe("mergeCarsByVehicleType", () => {
it("should merge cars by make and vehicle type", () => {
const cars = mergeCarsByVehicleType(mockCars);
expect(cars).toEqual([
{
_id: "66e29f9807b823f9e46e52d0",
fuel_type: "Electric",
importer_type: "AMD",
make: "BYD",
month: "2024-08",
number: 363,
vehicle_type: "Sports Utility Vehicle",
},
{
_id: "66e29f9807b823f9e46e52cb",
fuel_type: "Electric",
importer_type: "AMD",
make: "BMW",
month: "2024-08",
number: 247,
vehicle_type: "Sports Utility Vehicle",
},
{
_id: "66e29f9807b823f9e46e52fd",
fuel_type: "Petrol-Electric",
importer_type: "PI",
make: "MERCEDES BENZ",
month: "2024-08",
number: 237,
vehicle_type: "Sports Utility Vehicle",
},
]);
});
});
19 changes: 19 additions & 0 deletions utils/mergeCarsByVehicleType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { Car } from "@/types";

export const mergeCarsByVehicleType = (cars: Car[]): Car[] => {
const filteredCars = cars.filter(({ number }) => number > 0);

const mergedData: Record<string, Car> = {};
filteredCars.forEach((car) => {
const { make, number, vehicle_type } = car;
const key = `${make}-${vehicle_type}`;

if (!mergedData[key]) {
mergedData[key] = { ...car, number: 0 };
}

mergedData[key].number += number;
});

return Object.values(mergedData).sort((a, b) => b.number - a.number);
};

0 comments on commit 64645d5

Please sign in to comment.