From 6748d0a62577d58219e29a5b0c952d7729f29ea9 Mon Sep 17 00:00:00 2001 From: Khalim Conn-Kowlessar Date: Thu, 13 Jul 2023 17:23:29 +0100 Subject: [PATCH] Fixing bug with hardcoded userId and creation of portfolio| --- src/app/api/portfolio/route.ts | 5 ++++- src/app/components/home/Card.tsx | 5 ++++- src/app/components/home/ModalSubmit.tsx | 14 +++++++++++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/app/api/portfolio/route.ts b/src/app/api/portfolio/route.ts index 71b2992d..cf8303ca 100644 --- a/src/app/api/portfolio/route.ts +++ b/src/app/api/portfolio/route.ts @@ -46,7 +46,10 @@ export async function POST(request: NextRequest) { .values({ userId: userId, portfolioId: newPortfolioId, role: role }) .returning(); - return new NextResponse(JSON.stringify(response), { status: 201 }); + if (response.length !== 1) { + throw new Error("Got back more than one row from insert - investigate"); + } + return new NextResponse(JSON.stringify(response[0]), { status: 201 }); } catch (error) { console.error(error); throw error; diff --git a/src/app/components/home/Card.tsx b/src/app/components/home/Card.tsx index 6259dcae..398191c0 100644 --- a/src/app/components/home/Card.tsx +++ b/src/app/components/home/Card.tsx @@ -2,6 +2,7 @@ import React from "react"; import StatusBadge from "@/app/components/StatusBadge"; import { useRouter } from "next/navigation"; import { PortfolioStatus } from "@/app/db/schema/portfolio"; +import { formatNumber } from "@/app/utils"; const styles = { wrapper: @@ -28,6 +29,8 @@ const Card = ({ id, title, image, budget, status }: CardProps) => { router.push(`/portfolio/${id}`); } + const budgetFormatted = budget ? "£" + formatNumber(budget) : ""; + return (
{

{`${title}`}

-
{budget}
+
{budgetFormatted}
diff --git a/src/app/components/home/ModalSubmit.tsx b/src/app/components/home/ModalSubmit.tsx index 41450fbd..eac359ba 100644 --- a/src/app/components/home/ModalSubmit.tsx +++ b/src/app/components/home/ModalSubmit.tsx @@ -1,3 +1,5 @@ +"use client"; + import { useMutation } from "@tanstack/react-query"; import { useRouter } from "next/navigation"; import { @@ -5,6 +7,7 @@ import { PortfolioGoal, PortfolioRole, } from "@/app/db/schema/portfolio"; +import { useSession } from "next-auth/react"; type CreatePortfolioArgs = { userId: number; @@ -60,6 +63,15 @@ const ModalSubmit = ({ }) => { const router = useRouter(); + // This is a client component so we can access the session directly + const session = useSession(); + + if (!session.data) { + // The user is not logged in, redirect them to sign in + router.push("/"); + return null; + } + const { mutate, isLoading } = useMutation(createPortfolio, { onSuccess: (data) => { router.push(`/portfolio/${data.id}`); @@ -71,7 +83,7 @@ const ModalSubmit = ({ }); const handleSubmit = () => { - const userId = 2; + const userId = session.data.user.dbId; const status = "scoping"; const role = "creator";