-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
152 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { UserForm } from 'components/forms/user-form'; | ||
import { Section } from 'components/section'; | ||
import { redirect } from 'next/navigation'; | ||
import { createTrpcCaller } from 'server/api/caller'; | ||
import { getServerAuthSession } from 'server/auth'; | ||
|
||
export default async function ProfilePage() { | ||
const session = await getServerAuthSession(); | ||
|
||
if (!session) { | ||
redirect('/logowanie'); | ||
} | ||
|
||
const caller = await createTrpcCaller(); | ||
const user = await caller.user.getById({ | ||
userId: session.user.id, | ||
}); | ||
|
||
if (!user) { | ||
redirect('/'); | ||
} | ||
|
||
return ( | ||
<Section title="Profil"> | ||
<UserForm user={user} /> | ||
</Section> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
'use client'; | ||
|
||
import { zodResolver } from '@hookform/resolvers/zod'; | ||
import { Button } from 'components/ui/button'; | ||
import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from 'components/ui/form'; | ||
import { Input } from 'components/ui/input'; | ||
import { useUpdateUser } from 'hooks/use-update-user'; | ||
import { useForm } from 'react-hook-form'; | ||
import { GetUserById } from 'utils/api'; | ||
import { z } from 'zod'; | ||
|
||
const updateUserFormSchema = z.object({ | ||
name: z | ||
.string({ required_error: 'Musisz podać imię i nazwisko' }) | ||
.min(3, 'Minimalna długość to 3 znaki') | ||
.refine((value) => value.split(' ').length === 2, { message: 'Musisz podać imię i nazwisko' }), | ||
}); | ||
|
||
type UpdateUserFormSchema = z.infer<typeof updateUserFormSchema>; | ||
|
||
interface UserFormProps { | ||
user: GetUserById; | ||
} | ||
|
||
export function UserForm({ user }: UserFormProps) { | ||
const defaultValues = user | ||
? { | ||
name: user.name || '', | ||
} | ||
: { | ||
name: '', | ||
}; | ||
|
||
const form = useForm<UpdateUserFormSchema>({ | ||
resolver: zodResolver(updateUserFormSchema), | ||
defaultValues, | ||
}); | ||
|
||
const { mutate: updateUser } = useUpdateUser(); | ||
|
||
const handleUpdateUser = async (values: UpdateUserFormSchema) => { | ||
if (user) { | ||
updateUser({ userId: user.id, name: values.name }); | ||
} | ||
}; | ||
|
||
return ( | ||
<Form {...form}> | ||
<form onSubmit={form.handleSubmit(handleUpdateUser)}> | ||
<FormField | ||
control={form.control} | ||
name="name" | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>Imię i nazwisko</FormLabel> | ||
<FormControl> | ||
<Input {...field} /> | ||
</FormControl> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
<div className="mt-6 flex justify-end"> | ||
<Button>Zapisz</Button> | ||
</div> | ||
</form> | ||
</Form> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { useRouter } from 'next/navigation'; | ||
import { api } from 'utils/api'; | ||
|
||
export function useUpdateUser() { | ||
const router = useRouter(); | ||
const utils = api.useContext(); | ||
|
||
return api.user.update.useMutation({ | ||
async onSuccess(_, variables) { | ||
await utils.user.getById.invalidate({ userId: variables.userId }); | ||
router.refresh(); | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters