diff --git a/src/app/api/dealApi.ts b/src/app/api/dealApi.ts
index bdb5365..28204b5 100644
--- a/src/app/api/dealApi.ts
+++ b/src/app/api/dealApi.ts
@@ -1,5 +1,4 @@
import { createSupabaseClient } from "@/lib/supabase/clientComponentClient";
-import { getCurrentUserID } from "./userApi";
export type Deal = {
deal_amount: number;
@@ -7,9 +6,14 @@ export type Deal = {
investor_id: string;
};
-export async function getDealList() {
+export async function getDealList(userId: string | undefined) {
+ if (!userId) {
+ // console.error("No deal list of this user was found");
+ return []; // Exit on error
+ }
+
const supabase = createSupabaseClient();
- // get id of investor who invests in the business
+ // get id of investors who invest in the business
const { data: dealData, error: dealError } = await supabase
.from("business")
.select(
@@ -21,18 +25,18 @@ export async function getDealList() {
)
`
)
- .eq("user_id", await getCurrentUserID())
+ .eq("user_id", userId)
.single();
if (dealError) {
- alert(JSON.stringify(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);
@@ -53,16 +57,22 @@ export async function getDealList() {
if (sortedDealDataError) {
alert(JSON.stringify(sortedDealDataError));
console.error("Error sorting deal list:", sortedDealDataError);
- return; // Exit on error
+ return []; // Exit on error
}
+ // console.log(sortedDealData)
return sortedDealData;
}
// #TODO fix query to be non unique
-export async function getRecentDealData() {
+export async function getRecentDealData(userId: string | undefined) {
+ if (!userId) {
+ console.error("User not found");
+ return; // Exit on error
+ }
+
const supabase = createSupabaseClient();
- const dealList = await getDealList();
+ const dealList = await getDealList(userId);
if (!dealList) {
// #TODO div error
@@ -100,6 +110,8 @@ export async function getRecentDealData() {
const recentDealData = recentDealList.map((item, index) => {
return { ...item, ...recentUserData[index] };
});
+
+
return recentDealData;
}
@@ -121,8 +133,6 @@ export function convertToGraphData(deals: Deal[]): Record
{
// Create a sorted graph data object
const sortedGraphData: Record = {};
- sortedKeys.forEach((key) => {
- sortedGraphData[key] = graphData[key];
- });
+ sortedKeys.forEach((key) => {sortedGraphData[key] = graphData[key]});
return sortedGraphData;
}
diff --git a/src/app/dashboard/hook.ts b/src/app/dashboard/hook.ts
index e2a7ccf..676d249 100644
--- a/src/app/dashboard/hook.ts
+++ b/src/app/dashboard/hook.ts
@@ -1,13 +1,15 @@
import { useEffect, useState } from "react";
import { Deal, getDealList, convertToGraphData, getRecentDealData } from "../api/dealApi";
import { RecentDealData } from "@/components/recent-funds";
+import { getCurrentUserID } from "../api/userApi";
// custom hook for deal list
export function useDealList() {
- const [dealList, setDealList] = useState();
+ const [dealList, setDealList] = useState([]);
const fetchDealList = async () => {
- setDealList(await getDealList());
+ // set the state to the deal list of current business user
+ setDealList(await getDealList(await getCurrentUserID()));
}
useEffect(() => {
@@ -21,7 +23,8 @@ export function useGraphData() {
const [graphData, setGraphData] = useState({});
const fetchGraphData = async () => {
- const dealList = await getDealList();
+ // fetch the state to the deal list of current business user
+ const dealList = await getDealList(await getCurrentUserID());
if (dealList) {
setGraphData(convertToGraphData(dealList));
}
@@ -38,7 +41,8 @@ export function useRecentDealData() {
const [recentDealData, setRecentDealData] = useState();
const fetchRecentDealData = async () => {
- setRecentDealData(await getRecentDealData());
+ // set the state to the deal list of current business user
+ setRecentDealData(await getRecentDealData(await getCurrentUserID()));
}
useEffect(() => {
diff --git a/src/app/dashboard/page.tsx b/src/app/dashboard/page.tsx
index 072d95d..68a471b 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,17 +21,9 @@ 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 (
<>
- {/* {dealList?.map((deal, index) => (
-
-
Deal Amount: {deal.deal_amount}
-
Created Time: {new Date(deal.created_time).toUTCString()}
-
Investor ID: {deal.investor_id}
-
- ))} */}
- ${totalDealAmount}
+ ${sumByKey(dealList, "deal_amount")}
{/*
+20.1% from last month
*/}
diff --git a/src/app/project/apply/page.tsx b/src/app/project/apply/page.tsx
index b14b662..0509058 100644
--- a/src/app/project/apply/page.tsx
+++ b/src/app/project/apply/page.tsx
@@ -18,7 +18,7 @@ const BUCKET_PITCH_APPLICATION_NAME = "project-application";
export default function ApplyProject() {
const [isSuccess, setIsSuccess] = useState(true);
const onSubmit: SubmitHandler = async (data) => {
- alert("มาแน้ววว");
+ // alert("มาแน้ววว");
await sendApplication(data);
// console.table(data);
// console.log(typeof data["projectPhotos"], data["projectPhotos"]);
diff --git a/src/lib/data/projectQuery.ts b/src/lib/data/projectQuery.ts
index 568b5da..6e0ae76 100644
--- a/src/lib/data/projectQuery.ts
+++ b/src/lib/data/projectQuery.ts
@@ -6,28 +6,28 @@ async function getTopProjects(client: SupabaseClient, numberOfRecords: number =
.from("project")
.select(
`
- id,
- project_name,
- business_id,
- published_time,
- project_short_description,
- card_image_url,
- project_investment_detail (
- min_investment,
- total_investment,
- target_investment,
- investment_deadline
- ),
- project_tag (
- tag (
- id,
- value
- )
- ),
- business (
- location
+ id,
+ project_name,
+ business_id,
+ published_time,
+ project_short_description,
+ card_image_url,
+ project_investment_detail (
+ min_investment,
+ total_investment,
+ target_investment,
+ investment_deadline
+ ),
+ project_tag (
+ tag (
+ id,
+ value
)
- `
+ ),
+ business (
+ location
+ )
+ `
)
.order("published_time", { ascending: false })
.limit(numberOfRecords);
@@ -79,6 +79,7 @@ async function getProjectData(client: SupabaseClient, projectId: number) {
project_short_description,
project_description,
published_time,
+ card_image_url,
...project_investment_detail!inner (
min_investment,
total_investment,
@@ -89,6 +90,9 @@ async function getProjectData(client: SupabaseClient, projectId: number) {
...tag!inner (
tag_name:value
)
+ ),
+ ...business (
+ user_id
)
`
)
diff --git a/src/lib/utils.ts b/src/lib/utils.ts
index d084cca..812d222 100644
--- a/src/lib/utils.ts
+++ b/src/lib/utils.ts
@@ -4,3 +4,30 @@ 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);
+}
+
+export function toPercentage(part: number, total: number): number {
+ if (total === 0) return 0; // Prevent division by zero
+ return Number(((part * 100) / total).toFixed(2));
+}
\ No newline at end of file