|
| 1 | +import { |
| 2 | + Heading, |
| 3 | + Text, |
| 4 | + MultiStep, |
| 5 | + Button, |
| 6 | + TextArea, |
| 7 | + Avatar, |
| 8 | +} from '@ignite-ui/react' |
| 9 | +import { ArrowRight } from 'phosphor-react' |
| 10 | +import { useForm } from 'react-hook-form' |
| 11 | +import { z } from 'zod' |
| 12 | +import { zodResolver } from '@hookform/resolvers/zod' |
| 13 | +import { Container, Header } from '../styles' |
| 14 | +import { FormAnnotation, ProfileBox } from './styles' |
| 15 | +import { getSession, useSession } from 'next-auth/react' |
| 16 | +import { api } from '@/lib/axios' |
| 17 | +import { useRouter } from 'next/router' |
| 18 | +import { GetServerSideProps } from 'next' |
| 19 | + |
| 20 | +const updateProfileSchema = z.object({ |
| 21 | + bio: z.string(), |
| 22 | +}) |
| 23 | + |
| 24 | +type UpdateProfileData = z.infer<typeof updateProfileSchema> |
| 25 | + |
| 26 | +export default function UpdateProfile() { |
| 27 | + const { |
| 28 | + register, |
| 29 | + handleSubmit, |
| 30 | + formState: { isSubmitting }, |
| 31 | + } = useForm<UpdateProfileData>({ |
| 32 | + resolver: zodResolver(updateProfileSchema), |
| 33 | + }) |
| 34 | + |
| 35 | + const session = useSession() |
| 36 | + const router = useRouter() |
| 37 | + |
| 38 | + async function handleUpdateProfile(data: UpdateProfileData) { |
| 39 | + await api.put('/users/profile', { |
| 40 | + bio: data.bio, |
| 41 | + }) |
| 42 | + await router.push(`/schedule/${session.data?.user.username}`) |
| 43 | + } |
| 44 | + |
| 45 | + return ( |
| 46 | + <Container> |
| 47 | + <Header> |
| 48 | + <Heading as={'strong'}>Bem-vindo ao Ignite Call!</Heading> |
| 49 | + <Text> |
| 50 | + Precisamos de algumas informações para criar seu perfil! Ah, você pode |
| 51 | + editar essas informações depois. |
| 52 | + </Text> |
| 53 | + <MultiStep size={4} currentStep={4} /> |
| 54 | + </Header> |
| 55 | + |
| 56 | + <ProfileBox as="form" onSubmit={handleSubmit(handleUpdateProfile)}> |
| 57 | + <label> |
| 58 | + <Text>Foto de perfil</Text> |
| 59 | + <Avatar |
| 60 | + src={session.data?.user.avatar_url} |
| 61 | + alt={session.data?.user.name} |
| 62 | + /> |
| 63 | + </label> |
| 64 | + <label> |
| 65 | + <Text size={'sm'}>Sobre você</Text> |
| 66 | + <TextArea {...register('bio')} /> |
| 67 | + <FormAnnotation size={'sm'}> |
| 68 | + Fale um pouco sobre você. Isto será exibido em sua página pessoal. |
| 69 | + </FormAnnotation> |
| 70 | + </label> |
| 71 | + |
| 72 | + <Button type="submit" disabled={isSubmitting}> |
| 73 | + Finalizar |
| 74 | + <ArrowRight /> |
| 75 | + </Button> |
| 76 | + </ProfileBox> |
| 77 | + </Container> |
| 78 | + ) |
| 79 | +} |
| 80 | + |
| 81 | +export const getServerSideProps: GetServerSideProps = async ({ req, res }) => { |
| 82 | + const session = await getSession({ req }) |
| 83 | + |
| 84 | + return { |
| 85 | + props: { |
| 86 | + session, |
| 87 | + }, |
| 88 | + } |
| 89 | +} |
0 commit comments