diff --git a/apps/web/app/api/user/update/route.ts b/apps/web/app/api/user/update/route.ts new file mode 100644 index 00000000..8a6168a6 --- /dev/null +++ b/apps/web/app/api/user/update/route.ts @@ -0,0 +1,23 @@ +import { getServerSession } from "next-auth"; +import { NextResponse } from "next/server"; +import db from "@repo/db/client"; +import { authOptions } from "../../../../lib/auth"; + +export async function PUT(req: Request) { + const session = await getServerSession(authOptions); + if (!session?.user?.email) { + return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); + } + + const { name } = await req.json(); + if (!name) { + return NextResponse.json({ error: "Name is required" }, { status: 400 }); + } + + const user = await db.user.update({ + where: { email: session.user.email }, + data: { name } + }); + + return NextResponse.json(user); +} diff --git a/apps/web/components/UserDetailForm.tsx b/apps/web/components/UserDetailForm.tsx index aad4a2a9..10df06c3 100644 --- a/apps/web/components/UserDetailForm.tsx +++ b/apps/web/components/UserDetailForm.tsx @@ -1,25 +1,79 @@ -import { Input, Label } from "@repo/ui"; -import React from "react"; +import { Input, Label, Button } from "@repo/ui"; +import { Pencil, Check, X } from "lucide-react"; +import React, { useState } from "react"; +import { useRouter } from "next/navigation"; +import { useSession } from "next-auth/react"; import UserImage from "./UserImage"; import { User } from "@prisma/client"; export default function UserDetailForm({ user }: { user: User }) { + const [isEditing, setIsEditing] = useState(false); + const [name, setName] = useState(user?.name || ""); + const { data: session, update: updateSession } = useSession(); + const router = useRouter(); + + const handleSubmit = async () => { + try { + const res = await fetch("/api/user/update", { + method: "PUT", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ name }), + }); + if (!res.ok) throw new Error("Failed to update name"); + + await updateSession(); + setIsEditing(false); + router.refresh(); + } catch (error) { + console.error("Error updating name:", error); + } + }; + return (