diff --git a/apps/web/app/(org)/dashboard/layout.tsx b/apps/web/app/(org)/dashboard/layout.tsx index 457fdd05800..90dc58aa4eb 100644 --- a/apps/web/app/(org)/dashboard/layout.tsx +++ b/apps/web/app/(org)/dashboard/layout.tsx @@ -26,7 +26,7 @@ export default async function DashboardLayout({ redirect("/login"); } - if (!user.name || user.name.length <= 1) { + if (!user.name || user.name.length === 0) { redirect("/onboarding"); } diff --git a/apps/web/app/(org)/dashboard/settings/account/Settings.tsx b/apps/web/app/(org)/dashboard/settings/account/Settings.tsx index 1377d7f7393..0aff8d02cd9 100644 --- a/apps/web/app/(org)/dashboard/settings/account/Settings.tsx +++ b/apps/web/app/(org)/dashboard/settings/account/Settings.tsx @@ -2,7 +2,9 @@ import type { users } from "@cap/database/schema"; import { Button, Card, CardDescription, CardTitle, Input } from "@cap/ui"; +import { useMutation } from "@tanstack/react-query"; import { useRouter } from "next/navigation"; +import { useState } from "react"; import { toast } from "sonner"; export const Settings = ({ @@ -10,41 +12,36 @@ export const Settings = ({ }: { user?: typeof users.$inferSelect | null; }) => { - const firstName = user ? user?.name : ""; - const lastName = user ? user?.lastName : ""; - const email = user ? user?.email : ""; + const [firstName, setFirstName] = useState(user?.name || ""); + const [lastName, setLastName] = useState(user?.lastName || ""); const router = useRouter(); - const handleSubmit = async (e: React.FormEvent) => { - e.preventDefault(); - - const formData = new FormData(e.currentTarget); - const firstName = formData.get("firstName") as string; - const lastName = formData.get("lastName") as string; - - try { - const response = await fetch("/api/settings/user/name", { + const { mutate: updateName, isPending: updateNamePending } = useMutation({ + mutationFn: async () => { + await fetch("/api/settings/user/name", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ firstName, lastName }), }); - - if (response.ok) { - toast.success("Name updated successfully"); - router.refresh(); - } else { - toast.error("Failed to update name"); - } - } catch (error) { - console.error("Error updating name:", error); - toast.error("An error occurred while updating name"); - } - }; + }, + onSuccess: () => { + toast.success("Name updated successfully"); + router.refresh(); + }, + onError: () => { + toast.error("Failed to update name"); + }, + }); return ( -
+ { + e.preventDefault(); + updateName(); + }} + >
Your name @@ -57,6 +54,7 @@ export const Settings = ({ setFirstName(e.target.value)} defaultValue={firstName as string} id="firstName" name="firstName" @@ -66,6 +64,7 @@ export const Settings = ({ setLastName(e.target.value)} defaultValue={lastName as string} id="lastName" name="lastName" @@ -82,15 +81,22 @@ export const Settings = ({
-
); diff --git a/apps/web/app/(org)/onboarding/Onboarding.tsx b/apps/web/app/(org)/onboarding/Onboarding.tsx index 6e5a530510a..cdffc88edf4 100644 --- a/apps/web/app/(org)/onboarding/Onboarding.tsx +++ b/apps/web/app/(org)/onboarding/Onboarding.tsx @@ -74,7 +74,6 @@ export const Onboarding = () => { id="lastName" name="lastName" placeholder="Last name" - required value={lastName} disabled={loading || isRedirecting} onChange={(e) => setLastName(e.target.value)} @@ -82,7 +81,7 @@ export const Onboarding = () => {