diff --git a/backend/database/src/index.ts b/backend/database/src/index.ts index 2968f4d..39349e0 100644 --- a/backend/database/src/index.ts +++ b/backend/database/src/index.ts @@ -21,38 +21,11 @@ export default { const db = drizzle(env.DB, { schema }); - if (path.startsWith("/api/user")) { - if (path === "/api/user") { - if (method === "GET") { - const params = url.searchParams; - - if (params.has("id")) { - const id = params.get("id") as string; - const res = await db.select().from(user).where(eq(user.id, id)).get(); - console.log(res); - return json(res ?? {}); - } else { - const res = await db.select().from(user).all(); - return json(res ?? {}); - } - } else if (method === "POST") { - const userSchema = z.object({ - id: z.string(), - name: z.string(), - email: z.string().email(), - }); - - const body = await request.json(); - const { id, name, email } = userSchema.parse(body); - - const res = await db.insert(user).values({ id, name, email }).returning().get(); - return json({ res }); - } else { - return new Response("Method Not Allowed", { status: 405 }); - } - } else if (path === "/api/user/sandbox") { + if (path === "/api/user") { + if (method === "GET") { const params = url.searchParams; - if (method === "GET" && params.has("id")) { + + if (params.has("id")) { const id = params.get("id") as string; const res = await db.query.user.findFirst({ where: (user, { eq }) => eq(user.id, id), @@ -62,10 +35,23 @@ export default { }); return json(res ?? {}); } else { - return new Response("Method Not Allowed", { status: 405 }); + const res = await db.select().from(user).all(); + return json(res ?? {}); } + } else if (method === "POST") { + const userSchema = z.object({ + id: z.string(), + name: z.string(), + email: z.string().email(), + }); + + const body = await request.json(); + const { id, name, email } = userSchema.parse(body); + + const res = await db.insert(user).values({ id, name, email }).returning().get(); + return json({ res }); } else { - return new Response("Not Found", { status: 404 }); + return new Response("Method Not Allowed", { status: 405 }); } } else return new Response("Not Found", { status: 404 }); }, diff --git a/frontend/app/(app)/dashboard/page.tsx b/frontend/app/(app)/dashboard/page.tsx index 467d1c7..e2db8a6 100644 --- a/frontend/app/(app)/dashboard/page.tsx +++ b/frontend/app/(app)/dashboard/page.tsx @@ -2,7 +2,7 @@ import { UserButton, currentUser } from "@clerk/nextjs" import { redirect } from "next/navigation" import Dashboard from "@/components/dashboard" import Navbar from "@/components/dashboard/navbar" -import { Sandbox } from "@/lib/types" +import { Sandbox, User } from "@/lib/types" export default async function DashboardPage() { const user = await currentUser() @@ -11,17 +11,13 @@ export default async function DashboardPage() { redirect("/") } - const res = await fetch( - `http://localhost:8787/api/user/sandbox?id=${user.id}` - ) - const data = (await res.json()).sandbox as Sandbox[] - - console.log(data) + const userRes = await fetch(`http://localhost:8787/api/user?id=${user.id}`) + const userData = (await userRes.json()) as User return (
- - + +
) } diff --git a/frontend/components/dashboard/navbar/index.tsx b/frontend/components/dashboard/navbar/index.tsx index 3a31729..1753c13 100644 --- a/frontend/components/dashboard/navbar/index.tsx +++ b/frontend/components/dashboard/navbar/index.tsx @@ -1,12 +1,11 @@ -import { UserButton } from "@clerk/nextjs" -import { dark } from "@clerk/themes" import Image from "next/image" import Link from "next/link" import Logo from "@/assets/logo.svg" import DashboardNavbarSearch from "./search" import DashboardUserButton from "./userButton" +import { User } from "@/lib/types" -export default function DashboardNavbar({ userId }: { userId: string }) { +export default function DashboardNavbar({ userData }: { userData: User }) { return (
@@ -20,7 +19,7 @@ export default function DashboardNavbar({ userId }: { userId: string }) {
- +
) diff --git a/frontend/components/dashboard/navbar/userButton.tsx b/frontend/components/dashboard/navbar/userButton.tsx index c8a3aae..99f37d5 100644 --- a/frontend/components/dashboard/navbar/userButton.tsx +++ b/frontend/components/dashboard/navbar/userButton.tsx @@ -8,18 +8,25 @@ import { DropdownMenuSeparator, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu" +import { User } from "@/lib/types" import { LogOut, Pencil } from "lucide-react" -export default function DashboardUserButton({ userId }: { userId: string }) { +export default function DashboardUserButton({ userData }: { userData: User }) { return ( -
+
+ {userData.name + .split(" ") + .slice(0, 2) + .map((name) => name[0].toUpperCase())} +
Edit Profile + {/* open modal with name and email (disabled) */} diff --git a/frontend/lib/types.ts b/frontend/lib/types.ts index df47309..f9bf047 100644 --- a/frontend/lib/types.ts +++ b/frontend/lib/types.ts @@ -4,6 +4,7 @@ export type User = { id: string name: string email: string + sandbox: Sandbox[] } export type Sandbox = {