Refactor projectTag function and add PieChart component

This commit is contained in:
THIS ONE IS A LITTLE BIT TRICKY KRUB 2024-10-30 14:30:00 +07:00
parent 209d6f1f73
commit 62e20c282a
3 changed files with 40 additions and 19 deletions

View File

@ -17,6 +17,7 @@ export default async function Portfolio({
); );
const projectTag = async () => { const projectTag = async () => {
// get unique project id from deals
const uniqueProjectIds = Array.from( const uniqueProjectIds = Array.from(
new Set(deals?.map((deal) => deal.project_id)) new Set(deals?.map((deal) => deal.project_id))
); );
@ -24,8 +25,10 @@ export default async function Portfolio({
const tagIds = ( const tagIds = (
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(
await getProjectTag(supabase, projectId); supabase,
projectId
);
if (tagError) { if (tagError) {
console.error(tagError); console.error(tagError);
return []; return [];
@ -35,12 +38,15 @@ export default async function Portfolio({
) )
).flat(); ).flat();
// console.log(tagIds); // console.log(tagIds, uniqueProjectIds);
const tagNames = await Promise.all( const tagNames = await Promise.all(
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(supabase, id); const { data: tagName, error: nameError } = await getTagName(
supabase,
id
);
if (nameError) { if (nameError) {
console.error(nameError); console.error(nameError);
return null; return null;
@ -48,24 +54,28 @@ export default async function Portfolio({
return tagName; return tagName;
}) })
); );
// console.log(tagNames);
return tagNames.filter((tagName) => tagName !== null); return tagNames.filter((tagName) => tagName !== null);
}; };
const countTags = (tags: any[]) => { const countTags = (tags: any[]) => {
const tagCounts = tags.flat().reduce((acc, tag) => { const tagCounts = tags.flat().reduce(
(acc, tag) => {
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>); },
{} as Record<string, number>
return Object.entries(tagCounts).map(([name, count]) => ({ );
name,
count: count as number, return Object.entries(tagCounts).map(([name, count]) => ({
})); name,
}; count: count as number,
}));
};
const tags = await projectTag(); const tags = await projectTag();
console.log(tags) // console.log(tags);
// const tagCount = countTags(tags); const tagCount = countTags(tags);
// console.log(tagCount);
if (investorDealError) { if (investorDealError) {
console.error(investorDealError); console.error(investorDealError);
@ -163,6 +173,15 @@ export default async 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
data={tagCount.map(
(item: { name: string; count: number }) => item.count
)}
labels={tagCount.map(
(item: { name: string; count: number }) => item.name
)}
header="Total"
/>
</div> </div>
); );
} }

View File

@ -1,3 +1,5 @@
"use client";
import { Pie } from "react-chartjs-2"; import { Pie } from "react-chartjs-2";
import { Chart as ChartJS, ArcElement, Tooltip, Legend } from "chart.js"; import { Chart as ChartJS, ArcElement, Tooltip, Legend } from "chart.js";
@ -6,7 +8,7 @@ ChartJS.register(ArcElement, Tooltip, Legend);
interface PieChartProps { interface PieChartProps {
labels: string[]; labels: string[];
data: number[]; data: number[];
header:string header: string;
} }
const PieChart = (props: PieChartProps) => { const PieChart = (props: PieChartProps) => {

View File

@ -56,8 +56,8 @@ 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("project_tag").select("tag_id").in("item_id", [tagId]); return client.from("tag").select("value").in("id", [tagId]);
} }
export { export {
@ -67,5 +67,5 @@ export {
getTags, getTags,
getInvestorDeal, getInvestorDeal,
getProjectTag, getProjectTag,
getTagName getTagName,
}; };