diff --git a/src/app/api/dealApi.ts b/src/app/api/dealApi.ts index 31b2094..5c7c948 100644 --- a/src/app/api/dealApi.ts +++ b/src/app/api/dealApi.ts @@ -25,20 +25,45 @@ export async function getDealList() { .eq('user_id', await getCurrentUserID()) .single(); - if (error || !dealData) { - alert(JSON.stringify(error)); - console.error('Error fetching deal list:', error); - } else { - const dealList = dealData.project[0].investment_deal; + // Handle errors and no data cases + if (error) { + alert(JSON.stringify(error)); + console.error('Error fetching deal list:', error); + return; // Exit on error + } - if (!dealList.length) { - alert("No data available"); - return; // Exit early if there's no data - } + if (!dealData || !dealData.project.length) { + alert("No project available"); + return; // Exit if there's no data + } - // Sort the dealList by created_time in descending order - const byCreatedTimeDesc = (a: Deal, b: Deal) => - new Date(b.created_time).getTime() - new Date(a.created_time).getTime(); - return dealList.sort(byCreatedTimeDesc); - } -}; \ No newline at end of file + const dealList = dealData.project[0].investment_deal; + + // Check for empty dealList + if (!dealList.length) { + alert("No deal list available"); + return; // Exit if there's no data + } + + // Sort the dealList by created_time in descending order + const byCreatedTimeDesc = (a: Deal, b: Deal) => + new Date(b.created_time).getTime() - new Date(a.created_time).getTime(); + return dealList.sort(byCreatedTimeDesc); +}; + +// #TODO move to util +export function convertToGraphData(deals: Deal[]): Record { + let graphData = deals.reduce((acc, deal) => { + const monthYear = new Date(deal.created_time).toISOString().slice(0, 7); // E.g., '2024-10' + acc[monthYear] = (acc[monthYear] || 0) + deal.deal_amount; // Sum the deal_amount + return acc; + }, {} as Record); // Change type to Record + + // Sort keys in ascending order + const sortedKeys = Object.keys(graphData).sort((a, b) => (a > b ? 1 : -1)); + + // Create a sorted graph data object + const sortedGraphData: Record = {}; + sortedKeys.forEach((key) => {sortedGraphData[key] = graphData[key]}); + return sortedGraphData; +} \ No newline at end of file diff --git a/src/app/dashboard/hook.ts b/src/app/dashboard/hook.ts index a220dc0..70166fc 100644 --- a/src/app/dashboard/hook.ts +++ b/src/app/dashboard/hook.ts @@ -1,5 +1,5 @@ import { useEffect, useState } from "react"; -import { Deal, getDealList } from "../api/dealApi"; +import { Deal, getDealList, convertToGraphData } from "../api/dealApi"; // custom hook for deal list export function useDealList() { @@ -14,4 +14,21 @@ export function useDealList() { }, []); return dealList; +} + +export function useGraphData() { + const [graphData, setGraphData] = useState>({}); + + const fetchGraphData = async () => { + const dealList = await getDealList(); + if (dealList) { + setGraphData(convertToGraphData(dealList)); + } + } + + useEffect(() => { + fetchGraphData(); + }, []); + + return graphData; } \ No newline at end of file diff --git a/src/app/dashboard/page.tsx b/src/app/dashboard/page.tsx index 3d2840f..5d7cd2e 100644 --- a/src/app/dashboard/page.tsx +++ b/src/app/dashboard/page.tsx @@ -12,10 +12,11 @@ import { Overview } from "@/components/ui/overview"; import { RecentFunds } from "@/components/recent-funds"; import { useState } from "react"; -import { useDealList } from "./hook"; +import { useDealList, useGraphData } from "./hook"; export default function Dashboard() { const [graphType, setGraphType] = useState("line"); + const graphData = useGraphData(); const dealList = useDealList(); const totalDealAmount = dealList?.reduce((sum, deal) => sum + deal.deal_amount, 0) || 0; @@ -166,7 +167,7 @@ export default function Dashboard() { Overview - + {/* tab to switch between line and bar graph */} ; // Object with month-year as keys and sum as value } export function Overview(props: OverViewProps) { + // Transform the grouped data into the format for the chart + const chartData = Object.entries(props.graphData).map(([monthYear, totalArray]) => ({ + name: monthYear, + total: totalArray, // Get the total amount for the month + })); + return ( {props.graphType === 'line' ? ( - + ) : ( - + )} - ); + ); }