diff --git a/src/app/(investment)/deals/[id]/page.tsx b/src/app/(investment)/deals/[id]/page.tsx index 8eac7cb..9001daa 100644 --- a/src/app/(investment)/deals/[id]/page.tsx +++ b/src/app/(investment)/deals/[id]/page.tsx @@ -28,9 +28,9 @@ export default async function ProjectDealPage({ params }: { params: { id: number return
Error
; } - // console.log(projectData); + console.log(projectData); - const projectBusinessOwnerId = projectData.business.user_id; + const projectBusinessOwnerId = projectData.user_id; // console.log(projectBusinessOwnerId); const dealData = await getDealList(projectBusinessOwnerId); // console.log(dealData); @@ -96,7 +96,10 @@ export default async function ProjectDealPage({ params }: { params: { id: number {/* #TODO use sum() instead of storing total in database */}

${projectData?.total_investment}

-

5% raised of $5M max goal

+

+ {projectData?.total_investment / projectData?.target_investment}% + raised of ${projectData?.target_investment} max goal +

deal.investor_id); @@ -57,10 +57,10 @@ export async function getDealList(userId: string | undefined) { if (sortedDealDataError) { alert(JSON.stringify(sortedDealDataError)); console.error("Error sorting deal list:", sortedDealDataError); - return; // Exit on error + return []; // Exit on error } - console.log(sortedDealData) + // console.log(sortedDealData) return sortedDealData; } diff --git a/src/app/dashboard/hook.ts b/src/app/dashboard/hook.ts index 236c4bc..676d249 100644 --- a/src/app/dashboard/hook.ts +++ b/src/app/dashboard/hook.ts @@ -5,7 +5,7 @@ import { getCurrentUserID } from "../api/userApi"; // custom hook for deal list export function useDealList() { - const [dealList, setDealList] = useState(); + const [dealList, setDealList] = useState([]); const fetchDealList = async () => { // set the state to the deal list of current business user diff --git a/src/app/dashboard/page.tsx b/src/app/dashboard/page.tsx index 072d95d..5216a19 100644 --- a/src/app/dashboard/page.tsx +++ b/src/app/dashboard/page.tsx @@ -13,6 +13,7 @@ import { RecentFunds } from "@/components/recent-funds"; import { useState } from "react"; import { useDealList, useGraphData, useRecentDealData } from "./hook"; +import { sumByKey } from "@/lib/utils"; export default function Dashboard() { const [graphType, setGraphType] = useState("line"); @@ -20,7 +21,6 @@ export default function Dashboard() { const dealList = useDealList(); // #TODO dependency injection refactor + define default value inside function (and not here) const recentDealData = useRecentDealData() || []; - const totalDealAmount = dealList?.reduce((sum, deal) => sum + deal.deal_amount, 0) || 0; return ( <> @@ -78,7 +78,7 @@ export default function Dashboard() { -
${totalDealAmount}
+
${sumByKey(dealList, "deal_amount")}
{/*

+20.1% from last month

*/} diff --git a/src/lib/data/projectQuery.ts b/src/lib/data/projectQuery.ts index ecdf23e..c1a7217 100644 --- a/src/lib/data/projectQuery.ts +++ b/src/lib/data/projectQuery.ts @@ -90,7 +90,7 @@ async function getProjectData(client: SupabaseClient, projectId: number) { tag_name:value ) ), - business ( + ...business ( user_id ) ` diff --git a/src/lib/utils.ts b/src/lib/utils.ts index d084cca..a6761ea 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -4,3 +4,25 @@ import { twMerge } from "tailwind-merge" export function cn(...inputs: ClassValue[]) { return twMerge(clsx(inputs)) } + +export function sum(list: any[]) { + if (!list || list.length === 0) { + return 0; + } + + return list.reduce((total, num) => total + num, 0); +} + +export function sumByKey(list: any[], key: string) { + // example usage + // const items = [ + // { amount: 10 }, + // { amount: 20 }, + // { amount: 30 }, + // { amount: 40 } + // ]; + + // const totalAmount = sumByKey(items, 'amount'); + // console.log(totalAmount); // Output: 100 + return list.reduce((total, obj) => total + (obj[key] || 0), 0); +} \ No newline at end of file