From 4346660a35fb2d139c811b843a775ee3537cf47e Mon Sep 17 00:00:00 2001 From: Khalim Conn-Kowlessar Date: Mon, 15 Apr 2024 19:54:41 +0100 Subject: [PATCH] Fixing typescript errors --- .../portfolio/summary/EpcBarChart.tsx | 41 +++++ .../summary/SelectComparisonModal.tsx | 99 ++++++++++++ .../portfolio/summary/SummaryTable.tsx | 146 ++++++++++++++++++ .../[slug]/(portfolio)/summary/page.tsx | 39 +++++ 4 files changed, 325 insertions(+) create mode 100644 src/app/components/portfolio/summary/EpcBarChart.tsx create mode 100644 src/app/components/portfolio/summary/SelectComparisonModal.tsx create mode 100644 src/app/components/portfolio/summary/SummaryTable.tsx create mode 100644 src/app/portfolio/[slug]/(portfolio)/summary/page.tsx diff --git a/src/app/components/portfolio/summary/EpcBarChart.tsx b/src/app/components/portfolio/summary/EpcBarChart.tsx new file mode 100644 index 0000000..f111afa --- /dev/null +++ b/src/app/components/portfolio/summary/EpcBarChart.tsx @@ -0,0 +1,41 @@ +import { BarChart } from "@tremor/react"; + +const dataFormatter = (number: number) => + Intl.NumberFormat("us").format(number).toString(); + +const EpcBarChart = ({ + chartdata, +}: { + chartdata: { + name: string; + A?: number; + B?: number; + C?: number; + D?: number; + E?: number; + F?: number; + G?: number; + }[]; +}) => ( + +); + +export default EpcBarChart; diff --git a/src/app/components/portfolio/summary/SelectComparisonModal.tsx b/src/app/components/portfolio/summary/SelectComparisonModal.tsx new file mode 100644 index 0000000..dbad4e4 --- /dev/null +++ b/src/app/components/portfolio/summary/SelectComparisonModal.tsx @@ -0,0 +1,99 @@ +import { Dialog, Transition } from "@headlessui/react"; +import { Fragment, useState } from "react"; + +const SelectComparisonModal = ({ + isOpen, + setIsOpen, + userPortfolios, + onAddColumn, +}: { + isOpen: boolean; + setIsOpen: (isOpen: boolean) => void; + userPortfolios: { name: string; id: bigint }[]; + onAddColumn: (columnName: string) => void; +}) => { + const [selectedPortfolio, setSelectedPortfolio] = useState(""); + + const addColumn = () => { + onAddColumn(selectedPortfolio); + setIsOpen(false); + }; + + return ( + + +
+ + + + + {/* This element is to trick the browser into centering the modal contents. */} + + +
+ + Add Comparison + +
+ +
+ +
+ +
+
+
+
+
+
+ ); +}; + +export default SelectComparisonModal; diff --git a/src/app/components/portfolio/summary/SummaryTable.tsx b/src/app/components/portfolio/summary/SummaryTable.tsx new file mode 100644 index 0000000..eb3204e --- /dev/null +++ b/src/app/components/portfolio/summary/SummaryTable.tsx @@ -0,0 +1,146 @@ +"use client"; + +import React, { useState } from "react"; +import { + useReactTable, + flexRender, + getCoreRowModel, + ColumnDef, + CellContext, +} from "@tanstack/react-table"; +import { + Table, + TableBody, + TableCell, + TableHead, + TableHeader, + TableRow, +} from "@/app/shadcn_components/ui/table"; +import SelectComparisonModal from "./SelectComparisonModal"; +import EpcBarChart from "./EpcBarChart"; + +interface DataItem { + title: string; + today: string; + summary: string; +} + +const SummaryTable = ({ + portfolioName, + data, + userPortfolios, +}: { + portfolioName: string; + data: DataItem[]; + userPortfolios: { name: string; id: bigint }[]; +}) => { + const initialchartdata = [ + { name: "G", G: 2488 }, + { name: "F", F: 1445 }, + { name: "E", E: 743 }, + { name: "D", D: 281 }, + { name: "C", C: 251 }, + { name: "B", B: 232 }, + { name: "A", A: 98 }, + ]; + + // Initial columns + // Initial columns + const [columns, setColumns] = useState[]>([ + { + accessorKey: "title", + header: () => null, + cell: (info) => {info.getValue() as string}, + }, + { + accessorKey: "today", + header: () => Today, + cell: (info) => { + // Check if the title is "EPCs" and render the bar chart + if (info.row.original.title === "EPCs") { + return ; + } + // Otherwise, just return the text + return {info.getValue() as string}; + }, + }, + { + accessorKey: "summary", + header: () => {portfolioName}, + cell: (info) => { + return {info.getValue() as string}; + }, + }, + ]); + + const [isModalOpen, setIsModalOpen] = useState(false); + + const table = useReactTable({ + data, + columns, + getCoreRowModel: getCoreRowModel(), + }); + + const addColumn = (columnName: string) => { + const newColumn: ColumnDef = { + accessorKey: columnName.toLowerCase().replace(/\s+/g, "_"), + header: () => {columnName}, + cell: ({ getValue }: CellContext) => { + const value = getValue(); + return ( + {typeof value === "string" ? value : "Invalid data"} + ); + }, + }; + setColumns((oldColumns) => [...oldColumns, newColumn]); + }; + + return ( +
+ + + +
+ + + {table.getHeaderGroups().map((headerGroup) => ( + + {headerGroup.headers.map((header) => ( + + {flexRender( + header.column.columnDef.header, + header.getContext() + )} + + ))} + + ))} + + + {table.getRowModel().rows.map((row) => ( + + {row.getVisibleCells().map((cell) => ( + + {flexRender(cell.column.columnDef.cell, cell.getContext())} + + ))} + + ))} + +
+
+
+ ); +}; + +export default SummaryTable; diff --git a/src/app/portfolio/[slug]/(portfolio)/summary/page.tsx b/src/app/portfolio/[slug]/(portfolio)/summary/page.tsx new file mode 100644 index 0000000..a1bd86a --- /dev/null +++ b/src/app/portfolio/[slug]/(portfolio)/summary/page.tsx @@ -0,0 +1,39 @@ +import SummaryTable from "@/app/components/portfolio/summary/SummaryTable"; +import { getPortfolio, getUserPortfolios } from "../../utils"; +import { getSession } from "next-auth/react"; + +export default async function PortfolioSummary({ + params, +}: { + params: { slug: string }; +}) { + const portfolioId = params.slug; + const { name: portfolioName } = await getPortfolio(portfolioId); + // Retrieve all of the names and ids of the portfolios, attributed to this user + // Get user id from the session + const userPortfolios = await getUserPortfolios(portfolioId); + + // Dummy data for rows + const data = [ + { + title: "EPCs", + today: "Initial Bar Chart Here", + summary: "Comparison Bar Chart", + }, + { title: "Total Co2", today: "100t", summary: "50t" }, + { title: "Energy bills", today: "£20,000", summary: "£10,000" }, + { title: "Cost", today: "", summary: "£32,412" }, + { title: "Cost per CO2 reduction", today: "", summary: "£50" }, + ]; + + return ( +
+

Overview

+ +
+ ); +}