Skip to content

Commit

Permalink
Refactor data tables
Browse files Browse the repository at this point in the history
  • Loading branch information
Ru Chern Chong committed Aug 11, 2024
1 parent 37758ab commit 9c5c4fd
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 62 deletions.
29 changes: 27 additions & 2 deletions app/cars/[type]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ import { API_URL, EXCLUSION_LIST, SITE_URL } from "@/config";
import { capitaliseWords } from "@/utils/capitaliseWords";
import { fetchApi } from "@/utils/fetchApi";
import { Car, LatestMonth } from "@/types";
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "@/components/ui/card";
import { formatDateToMonthYear } from "@/utils/formatDateToMonthYear";
import { ScrollArea } from "@/components/ui/scroll-area";

interface Props {
params: { type: string };
Expand Down Expand Up @@ -132,8 +141,24 @@ const CarsByFuelTypePage = async ({ params, searchParams }: Props) => {
})}
</TabsList>
</Tabs>
<CarTreeMap data={filteredCars} />
<DataTable data={filteredCars} fuelType={type} />
<div className="grid grid-cols-1 gap-4 lg:grid-cols-2">
<Card>
<CardHeader>
<CardTitle>Overview</CardTitle>
</CardHeader>
<CardContent>
<CarTreeMap data={filteredCars} />
</CardContent>
</Card>
<Card>
<CardHeader>
<CardTitle>Registrations</CardTitle>
</CardHeader>
<CardContent>
<DataTable data={filteredCars} fuelType={type} />
</CardContent>
</Card>
</div>
</div>
</section>
);
Expand Down
17 changes: 1 addition & 16 deletions app/components/DataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,6 @@ export const DataTable = ({ data, fuelType }: DataTableProps) => {

return (
<Table>
{selectedMonth && (
<TableCaption>
{capitaliseWords(fuelType)} cars registration in Singapore for{" "}
{formatDateToMonthYear(selectedMonth)}
</TableCaption>
)}
<TableHeader>
<TableRow>
{Object.values(CARS.TABLE.HEADERS).map((header) => (
Expand All @@ -64,8 +58,7 @@ export const DataTable = ({ data, fuelType }: DataTableProps) => {
)}
{data.length > 0 &&
data.map((item, index) => (
<TableRow key={item._id} className="even:bg-muted">
<TableCell>{MEDAL_MAPPING[index + 1] || index + 1}</TableCell>
<TableRow key={item._id}>
<TableCell>
<Link href={`/make/${item.make}`}>{item.make}</Link>
</TableCell>
Expand All @@ -82,14 +75,6 @@ export const DataTable = ({ data, fuelType }: DataTableProps) => {
</TableRow>
))}
</TableBody>
<TableFooter>
{data.length > 0 && (
<TableRow>
<TableCell colSpan={2}>Total</TableCell>
<TableCell colSpan={2}>{total}</TableCell>
</TableRow>
)}
</TableFooter>
</Table>
);
};
88 changes: 46 additions & 42 deletions app/make/[make]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type { Car, LatestMonth } from "@/types";
import { StructuredData } from "@/components/StructuredData";
import Typography from "@/components/Typography";
import { MAKE } from "@/constants";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";

interface Props {
params: { make: string };
Expand Down Expand Up @@ -91,49 +92,52 @@ const CarMakePage = async ({ params, searchParams }: Props) => {
</h1>
<p className="text-xl text-muted-foreground">Registrations</p>
</div>
<Table>
<TableCaption>
Historical trends for {decodeURIComponent(make)}
</TableCaption>
<TableHeader>
<TableRow>
{Object.values(MAKE.TABLE.HEADERS).map((header) => (
<TableHead key={header}>{header}</TableHead>
))}
</TableRow>
</TableHeader>
<TableBody>
{filteredCars.length === 0 && (
<TableRow>
<TableCell
colSpan={Object.entries(MAKE.TABLE.HEADERS).length}
className="text-center"
>
<Typography.H4>No data available</Typography.H4>
</TableCell>
</TableRow>
)}
{filteredCars.map((car, index) => (
<TableRow
key={`${car.fuel_type}-${car.vehicle_type}`}
className="even:bg-muted"
>
<TableCell>{index + 1}</TableCell>
<TableCell>{car.month}</TableCell>
<TableCell>
<Link
href={`/cars/${car.fuel_type.toLowerCase()}?month=${car.month}`}
className="hover:underline"
<Card>
<CardHeader>
<CardTitle>Trends</CardTitle>
</CardHeader>
<CardContent>
<Table>
<TableHeader>
<TableRow>
{Object.values(MAKE.TABLE.HEADERS).map((header) => (
<TableHead key={header}>{header}</TableHead>
))}
</TableRow>
</TableHeader>
<TableBody>
{filteredCars.length === 0 && (
<TableRow>
<TableCell
colSpan={Object.entries(MAKE.TABLE.HEADERS).length}
className="text-center"
>
<Typography.H4>No data available</Typography.H4>
</TableCell>
</TableRow>
)}
{filteredCars.map((car, index) => (
<TableRow
key={`${car.fuel_type}-${car.vehicle_type}`}
className="even:bg-muted"
>
{car.fuel_type}
</Link>
</TableCell>
<TableCell>{car.vehicle_type}</TableCell>
<TableCell>{car.number}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
<TableCell>{car.month}</TableCell>
<TableCell>
<Link
href={`/cars/${car.fuel_type.toLowerCase()}?month=${car.month}`}
className="hover:underline"
>
{car.fuel_type}
</Link>
</TableCell>
<TableCell>{car.vehicle_type}</TableCell>
<TableCell>{car.number}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</CardContent>
</Card>
</div>
</section>
);
Expand Down
2 changes: 0 additions & 2 deletions constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export const CARS = {
TABLE: {
HEADERS: {
SERIAL: "#",
MAKE: "Make",
COUNT: "Count",
MARKET_SHARE: "Market Share",
Expand All @@ -12,7 +11,6 @@ export const CARS = {
export const MAKE = {
TABLE: {
HEADERS: {
SERIAL: "#",
MONTH: "Month",
FUEL_TYPE: "Fuel Type",
VEHICLE_TYPE: "Vehicle Type",
Expand Down

0 comments on commit 9c5c4fd

Please sign in to comment.