Refactor generalApi.ts and portfolio/[uid]/page.tsx

This commit is contained in:
Pattadon 2024-10-30 12:07:59 +07:00
parent f4a1c287c1
commit 209d6f1f73
3 changed files with 40 additions and 66 deletions

View File

@ -34,23 +34,6 @@ async function clearFolder(
return errors; return errors;
} }
export async function getProjectTag(projectId: number) {
return supabase
.from("project_tag")
.select("tag_id")
.in("item_id", [projectId]);
}
export async function getTagName(tagId: number) {
return supabase.from("tag").select("value").in("id", [tagId]);
}
export async function getInvestorDeal(userId: string) {
return supabase
.from("investment_deal")
.select("*")
.in("investor_id", [userId])
.order("created_time", { ascending: true });
}
async function uploadToFolder( async function uploadToFolder(
bucketName: string, bucketName: string,
filePath: string, filePath: string,

View File

@ -1,22 +1,19 @@
"use client";
import { Overview } from "@/components/ui/overview"; import { Overview } from "@/components/ui/overview";
import { import { createSupabaseClient } from "@/lib/supabase/serverComponentClient";
getInvestorDeal, import { getInvestorDeal, getProjectTag, getTagName } from "@/lib/data/query";
getProjectTag,
getTagName,
} from "@/app/api/generalApi";
import { useQuery } from "@supabase-cache-helpers/postgrest-react-query";
import PieChart from "@/components/pieChart"; import PieChart from "@/components/pieChart";
export default function Portfolio({ export default async function Portfolio({
params, params,
}: { }: {
params: { uid: string }; params: { uid: string };
}) { }) {
const supabase = createSupabaseClient();
const daysOfWeek = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; const daysOfWeek = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
const dayOfWeekData = daysOfWeek.map((day) => ({ name: day, value: 0 })); const dayOfWeekData = daysOfWeek.map((day) => ({ name: day, value: 0 }));
const { data: deals, error: investorDealError } = useQuery( const { data: deals, error: investorDealError } = await getInvestorDeal(
getInvestorDeal(params.uid) supabase,
params.uid
); );
const projectTag = async () => { const projectTag = async () => {
@ -28,7 +25,7 @@ export default function Portfolio({
await Promise.all( await Promise.all(
uniqueProjectIds.map(async (projectId: number) => { uniqueProjectIds.map(async (projectId: number) => {
const { data: tagIdsArray, error: tagError } = const { data: tagIdsArray, error: tagError } =
await getProjectTag(projectId); await getProjectTag(supabase, projectId);
if (tagError) { if (tagError) {
console.error(tagError); console.error(tagError);
return []; return [];
@ -43,7 +40,7 @@ export default function Portfolio({
tagIds tagIds
.filter((tagId) => tagId !== null) .filter((tagId) => tagId !== null)
.map(async (id: number) => { .map(async (id: number) => {
const { data: tagName, error: nameError } = await getTagName(id); const { data: tagName, error: nameError } = await getTagName(supabase, id);
if (nameError) { if (nameError) {
console.error(nameError); console.error(nameError);
return null; return null;
@ -59,15 +56,16 @@ export default function Portfolio({
const tagName = tag.value; const tagName = tag.value;
acc[tagName] = (acc[tagName] || 0) + 1; acc[tagName] = (acc[tagName] || 0) + 1;
return acc; return acc;
}, {}); }, {} as Record<string, number>);
return Object.entries(tagCounts).map(([name, count]) => ({ return Object.entries(tagCounts).map(([name, count]) => ({
name, name,
count: count as number, count: count as number,
})); }));
}; };
const {data:tags, error: projectTagError, isLoading: projectTagLoading} = useQuery(projectTag()); const tags = await projectTag();
const tagCount = countTags(tags); console.log(tags)
// const tagCount = countTags(tags);
if (investorDealError) { if (investorDealError) {
console.error(investorDealError); console.error(investorDealError);
@ -150,7 +148,7 @@ export default function Portfolio({
return ( return (
<div> <div>
{/* {JSON.stringify(params.uid)} */} {/* {JSON.stringify(params.uid)} */}
{JSON.stringify(tagCount)} {/* {JSON.stringify(tagCount)} */}
{/* {JSON.stringify(deals)} */} {/* {JSON.stringify(deals)} */}
{/* {JSON.stringify(dayOfWeekData)} */} {/* {JSON.stringify(dayOfWeekData)} */}
{/* {JSON.stringify(overAllGraphData)} */} {/* {JSON.stringify(overAllGraphData)} */}
@ -165,13 +163,6 @@ export default function Portfolio({
<Overview graphType="bar" data={threeYearGraphData}></Overview> <Overview graphType="bar" data={threeYearGraphData}></Overview>
<Overview graphType="bar" data={dayOfWeekData}></Overview> <Overview graphType="bar" data={dayOfWeekData}></Overview>
</div> </div>
<PieChart
labels={tagCount.map((item: { name: string }) => {
return item.name;
})}
data={tagCount.map((item: { count: number }) => item.count)}
header="Ratio of Investment's project category"
/>
</div> </div>
); );
} }

View File

@ -44,28 +44,28 @@ function getInvestmentCounts(client: SupabaseClient, projectIds: string[]) {
.in("project_id", projectIds); .in("project_id", projectIds);
} }
// function getInvestorDeal(client: SupabaseClient, userId: string) { function getInvestorDeal(client: SupabaseClient, userId: string) {
// return client return client
// .from("investment_deal") .from("investment_deal")
// .select("*") .select("*")
// .in("investor_id", [userId]) .in("investor_id", [userId])
// .order("created_time", { ascending: true }); .order("created_time", { ascending: true });
// } }
// function getProjectTag(client: SupabaseClient, projectId: number) { function getProjectTag(client: SupabaseClient, projectId: number) {
// return client.from("project_tag").select("tag_id").in("item_id", [projectId]); return client.from("project_tag").select("tag_id").in("item_id", [projectId]);
// } }
// function getTagName(client: SupabaseClient, tagId: number){ function getTagName(client: SupabaseClient, tagId: number){
// return client.from("tag").select("value").in("id", [tagId]); return client.from("project_tag").select("tag_id").in("item_id", [tagId]);
// } }
export { export {
getBusinesses, getBusinesses,
getInvestmentCounts, getInvestmentCounts,
getProjects, getProjects,
getTags, getTags,
// getInvestorDeal, getInvestorDeal,
// getProjectTag, getProjectTag,
// getTagName, getTagName
}; };