|
| 1 | +import { FetchBaseQueryError } from '@reduxjs/toolkit/dist/query'; |
| 2 | +import React from 'react'; |
| 3 | +import { yupResolver } from '@hookform/resolvers/yup'; |
| 4 | +import { |
| 5 | + FormControl, |
| 6 | + FormErrorMessage, |
| 7 | + Icon, |
| 8 | + Input, |
| 9 | + InputGroup, |
| 10 | + InputLeftElement, |
| 11 | + Stack, |
| 12 | + useColorModeValue, |
| 13 | +} from '@chakra-ui/react'; |
| 14 | +import { FaUserAlt } from 'react-icons/fa'; |
| 15 | +import { IoMdMail as IoMail } from 'react-icons/io'; |
| 16 | +import { useForm, SubmitHandler, FormProvider } from 'react-hook-form'; |
| 17 | +import * as yup from 'yup'; |
| 18 | +import { FormErrors } from '@/components/Form'; |
| 19 | +import { FormButton } from '@/components/Form/FormButton'; |
| 20 | +import { AuthPayload } from '../types'; |
| 21 | +import { useSignupMutation } from '../api/signupApi'; |
| 22 | +import { PasswordInputs } from './PasswordsInputs'; |
| 23 | + |
| 24 | +const schema = yup.object().shape({ |
| 25 | + name: yup |
| 26 | + .string() |
| 27 | + .min(3, `La longueur min est 3`) |
| 28 | + .max(30, `La longueur maximale est 30`) |
| 29 | + .required(), |
| 30 | + email: yup.string().email().required(), |
| 31 | + password: yup |
| 32 | + .string() |
| 33 | + .min(4, `La longueur min est 3`) |
| 34 | + .max(30, `La longueur maximale est 30`) |
| 35 | + .required(`Ce champ est obligatoire`), |
| 36 | +}); |
| 37 | + |
| 38 | +export const FormSignin = () => { |
| 39 | + const methods = useForm<AuthPayload>({ |
| 40 | + resolver: yupResolver(schema), |
| 41 | + }); |
| 42 | + const [ |
| 43 | + signup, // This is the mutation trigger |
| 44 | + { isLoading, error, status }, // This is the destructured mutation result |
| 45 | + ] = useSignupMutation(); |
| 46 | + |
| 47 | + const bgFormColor = useColorModeValue(`gray.100`, `gray.700`); |
| 48 | + const iconColor = useColorModeValue(`gray.400`, `gray.300`); |
| 49 | + |
| 50 | + const FormSubmitHandler: SubmitHandler<AuthPayload> = (data: AuthPayload) => { |
| 51 | + signup(data); |
| 52 | + }; |
| 53 | + |
| 54 | + return ( |
| 55 | + <FormProvider {...methods}> |
| 56 | + <form onSubmit={methods.handleSubmit(FormSubmitHandler)}> |
| 57 | + <Stack |
| 58 | + spacing={4} |
| 59 | + p="2rem" |
| 60 | + backgroundColor={bgFormColor} |
| 61 | + boxShadow="md" |
| 62 | + borderRadius="base" |
| 63 | + > |
| 64 | + <FormControl isRequired isInvalid={!!methods.formState.errors?.name}> |
| 65 | + <InputGroup> |
| 66 | + <InputLeftElement pointerEvents="none"> |
| 67 | + <Icon as={FaUserAlt} w={4} h={4} color={iconColor} /> |
| 68 | + </InputLeftElement> |
| 69 | + <Input |
| 70 | + type="text" |
| 71 | + placeholder="Nom" |
| 72 | + variant="flushed" |
| 73 | + focusBorderColor="pink.400" |
| 74 | + {...methods.register(`name`)} |
| 75 | + /> |
| 76 | + <FormErrorMessage> |
| 77 | + {methods.formState.errors.email && `erreur email`} |
| 78 | + </FormErrorMessage> |
| 79 | + </InputGroup> |
| 80 | + </FormControl> |
| 81 | + |
| 82 | + <FormControl isRequired isInvalid={!!methods.formState.errors?.email}> |
| 83 | + <InputGroup> |
| 84 | + <InputLeftElement pointerEvents="none"> |
| 85 | + <Icon as={IoMail} w={5} h={5} color={iconColor} /> |
| 86 | + </InputLeftElement> |
| 87 | + <Input |
| 88 | + type="email" |
| 89 | + placeholder="E-mail" |
| 90 | + variant="flushed" |
| 91 | + focusBorderColor="pink.400" |
| 92 | + {...methods.register(`email`)} |
| 93 | + /> |
| 94 | + <FormErrorMessage> |
| 95 | + {methods.formState.errors.email && `erreur email`} |
| 96 | + </FormErrorMessage> |
| 97 | + </InputGroup> |
| 98 | + </FormControl> |
| 99 | + |
| 100 | + <PasswordInputs confirmation={false} iconColor={iconColor} /> |
| 101 | + |
| 102 | + <FormButton |
| 103 | + label="Inscription" |
| 104 | + isLoading={isLoading} |
| 105 | + status={status} |
| 106 | + /> |
| 107 | + {error && <FormErrors error={error as FetchBaseQueryError} />} |
| 108 | + </Stack> |
| 109 | + </form> |
| 110 | + </FormProvider> |
| 111 | + ); |
| 112 | +}; |
0 commit comments