mirror of
https://github.com/Portabase/portabase.git
synced 2026-07-14 11:16:13 +02:00
Profile Page with update user information.
This commit is contained in:
@@ -10,7 +10,7 @@ import {Button} from "@/components/ui/button";
|
||||
import {useRouter} from "next/navigation";
|
||||
import {useMutation} from "@tanstack/react-query";
|
||||
import {TooltipProvider} from "@/components/ui/tooltip";
|
||||
import {AgentSchema, AgentType} from "@/components/wrappers/Agent/AgentForm/action-form.schema";
|
||||
import {AgentSchema, AgentType} from "@/components/wrappers/Agent/AgentForm/agent-form.schema";
|
||||
import {toast} from "sonner";
|
||||
import {createAgentAction, updateAgentAction} from "@/components/wrappers/Agent/AgentForm/agent-form.action";
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"use server"
|
||||
import {ActionError, userAction} from "@/safe-actions";
|
||||
import {prisma} from "@/prisma";
|
||||
import {AgentSchema} from "@/components/wrappers/Agent/AgentForm/action-form.schema";
|
||||
import {AgentSchema} from "@/components/wrappers/Agent/AgentForm/agent-form.schema";
|
||||
import {z} from "zod";
|
||||
|
||||
|
||||
|
||||
@@ -30,6 +30,7 @@ export const RegisterForm = (props: registerFormProps) => {
|
||||
mutationFn: async (values: RegisterType) => {
|
||||
console.log(values)
|
||||
const createUser = await registerUserAction(values);
|
||||
console.log(createUser)
|
||||
const data = createUser?.data?.data
|
||||
if (createUser?.serverError || !data) {
|
||||
console.log(createUser?.serverError);
|
||||
|
||||
@@ -22,7 +22,6 @@ export const registerUserAction = action
|
||||
data: new_user,
|
||||
}
|
||||
}
|
||||
return {
|
||||
data: user,
|
||||
}
|
||||
throw new Error('An error occured while creating user');
|
||||
|
||||
});
|
||||
@@ -8,6 +8,7 @@ import Link from "next/link";
|
||||
import {useTranslations} from "use-intl";
|
||||
import {SidebarMenuButton} from "@/components/ui/sidebar";
|
||||
import {UserAvatar} from "@/components/wrappers/Dashboard/UserAvatar/UserAvatar";
|
||||
import {redirect} from "next/navigation";
|
||||
|
||||
export type LoggedInDropdownProps = PropsWithChildren<{}>
|
||||
|
||||
@@ -22,7 +23,9 @@ export const LoggedInDropdown = (props: LoggedInDropdownProps) => {
|
||||
side="top"
|
||||
className="w-[--radix-popper-anchor-width]"
|
||||
>
|
||||
<DropdownMenuItem>
|
||||
<DropdownMenuItem onClick={() => {
|
||||
redirect("/dashboard/profile")
|
||||
}}>
|
||||
<span>Account</span>
|
||||
</DropdownMenuItem>
|
||||
<DropdownMenuItem>
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
"use client";
|
||||
|
||||
import {Card, CardContent, CardDescription, CardHeader, CardTitle} from "@/components/ui/card";
|
||||
import {
|
||||
FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, useZodForm
|
||||
} from "@/components/ui/form";
|
||||
import {Input} from "@/components/ui/input";
|
||||
import {Form} from "@/components/ui/form"
|
||||
import {Button} from "@/components/ui/button";
|
||||
import {useRouter} from "next/navigation";
|
||||
import {useMutation} from "@tanstack/react-query";
|
||||
import {TooltipProvider} from "@/components/ui/tooltip";
|
||||
import {UserSchema, UserType} from "@/components/wrappers/Dashboard/Profile/UserForm/user-form.schema";
|
||||
import {toast} from "sonner";
|
||||
import {updateUserAction} from "@/components/wrappers/Dashboard/Profile/UserForm/user-form.action";
|
||||
import { useSession } from "next-auth/react";
|
||||
|
||||
export type userFormProps = {
|
||||
defaultValues?: UserType;
|
||||
userId?: string;
|
||||
}
|
||||
|
||||
export const UserForm = (props: userFormProps) => {
|
||||
|
||||
const isCreate = !Boolean(props.defaultValues)
|
||||
|
||||
const form = useZodForm({
|
||||
schema: UserSchema,
|
||||
defaultValues: props.defaultValues,
|
||||
});
|
||||
|
||||
const router = useRouter();
|
||||
const { data: session, update } = useSession();
|
||||
|
||||
const mutation = useMutation({
|
||||
mutationFn: async (values: UserType) => {
|
||||
console.log("values", values)
|
||||
console.log(props.userId)
|
||||
const updateUser = await updateUserAction({
|
||||
id: props.userId ?? "-",
|
||||
data: values
|
||||
})
|
||||
|
||||
const data = updateUser?.data?.data
|
||||
if (updateUser?.serverError || !data) {
|
||||
console.log(updateUser?.serverError);
|
||||
toast.error(updateUser?.serverError);
|
||||
return;
|
||||
}
|
||||
console.log("email:", values.email)
|
||||
|
||||
const newSession = {
|
||||
...session,
|
||||
user: {
|
||||
...session?.user,
|
||||
name: values.name,
|
||||
email: values.email
|
||||
},
|
||||
};
|
||||
|
||||
const updateSession = await update(newSession);
|
||||
console.log(updateSession);
|
||||
toast.success(`Success updating user informations`);
|
||||
router.push(`/dashboard/profile`);
|
||||
router.refresh()
|
||||
}
|
||||
})
|
||||
|
||||
return (
|
||||
<TooltipProvider>
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>
|
||||
Account
|
||||
</CardTitle>
|
||||
<CardDescription>
|
||||
Your informations
|
||||
</CardDescription>
|
||||
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<Form form={form}
|
||||
className="flex flex-col gap-4"
|
||||
onSubmit={async (values) => {
|
||||
await mutation.mutateAsync(values);
|
||||
}}
|
||||
>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="name"
|
||||
render={({field}) => (
|
||||
<FormItem>
|
||||
<FormLabel>Name</FormLabel>
|
||||
<FormControl>
|
||||
<Input
|
||||
placeholder={"Your Name"} {...field} />
|
||||
</FormControl>
|
||||
<FormMessage/>
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="email"
|
||||
render={({field}) => (
|
||||
<FormItem>
|
||||
<FormLabel>Email</FormLabel>
|
||||
<FormControl>
|
||||
<Input
|
||||
placeholder={'exemple@portabase.com'} disabled {...field}
|
||||
value={field.value ?? ""}/>
|
||||
</FormControl>
|
||||
<FormMessage/>
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<Button>
|
||||
{isCreate ? `` : `Save`}
|
||||
</Button>
|
||||
</Form>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</TooltipProvider>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
"use server"
|
||||
import {userAction} from "@/safe-actions";
|
||||
import {z} from "zod";
|
||||
import {prisma} from "@/prisma";
|
||||
import {UserSchema} from "@/components/wrappers/Dashboard/Profile/UserForm/user-form.schema";
|
||||
|
||||
export const updateUserAction = userAction
|
||||
.schema(
|
||||
z.object({
|
||||
id: z.string(),
|
||||
data: UserSchema,
|
||||
}
|
||||
)
|
||||
)
|
||||
.action(async ({parsedInput, ctx}) => {
|
||||
const updatedUser = await prisma.user.update({
|
||||
where: {
|
||||
id: parsedInput.id,
|
||||
},
|
||||
data: parsedInput.data,
|
||||
})
|
||||
return {
|
||||
data: updatedUser,
|
||||
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,8 @@
|
||||
import {z} from "zod";
|
||||
|
||||
export const UserSchema = z.object({
|
||||
name: z.string(),
|
||||
email: z.string(),
|
||||
});
|
||||
|
||||
export type UserType = z.infer<typeof UserSchema>;
|
||||
Reference in New Issue
Block a user