Refactor date filtering and data aggregation in Portfolio component

This commit is contained in:
Pattadon 2024-10-29 13:03:45 +07:00
parent 42b47aa4e8
commit 44a389c488
3 changed files with 77 additions and 14 deletions

View File

@ -12,24 +12,82 @@ export default async function Portfolio({
if (error) {
console.error(error);
}
const getMonthName = (dateString: string) => {
const date = new Date(dateString);
return date.toLocaleString("default", { month: "long" });
const yearAgo = (num: number) => {
const newDate = new Date();
newDate.setFullYear(newDate.getFullYear() - num);
return newDate;
};
const graphData = deals
? deals.map((item) => ({
// convert month's index to string
name: getMonthName(item.created_time),
value: item.deal_amount as number,
}))
const getMonthName = (dateString: string) => {
const date = new Date(dateString);
return date.toLocaleString("default", { month: "long", year: "numeric" });
};
const overAllGraphData = deals
? deals
.filter((item) => new Date(item.created_time) >= yearAgo(1))
.reduce(
(acc, item) => {
const monthName = getMonthName(item.created_time).slice(0, 3);
const existingMonth = acc.find(
(entry: { name: string }) => entry.name === monthName
);
if (existingMonth) {
existingMonth.value += item.deal_amount;
} else {
acc.push({ name: monthName, value: item.deal_amount });
}
return acc;
},
[] as { name: string; value: number }[]
)
: [];
const threeYearGraphData = deals
? deals
.filter((item) => new Date(item.created_time) >= yearAgo(3))
.reduce(
(acc, item) => {
const year = new Date(item.created_time).getFullYear();
const existingYear = acc.find(
(entry: { name: string; }) => entry.name === year.toString()
);
if (existingYear) {
existingYear.value += item.deal_amount;
} else {
acc.push({ name: year.toString(), value: item.deal_amount })
}
return acc;
},
[] as { name: string; value: number }[]
)
: [];
// const graphData = [
// { name: "October", value: 500 },
// { name: "October", value: 500 },
// { name: "November", value: 500 },
// { name: "December", value: 500 },
// { name: "January", value: 500 },
// { name: "Febuary", value: 500 },
// { name: "March", value: 500 },
// ];
return (
<div>
{/* {JSON.stringify(deals)} */}
{JSON.stringify(graphData)}
<Overview graphType="line" data={graphData}></Overview>
{/* {JSON.stringify(deals)} */}
{/* {JSON.stringify(threeYearGraphData)} */}
<div className="flex w-full gap-10">
<Overview graphType="line" data={overAllGraphData}></Overview>
<Overview graphType="bar" data={threeYearGraphData}></Overview>
</div>
</div>
);
}

View File

@ -78,7 +78,7 @@ export function Overview(props: OverViewProps) {
tickFormatter={(value) => `$${value}`}
/>
<Line
dataKey="total"
dataKey="value"
fill="currentColor"
className="fill-primary"
/>
@ -99,7 +99,7 @@ export function Overview(props: OverViewProps) {
axisLine={false}
tickFormatter={(value) => `$${value}`}
/>
<Bar dataKey="total" fill="currentColor" className="fill-primary" />
<Bar dataKey="value" fill="currentColor" className="fill-primary" />
</BarChart>
)}
</ResponsiveContainer>

View File

@ -45,9 +45,14 @@ function getInvestmentCounts(client: SupabaseClient, projectIds: string[]) {
}
function getInvestorDeal(client: SupabaseClient, userId: string) {
return client.from("investment_deal").select("*").in("investor_id", [userId]);
return client
.from("investment_deal")
.select("*")
.in("investor_id", [userId])
.order("created_time", { ascending: true });
}
export {
getBusinesses,
getInvestmentCounts,