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) { if (error) {
console.error(error); console.error(error);
} }
const getMonthName = (dateString: string) => { const yearAgo = (num: number) => {
const date = new Date(dateString); const newDate = new Date();
return date.toLocaleString("default", { month: "long" }); newDate.setFullYear(newDate.getFullYear() - num);
return newDate;
}; };
const graphData = deals const getMonthName = (dateString: string) => {
? deals.map((item) => ({ const date = new Date(dateString);
// convert month's index to string return date.toLocaleString("default", { month: "long", year: "numeric" });
name: getMonthName(item.created_time), };
value: item.deal_amount as number,
})) 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 ( return (
<div> <div>
{/* {JSON.stringify(deals)} */} {/* {JSON.stringify(deals)} */}
{JSON.stringify(graphData)} {/* {JSON.stringify(deals)} */}
<Overview graphType="line" data={graphData}></Overview> {/* {JSON.stringify(threeYearGraphData)} */}
<div className="flex w-full gap-10">
<Overview graphType="line" data={overAllGraphData}></Overview>
<Overview graphType="bar" data={threeYearGraphData}></Overview>
</div>
</div> </div>
); );
} }

View File

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

View File

@ -45,9 +45,14 @@ function getInvestmentCounts(client: SupabaseClient, projectIds: string[]) {
} }
function getInvestorDeal(client: SupabaseClient, userId: 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 { export {
getBusinesses, getBusinesses,
getInvestmentCounts, getInvestmentCounts,