Added get portfolio id api

This commit is contained in:
Khalim Conn-Kowlessar 2023-07-12 12:22:36 +01:00
parent 0ce99e0f94
commit b5ab39cb09
4 changed files with 26 additions and 205 deletions

View file

@ -5,7 +5,6 @@ import {
PortfolioStatus,
PortfolioRole,
} from "@/app/db/schema/portfolio";
import type { NewPortfolio } from "@/app/db/schema/portfolio";
import { NextRequest, NextResponse } from "next/server";
import { db } from "@/app/db/db";
import { z } from "zod";

View file

@ -62,11 +62,7 @@ const ModalSubmit = ({
const { mutate, isLoading } = useMutation(createPortfolio, {
onSuccess: (data) => {
// handle success, e.g. navigate to new page
console.log("SUCCESS", data);
router.push(
`/portfolio/${data.id}?name=${portfolioName}&budget=${budget}&objective=${objective}`
);
router.push(`/portfolio/${data.id}`);
},
onError: (error) => {
// handle error

View file

@ -4,7 +4,7 @@ import { Menu } from "@headlessui/react";
import Link from "next/link";
import { PlusIcon, TableCellsIcon } from "@heroicons/react/24/outline";
export default function AddNew({ portfolioId }: { portfolioId: string }) {
export default function AddNew({ portfolioId }: { portfolioId: number }) {
return (
<Menu as="div" className="relative">
<Menu.Button className="rounded-md bg-brandtan text-white hover:bg-hovertan">

View file

@ -2,30 +2,14 @@ import { PencilSquareIcon, HomeIcon } from "@heroicons/react/24/outline";
import AddNew from "../../components/portfolio/AddNew";
import { formatNumber } from "@/app/utils";
import Link from "next/link";
import { SearchData } from "@/types/epc";
import { ArrowRightCircleIcon } from "@heroicons/react/20/solid";
import { Badge } from "@/app/shadcn_components/ui/badge";
import type { PortfolioStatus } from "@/app/db/schema/portfolio";
import StatusBadge from "@/app/components/StatusBadge";
import { getPortfolio } from "./utils";
function generateProperties(
value: number,
suffix: string,
status: (typeof PortfolioStatus)[number]
) {
const length = Math.ceil(value / 30000);
return Array.from({ length }, (_, index) => ({
id: `${index + 1}-k290f1ee-6c54-4b01-90e6-d701748f0851-${suffix}`,
lmkKey: "demo-property",
address: `${index + 1} Test Street`,
postcode: "TE1 1ST",
cost: 30000,
co2Reduction: 9 / 5,
targetEpcRating: "C",
status: status,
}));
}
// We enfore caching of data for 60 seconds
export const revalidate = 60;
type Property = {
id: string;
@ -56,7 +40,7 @@ function Propertycards({
portfolioId,
}: {
properties: Property[];
portfolioId: string;
portfolioId: number;
}) {
return (
<div className="flex justify-center">
@ -111,184 +95,26 @@ export default async function Page({
params,
searchParams,
}: {
params: { slug: string };
params: { slug: number };
searchParams: { [key: string]: string | string[] | undefined | number };
}) {
// This page is served from the server so we can make calls to the database
// This is temp until we retrieve this data from the frontend
// TODO: Update the objects to contains objective + any other required information
const Portfolios = [
{
id: "f290f1ee-6c54-4b01-90e6-d701748f0851",
properties: [] as Property[],
},
{
id: "d290f1ee-6c54-4b01-90e6-d701748f0851",
title: "Portfolio 1",
budget: 500000,
cost: 500000,
properties: generateProperties(500000, "1", "scoping") as Property[],
co2Reduction: 5.4,
totalWorksHours: 45,
totalValueIncrease: 500000 * 1.2,
rentalYieldIncrease: 500000 * 0.002,
},
{
id: "d290f1ee-6c54-4b01-90e6-d701748f0852",
title: "Portfolio 2",
budget: 150000,
cost: 150000,
co2Reduction: 9.0,
totalWorksHours: 30,
totalValueIncrease: 150000 * 1.2,
rentalYieldIncrease: 150000 * 0.002,
properties: generateProperties(150000, "2", "assessment") as Property[],
},
{
id: "d290f1ee-6c54-4b01-90e6-d701748f0853",
title: "Portfolio 3",
budget: 1000000,
cost: 1000000,
co2Reduction: 15.4,
totalWorksHours: 100,
totalValueIncrease: 1000000 * 1.2,
rentalYieldIncrease: 1000000 * 0.002,
properties: generateProperties(1000000, "3", "tendering") as Property[],
},
{
id: "d290f1ee-6c54-4b01-90e6-d701748f0854",
title: "Portfolio 4",
budget: 2000000,
cost: 2000000,
co2Reduction: 3.8,
totalWorksHours: 150,
totalValueIncrease: 2000000 * 1.2,
rentalYieldIncrease: 2000000 * 0.002,
properties: generateProperties(2000000, "4", "tendering") as Property[],
},
{
id: "d290f1ee-6c54-4b01-90e6-d701748f0855",
title: "Portfolio 5",
budget: 250000,
cost: 250000,
co2Reduction: 1.9,
totalWorksHours: 15,
totalValueIncrease: 250000 * 1.2,
rentalYieldIncrease: 250000 * 0.002,
properties: generateProperties(
250000,
"5",
"project underway"
) as Property[],
},
{
id: "d290f1ee-6c54-4b01-90e6-d701748f0856",
title: "Portfolio 6",
budget: 410000,
cost: 410000,
co2Reduction: 1.4,
totalWorksHours: 10,
totalValueIncrease: 410000 * 1.2,
rentalYieldIncrease: 410000 * 0.002,
properties: generateProperties(
410000,
"6",
"completion; status 'on track'"
) as Property[],
},
{
id: "d290f1ee-6c54-4b01-90e6-d701748f0857",
title: "Portfolio 7",
budget: 233000,
cost: 233000,
co2Reduction: 10.4,
totalWorksHours: 25,
totalValueIncrease: 233000 * 1.2,
rentalYieldIncrease: 233000 * 0.002,
properties: generateProperties(
233000,
"7",
"completion; status 'delayed'"
) as Property[],
},
{
id: "d290f1ee-6c54-4b01-90e6-d701748f0858",
title: "Portfolio 8",
budget: 670000,
cost: 670000,
co2Reduction: 4.5,
totalWorksHours: 65,
totalValueIncrease: 670000 * 1.2,
rentalYieldIncrease: 670000 * 0.002,
properties: generateProperties(
670000,
"8",
"completion; status 'at risk'"
) as Property[],
},
{
id: "d290f1ee-6c54-4b01-90e6-d701748f0859",
title: "Portfolio 9",
budget: 240000,
cost: 240000,
co2Reduction: 8.4,
totalWorksHours: 40,
totalValueIncrease: 240000 * 1.2,
rentalYieldIncrease: 240000 * 0.002,
properties: generateProperties(
240000,
"9",
"completion; status 'completed'"
) as Property[],
},
{
id: "d290f1ee-6c54-4b01-90e6-d701748f0860",
title: "Portfolio 10",
budget: 93000,
cost: 93000,
co2Reduction: 5.4,
totalWorksHours: 18,
totalValueIncrease: 93000 * 1.2,
rentalYieldIncrease: 93000 * 0.002,
properties: generateProperties(93000, "10", "needs review") as Property[],
},
];
const demo_id = "f290f1ee-6c54-4b01-90e6-d701748f0851";
let page_config;
let pageName;
let budget;
let co2Reduction: number;
let totalWorksHours: number;
let totalValueIncrease: number;
let rentalYieldIncrease: number;
let totalCost: number;
if (params.slug === demo_id) {
page_config = {
properties: [] as Property[],
...searchParams,
};
pageName = searchParams.name;
budget = searchParams.budget;
co2Reduction = 0;
totalWorksHours = 0;
totalValueIncrease = 0;
rentalYieldIncrease = 0;
totalCost = 0;
} else {
page_config = Portfolios.filter((portfolio) => {
return portfolio.id == params.slug;
})[0];
pageName = page_config.title;
budget = page_config.budget;
co2Reduction = page_config.co2Reduction as number;
totalWorksHours = page_config.totalWorksHours as number;
totalValueIncrease = page_config.totalValueIncrease as number;
rentalYieldIncrease = page_config.rentalYieldIncrease as number;
totalCost = page_config.cost as number;
}
const portfolioId = params.slug;
const {
name: portfolioName,
budget,
cost: totalCost,
co2EquivalentSavings,
totalWorkHours,
propertyValuationIncrease,
rentalYieldIncrease,
} = await getPortfolio(portfolioId);
// TODO: Put this back in for demo
const properties = page_config.properties;
const properties: Property[] = [];
// TODO: Add the porfolio summary information on the left
// Add an empty state for when there are no properties
@ -298,7 +124,7 @@ export default async function Page({
<>
<div className="flex justify-center">
<h1 className="text-3xl text-brandblue font-bold mt-3 mb-4">
Portfolio: {pageName}
Portfolio: {portfolioName}
</h1>
</div>
<div className="flex justify-center">
@ -323,13 +149,13 @@ export default async function Page({
Number of properties: {properties.length}
</li>
<li className="text-gray-700 px-2 mb-2">
Annual Co2 Savings: {co2Reduction} tonnes
Annual Co2 Savings: {co2EquivalentSavings} tonnes
</li>
<li className="text-gray-700 px-2 mb-2">
Total Works Time: {totalWorksHours} hours
Total Works Time: {totalWorkHours} hours
</li>
<li className="text-gray-700 px-2 mb-2">
Total Value Increase: £{totalValueIncrease}
Total Value Increase: £{propertyValuationIncrease}
</li>
<li className="text-gray-700 px-2 mb-2">
Annual rental yield Increase: £{rentalYieldIncrease}
@ -337,7 +163,7 @@ export default async function Page({
</ul>
</div>
<div className="w-full flex justify-center">
<AddNew portfolioId={demo_id} />
<AddNew portfolioId={portfolioId} />
</div>
</div>
<div className="col-span-6 bg-white">
@ -346,7 +172,7 @@ export default async function Page({
) : (
<Propertycards
properties={properties}
portfolioId={params.slug}
portfolioId={portfolioId}
/>
)}
</div>