refactor totalDealAmount function and change undefined deal value to empty array

This commit is contained in:
Naytitorn Chaovirachot 2024-11-01 18:07:55 +07:00
parent e0ddbbc50d
commit c15e451912
6 changed files with 37 additions and 12 deletions

View File

@ -28,9 +28,9 @@ export default async function ProjectDealPage({ params }: { params: { id: number
return <div>Error</div>;
}
// 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
<span>
{/* #TODO use sum() instead of storing total in database */}
<h1 className="font-semibold text-xl md:text-4xl mt-8">${projectData?.total_investment}</h1>
<p className="text-sm md:text-lg">5% raised of $5M max goal</p>
<p className="text-sm md:text-lg">
{projectData?.total_investment / projectData?.target_investment}%
raised of ${projectData?.target_investment} max goal
</p>
<Progress
value={projectData?.total_investment / projectData?.target_investment}
className="w-[60%] h-3 mt-3"

View File

@ -9,7 +9,7 @@ export type Deal = {
export async function getDealList(userId: string | undefined) {
if (!userId) {
// console.error("No deal list of this user was found");
return; // Exit on error
return []; // Exit on error
}
const supabase = createSupabaseClient();
@ -31,12 +31,12 @@ export async function getDealList(userId: string | undefined) {
if (dealError) {
// alert(JSON.stringify(dealError));
console.error("Error fetching deal list:", dealError);
return; // Exit on error
return []; // Exit on error
}
if (!dealData || !dealData.project.length) {
alert("No project available");
return; // Exit if there's no data
return []; // Exit if there's no data
}
const investorIdList = dealData.project[0].investment_deal.map((deal) => 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;
}

View File

@ -5,7 +5,7 @@ import { getCurrentUserID } from "../api/userApi";
// custom hook for deal list
export function useDealList() {
const [dealList, setDealList] = useState<Deal[]>();
const [dealList, setDealList] = useState<Deal[]>([]);
const fetchDealList = async () => {
// set the state to the deal list of current business user

View File

@ -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() {
</svg>
</CardHeader>
<CardContent>
<div className="text-2xl font-bold">${totalDealAmount}</div>
<div className="text-2xl font-bold">${sumByKey(dealList, "deal_amount")}</div>
{/* <p className="text-xs text-muted-foreground">
+20.1% from last month
</p> */}

View File

@ -90,7 +90,7 @@ async function getProjectData(client: SupabaseClient, projectId: number) {
tag_name:value
)
),
business (
...business (
user_id
)
`

View File

@ -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);
}