diff --git a/app/(customer)/dashboard/profile/page.tsx b/app/(customer)/dashboard/profile/page.tsx new file mode 100644 index 00000000..b74954db --- /dev/null +++ b/app/(customer)/dashboard/profile/page.tsx @@ -0,0 +1,44 @@ +import {PageParams} from "@/types/next"; +import {Page, PageActions, PageContent, PageHeader, PageTitle} from "@/features/layout/page"; +import {Avatar, AvatarFallback, AvatarImage} from "@/components/ui/avatar"; +import {notFound} from "next/navigation"; +import {requiredCurrentUser} from "@/auth/current-user"; +import {UserForm} from "@/components/wrappers/Dashboard/Profile/UserForm/UserForm"; +import {prisma} from "@/prisma"; +import {Badge} from "@/components/ui/badge"; + +export default async function RoutePage(props: PageParams<{}>) { + + const user = await requiredCurrentUser() + if (!user) { + notFound() + } + + const userInfo = await prisma.user.findUnique({ + where: { + email: user.email + } + }) + + console.log(userInfo) + + return ( + + + + + {user.name?.[0]} + {user.image ? ( + + ) : null} + + {user.name} + {userInfo.authMethod} + + + + + + +) +} \ No newline at end of file diff --git a/src/auth/auth.ts b/src/auth/auth.ts index 3dfaa878..f5999e2a 100644 --- a/src/auth/auth.ts +++ b/src/auth/auth.ts @@ -65,10 +65,21 @@ export const {handlers, auth: baseAuth, signIn, signOut} = NextAuth({ error: "/error" }, callbacks: { - session({session, user, token}) { - // session.user.role = user.role - // session.user.authMethod = user.authMethod; - return session + // session({session, user, token}) { + // // session.user.role = user.role + // // session.user.authMethod = user.authMethod; + // return session + // }, + async jwt({ token, trigger, session, user }) { + if (trigger === "update" && session) { + return { ...token, ...session?.user }; + } + + return { ...token, ...user }; + }, + async session({ session, token, user }) { + session.user = token; + return session; }, async signIn({ account, user }) { console.log(account) @@ -89,10 +100,9 @@ export const {handlers, auth: baseAuth, signIn, signOut} = NextAuth({ authMethod: account.provider }, }); - return true; }, - }, + } }); diff --git a/src/components/wrappers/Agent/AgentForm/AgentForm.tsx b/src/components/wrappers/Agent/AgentForm/AgentForm.tsx index 6b5a8bf0..7ccce815 100644 --- a/src/components/wrappers/Agent/AgentForm/AgentForm.tsx +++ b/src/components/wrappers/Agent/AgentForm/AgentForm.tsx @@ -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"; diff --git a/src/components/wrappers/Agent/AgentForm/agent-form.action.ts b/src/components/wrappers/Agent/AgentForm/agent-form.action.ts index 66c9bfc6..a87253ca 100644 --- a/src/components/wrappers/Agent/AgentForm/agent-form.action.ts +++ b/src/components/wrappers/Agent/AgentForm/agent-form.action.ts @@ -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"; diff --git a/src/components/wrappers/Agent/AgentForm/action-form.schema.ts b/src/components/wrappers/Agent/AgentForm/agent-form.schema.ts similarity index 100% rename from src/components/wrappers/Agent/AgentForm/action-form.schema.ts rename to src/components/wrappers/Agent/AgentForm/agent-form.schema.ts diff --git a/src/components/wrappers/Auth/Register/RegisterForm/RegisterForm.tsx b/src/components/wrappers/Auth/Register/RegisterForm/RegisterForm.tsx index d4b4b333..1758aabf 100644 --- a/src/components/wrappers/Auth/Register/RegisterForm/RegisterForm.tsx +++ b/src/components/wrappers/Auth/Register/RegisterForm/RegisterForm.tsx @@ -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); diff --git a/src/components/wrappers/Auth/Register/RegisterForm/register-form.action.ts b/src/components/wrappers/Auth/Register/RegisterForm/register-form.action.ts index 99391f0d..78fb480d 100644 --- a/src/components/wrappers/Auth/Register/RegisterForm/register-form.action.ts +++ b/src/components/wrappers/Auth/Register/RegisterForm/register-form.action.ts @@ -22,7 +22,6 @@ export const registerUserAction = action data: new_user, } } - return { - data: user, - } + throw new Error('An error occured while creating user'); + }); \ No newline at end of file diff --git a/src/components/wrappers/Dashboard/LoggedInDropdown/LoggedInDropdown.tsx b/src/components/wrappers/Dashboard/LoggedInDropdown/LoggedInDropdown.tsx index af5ac6b7..f0a644e3 100644 --- a/src/components/wrappers/Dashboard/LoggedInDropdown/LoggedInDropdown.tsx +++ b/src/components/wrappers/Dashboard/LoggedInDropdown/LoggedInDropdown.tsx @@ -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]" > - + { + redirect("/dashboard/profile") + }}> Account diff --git a/src/components/wrappers/Dashboard/Profile/UserForm/UserForm.tsx b/src/components/wrappers/Dashboard/Profile/UserForm/UserForm.tsx new file mode 100644 index 00000000..e17769c9 --- /dev/null +++ b/src/components/wrappers/Dashboard/Profile/UserForm/UserForm.tsx @@ -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 ( + + + + + Account + + + Your informations + + + + +
{ + await mutation.mutateAsync(values); + }} + > + ( + + Name + + + + + + )} + /> + ( + + Email + + + + + + )} + /> + + +
+
+
+ ) +} \ No newline at end of file diff --git a/src/components/wrappers/Dashboard/Profile/UserForm/user-form.action.ts b/src/components/wrappers/Dashboard/Profile/UserForm/user-form.action.ts new file mode 100644 index 00000000..00f8f641 --- /dev/null +++ b/src/components/wrappers/Dashboard/Profile/UserForm/user-form.action.ts @@ -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, + + } + }) diff --git a/src/components/wrappers/Dashboard/Profile/UserForm/user-form.schema.ts b/src/components/wrappers/Dashboard/Profile/UserForm/user-form.schema.ts new file mode 100644 index 00000000..ab91c203 --- /dev/null +++ b/src/components/wrappers/Dashboard/Profile/UserForm/user-form.schema.ts @@ -0,0 +1,8 @@ +import {z} from "zod"; + +export const UserSchema = z.object({ + name: z.string(), + email: z.string(), +}); + +export type UserType = z.infer; diff --git a/src/features/layout/page.tsx b/src/features/layout/page.tsx index e5e8030e..944cfb09 100644 --- a/src/features/layout/page.tsx +++ b/src/features/layout/page.tsx @@ -1,4 +1,6 @@ import {PropsWithChildren} from 'react'; +import {twx} from "@/lib/twx"; +import {cn} from "@/lib/utils"; export const Page = ({children}: PropsWithChildren<{}>) => { return ( @@ -12,17 +14,17 @@ export const PageHeader = ({children}: PropsWithChildren<{}>) => { ); }; -export const PageTitle = ({children}: PropsWithChildren<{}>) => { - return ( -

{children}

- ); -}; -export const PageDescription = ({children}: PropsWithChildren<{}>) => { - return ( -

{children}

- ); -}; +export const PageTitle = twx.h1((props)=>[ + cn(`text-3xl font-bold mb-6`, props.className), +]) + + + +export const PageDescription = twx.h2((props)=>[ + cn(`text-s mb-6 text-gray-700`, props.className), +]) + export const PageActions = ({children}: PropsWithChildren<{}>) => { return ( @@ -35,4 +37,5 @@ export const PageContent = ({children}: PropsWithChildren<{}>) => { return (
{children}
); -}; \ No newline at end of file +}; +