diff --git a/app/coe/(prices)/page.tsx b/app/coe/(prices)/page.tsx index 941f151..6ed8fc6 100644 --- a/app/coe/(prices)/page.tsx +++ b/app/coe/(prices)/page.tsx @@ -11,7 +11,12 @@ import { CardTitle, } from "@/components/ui/card"; import { API_URL, SITE_URL } from "@/config"; -import { type COEBiddingResult, type COEResult, RevalidateTags } from "@/types"; +import { + type COEBiddingResult, + type COEResult, + type Month, + RevalidateTags, +} from "@/types"; import { fetchApi } from "@/utils/fetchApi"; import type { Metadata } from "next"; import type { WebPage, WithContext } from "schema-dts"; @@ -30,16 +35,23 @@ export const generateMetadata = async (): Promise => { }; }; -const COEPricesPage = async () => { - const params = new URLSearchParams(); +const COEPricesPage = async ({ searchParams }) => { + const params = new URLSearchParams(searchParams); params.append("sort", "month"); params.append("orderBy", "asc"); const queryString = params.toString(); - const coeResults = await fetchApi( + const getCoeResults = await fetchApi( `${API_URL}/coe?${queryString}`, - { next: { tags: [RevalidateTags.COE] } }, + { + next: { tags: [RevalidateTags.COE] }, + }, ); + const getMonths = await fetchApi(`${API_URL}/coe/months`); + const [coeResults, months] = (await Promise.all([ + getCoeResults, + getMonths, + ])) as [COEResult[], Month[]]; const groupedData = coeResults.reduce( (acc: any, item) => { @@ -79,7 +91,7 @@ const COEPricesPage = async () => { COE Results
- +
diff --git a/components/COEPremiumChart.tsx b/components/COEPremiumChart.tsx index ede2d04..983c443 100644 --- a/components/COEPremiumChart.tsx +++ b/components/COEPremiumChart.tsx @@ -1,10 +1,10 @@ "use client"; -import { type CSSProperties, useMemo, useState } from "react"; +import { type CSSProperties, useEffect, useMemo, useState } from "react"; +import { usePathname, useRouter } from "next/navigation"; import { numberFormat } from "@ruchernchong/number-format"; import { CartesianGrid, Label, Line, LineChart, XAxis, YAxis } from "recharts"; import useStore from "@/app/store"; -import { UnreleasedFeature } from "@/components/UnreleasedFeature"; import { Card, CardContent, @@ -27,30 +27,52 @@ import { SelectValue, } from "@/components/ui/select"; import { formatDateToMonthYear } from "@/utils/formatDateToMonthYear"; -import type { COEBiddingResult, COECategory } from "@/types"; +import type { COEBiddingResult, COECategory, Month } from "@/types"; interface Props { data: COEBiddingResult[]; + months: Month[]; } -export const COEPremiumChart = ({ data }: Props) => { +export const COEPremiumChart = ({ data, months }: Props) => { + const router = useRouter(); + const pathname = usePathname(); + const categories = useStore(({ categories }) => categories); const [timeRange, setTimeRange] = useState("360d"); + useEffect(() => { + const params = new URLSearchParams(); + + switch (timeRange) { + case "ALL": + params.append("from", months[months.length - 1]); + params.append("to", months[0]); + break; + case "YTD": + params.append("from", `${new Date().getFullYear()}-01`); + params.append("to", months[0]); + break; + default: + } + + router.push(`${pathname}?${params.toString()}`); + }, [months, pathname, router, timeRange]); + const filteredData: COEBiddingResult[] = useMemo( () => data - .filter((item) => { - const date = new Date(item.month); - const now = new Date(); - let daysToSubtract = 360; - if (timeRange === "1800d") { - daysToSubtract = 1800; - } - now.setDate(now.getDate() - daysToSubtract); - return date >= now; - }) + // .filter((item) => { + // const date = new Date(item.month); + // const now = new Date(); + // let daysToSubtract = 360; + // if (timeRange === "1800d") { + // daysToSubtract = 1800; + // } + // now.setDate(now.getDate() - daysToSubtract); + // return date >= now; + // }) .map((item) => Object.entries(item).reduce((acc: any, [key, value]) => { if ( @@ -68,6 +90,12 @@ export const COEPremiumChart = ({ data }: Props) => { const chartConfig = {} satisfies ChartConfig; + const TIME_RANGES = [ + { timeRange: "360d", label: "Last 12 Months" }, + { timeRange: "YTD", label: "Year to Date" }, + { timeRange: "ALL", label: "All Time" }, + ]; + return ( @@ -77,21 +105,22 @@ export const COEPremiumChart = ({ data }: Props) => { Showing the last 12 months of historical trends
- - + + + + + {TIME_RANGES.map(({ timeRange, label }) => ( + + {label} - - - + ))} + +