mirror of
https://github.com/LucasVbr/meeting-app.git
synced 2026-07-09 15:08:06 +00:00
Refactor Register
Took 1 hour 8 minutes
This commit is contained in:
@@ -9,7 +9,7 @@ import {
|
|||||||
} from '@chakra-ui/react';
|
} from '@chakra-ui/react';
|
||||||
import {useState} from 'react';
|
import {useState} from 'react';
|
||||||
import {useRouter} from 'next/router';
|
import {useRouter} from 'next/router';
|
||||||
import {signIn, SignInResponse,} from 'next-auth/react';
|
import {signIn, SignInResponse} from 'next-auth/react';
|
||||||
|
|
||||||
import {LoginData} from '@/models/form/LoginData';
|
import {LoginData} from '@/models/form/LoginData';
|
||||||
|
|
||||||
@@ -20,20 +20,18 @@ export default function LoginForm() {
|
|||||||
|
|
||||||
const buttonWidth = {base: '100%', md: 'unset'};
|
const buttonWidth = {base: '100%', md: 'unset'};
|
||||||
|
|
||||||
const handleInput = ({email = loginData.email, password = loginData.password}) => {
|
const handleInput = (data: any) => {
|
||||||
setInvalidInput(false);
|
setInvalidInput(false);
|
||||||
setLoginData(new LoginData(email, password));
|
setLoginData({...loginData, ...data});
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleSubmit = async () => {
|
const handleSubmit = async () => {
|
||||||
const {email, password} = loginData;
|
|
||||||
await signIn('credentials',
|
await signIn('credentials',
|
||||||
{email, password, redirect: false})
|
{...loginData, redirect: false}).then((res) => {
|
||||||
.then((res) => {
|
const {ok} = res as SignInResponse;
|
||||||
const {ok} = res as SignInResponse
|
|
||||||
|
|
||||||
if (!ok) setInvalidInput(true)
|
if (!ok) setInvalidInput(true);
|
||||||
else router.push("/")
|
else router.push('/');
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -43,22 +41,23 @@ export default function LoginForm() {
|
|||||||
<Heading textAlign={'center'} size={'2xl'}>Connexion</Heading>
|
<Heading textAlign={'center'} size={'2xl'}>Connexion</Heading>
|
||||||
|
|
||||||
{/* Email */}
|
{/* Email */}
|
||||||
<FormControl mt={'100px'} isInvalid={invalidInput}>
|
<FormControl mb={'1rem'} mt={'100px'} isInvalid={invalidInput}>
|
||||||
<Box mb={'1rem'}>
|
|
||||||
<FormLabel>Adresse email</FormLabel>
|
|
||||||
<Input
|
|
||||||
isInvalid={invalidInput}
|
|
||||||
id={'email'}
|
|
||||||
type={'email'}
|
|
||||||
value={loginData.email}
|
|
||||||
onChange={(evt) => handleInput({email: evt.target.value})}
|
|
||||||
placeholder={'adresse@email.com'}
|
|
||||||
required={true}
|
|
||||||
/>
|
|
||||||
</Box>
|
|
||||||
|
|
||||||
{/* Mot de passe */}
|
<FormLabel>Adresse email</FormLabel>
|
||||||
<Box mb={'1rem'}>
|
<Input
|
||||||
|
isInvalid={invalidInput}
|
||||||
|
id={'email'}
|
||||||
|
type={'email'}
|
||||||
|
value={loginData.email}
|
||||||
|
onChange={(evt) => handleInput({email: evt.target.value})}
|
||||||
|
placeholder={'adresse@email.com'}
|
||||||
|
required={true}
|
||||||
|
/>
|
||||||
|
</FormControl>
|
||||||
|
|
||||||
|
{/* Mot de passe */
|
||||||
|
}
|
||||||
|
<FormControl mb={'1rem'}>
|
||||||
<FormLabel>Mot de passe</FormLabel>
|
<FormLabel>Mot de passe</FormLabel>
|
||||||
<Input
|
<Input
|
||||||
isInvalid={invalidInput}
|
isInvalid={invalidInput}
|
||||||
@@ -69,19 +68,19 @@ export default function LoginForm() {
|
|||||||
placeholder={'Mot de passe'}
|
placeholder={'Mot de passe'}
|
||||||
required={true}
|
required={true}
|
||||||
/>
|
/>
|
||||||
</Box>
|
|
||||||
|
|
||||||
{/*Boutons*/}
|
|
||||||
<Flex justify={'center'} mt={'50px'} gap={5}
|
|
||||||
justifyContent={'space-between'}>
|
|
||||||
<Button onClick={() => router.push('/')}
|
|
||||||
w={buttonWidth}>Retour</Button>
|
|
||||||
<Button onClick={handleSubmit}
|
|
||||||
w={buttonWidth} colorScheme="purple">Connexion</Button>
|
|
||||||
</Flex>
|
|
||||||
|
|
||||||
</FormControl>
|
</FormControl>
|
||||||
</Container>
|
|
||||||
</Box>
|
{/*Boutons*/
|
||||||
);
|
}
|
||||||
|
<Flex justify={'center'} mt={'50px'} gap={5}
|
||||||
|
justifyContent={'space-between'}>
|
||||||
|
<Button onClick={() => router.push('/')}
|
||||||
|
w={buttonWidth}>Retour</Button>
|
||||||
|
<Button onClick={handleSubmit}
|
||||||
|
w={buttonWidth} colorScheme="purple">Connexion</Button>
|
||||||
|
</Flex>
|
||||||
|
</Container>
|
||||||
|
</Box>
|
||||||
|
)
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,120 @@
|
|||||||
|
import {
|
||||||
|
Box, Button,
|
||||||
|
Flex,
|
||||||
|
FormControl,
|
||||||
|
FormLabel,
|
||||||
|
Heading,
|
||||||
|
Input,
|
||||||
|
Container,
|
||||||
|
} from '@chakra-ui/react';
|
||||||
|
import {useRouter} from 'next/router';
|
||||||
|
import {useState} from 'react';
|
||||||
|
import {RegisterData} from '@/models/form/RegisterData';
|
||||||
|
|
||||||
|
export default function RegisterForm() {
|
||||||
|
const router = useRouter();
|
||||||
|
const [registerData, setRegisterData] = useState(new RegisterData());
|
||||||
|
const [invalidInput, setInvalidInput] = useState(false);
|
||||||
|
|
||||||
|
const buttonWidth = {base: '100%', md: 'unset'};
|
||||||
|
|
||||||
|
const handleInput = (data: any) => {
|
||||||
|
setInvalidInput(false);
|
||||||
|
setRegisterData({...registerData, ...data});
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleSubmit = () => {
|
||||||
|
const {password, confirmPassword} = registerData;
|
||||||
|
if (password !== confirmPassword) setInvalidInput(true);
|
||||||
|
|
||||||
|
fetch('/api/user/new', {
|
||||||
|
method: 'PUT',
|
||||||
|
headers: {'Content-Type': 'application/json'},
|
||||||
|
body: JSON.stringify(registerData),
|
||||||
|
}).then(() => router.push('/')).catch(() => {
|
||||||
|
setInvalidInput(true);
|
||||||
|
setRegisterData({...registerData, password: '', confirmPassword: ''});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Box flexBasis={"100%"}>
|
||||||
|
<Container>
|
||||||
|
<Heading textAlign={"center"} size={"2xl"}>Inscription</Heading>
|
||||||
|
|
||||||
|
<Flex mt={"100px"} mb={'1rem'} gap={5}>
|
||||||
|
{/*Prénom*/}
|
||||||
|
<FormControl>
|
||||||
|
<FormLabel>Prénom</FormLabel>
|
||||||
|
<Input
|
||||||
|
id="firstName"
|
||||||
|
type="text"
|
||||||
|
value={registerData.firstName}
|
||||||
|
onChange={(evt) => handleInput({firstName: evt.target.value})}
|
||||||
|
placeholder="Prénom"
|
||||||
|
required={true}
|
||||||
|
/>
|
||||||
|
</FormControl>
|
||||||
|
|
||||||
|
{/*Nom*/}
|
||||||
|
<FormControl>
|
||||||
|
<FormLabel>Nom</FormLabel>
|
||||||
|
<Input
|
||||||
|
id="lastName"
|
||||||
|
type="text"
|
||||||
|
value={registerData.lastName}
|
||||||
|
onChange={(evt) => handleInput({lastName: evt.target.value})}
|
||||||
|
placeholder="Nom"
|
||||||
|
required={true}
|
||||||
|
/>
|
||||||
|
</FormControl>
|
||||||
|
</Flex>
|
||||||
|
|
||||||
|
{/*Email*/}
|
||||||
|
<FormControl mb={'1rem'} isInvalid={invalidInput}>
|
||||||
|
<FormLabel>Adresse email</FormLabel>
|
||||||
|
<Input
|
||||||
|
id="email"
|
||||||
|
type="email"
|
||||||
|
value={registerData.email}
|
||||||
|
onChange={(evt) => handleInput({email: evt.target.value})}
|
||||||
|
placeholder="adresse@email.com"
|
||||||
|
/>
|
||||||
|
</FormControl>
|
||||||
|
|
||||||
|
{/*Mot de passe*/}
|
||||||
|
<FormControl mb={'1rem'} isInvalid={invalidInput}>
|
||||||
|
<FormLabel>Mot de passe</FormLabel>
|
||||||
|
<Input
|
||||||
|
id="password"
|
||||||
|
type="password"
|
||||||
|
value={registerData.password}
|
||||||
|
onChange={(evt) => handleInput({password: evt.target.value})}
|
||||||
|
placeholder="Mot de passe"
|
||||||
|
/>
|
||||||
|
</FormControl>
|
||||||
|
|
||||||
|
{/*Mot de passe (2)*/}
|
||||||
|
<FormControl mb={'1rem'} isInvalid={invalidInput}>
|
||||||
|
<FormLabel>Confirmation du mot de passe</FormLabel>
|
||||||
|
<Input
|
||||||
|
id="confirmPassword"
|
||||||
|
type="password"
|
||||||
|
value={registerData.confirmPassword}
|
||||||
|
onChange={(evt) => handleInput(
|
||||||
|
{confirmPassword: evt.target.value})}
|
||||||
|
placeholder="Mot de passe"
|
||||||
|
/>
|
||||||
|
</FormControl>
|
||||||
|
|
||||||
|
|
||||||
|
<Flex mt={'50px'} w={'100%'} justify={'space-between'} gap={5}>
|
||||||
|
<Button onClick={() => router.push('/')} w={buttonWidth}>Retour</Button>
|
||||||
|
<Button onClick={handleSubmit} w={buttonWidth} colorScheme="purple">Je
|
||||||
|
m'inscris</Button>
|
||||||
|
</Flex>
|
||||||
|
</Container>
|
||||||
|
</Box>
|
||||||
|
);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
export class RegisterData {
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
public firstName: string = "",
|
||||||
|
public lastName: string = "",
|
||||||
|
public email: string = "",
|
||||||
|
public password: string = "",
|
||||||
|
public confirmPassword: string = "",
|
||||||
|
) {}
|
||||||
|
}
|
||||||
+9
-141
@@ -12,150 +12,18 @@ import {
|
|||||||
} from '@chakra-ui/react';
|
} from '@chakra-ui/react';
|
||||||
import {useRouter} from 'next/router';
|
import {useRouter} from 'next/router';
|
||||||
import {useForm} from 'react-hook-form';
|
import {useForm} from 'react-hook-form';
|
||||||
|
import RegisterForm from '@/components/form/RegisterForm';
|
||||||
type FormValues = {
|
|
||||||
firstName: string
|
|
||||||
lastName: string
|
|
||||||
email: string
|
|
||||||
password: string
|
|
||||||
confirmPassword: string
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function Register() {
|
export default function Register() {
|
||||||
const {
|
|
||||||
handleSubmit,
|
|
||||||
register,
|
|
||||||
formState: {errors, isSubmitting},
|
|
||||||
} = useForm();
|
|
||||||
|
|
||||||
const router = useRouter();
|
|
||||||
|
|
||||||
const redirect_home = () => {
|
|
||||||
router.push('/');
|
|
||||||
};
|
|
||||||
|
|
||||||
const onRegister = async (values: FormValues) => {
|
|
||||||
// Verify password
|
|
||||||
if (values.password !== values.confirmPassword) {
|
|
||||||
alert('Les mots de passe ne correspondent pas');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create User
|
|
||||||
fetch('/api/user/', {
|
|
||||||
method: 'PUT',
|
|
||||||
headers: {'Content-Type': 'application/json'},
|
|
||||||
body: JSON.stringify(values),
|
|
||||||
}).then(res => res.json()).then(data => {
|
|
||||||
if (data.error) throw new Error();
|
|
||||||
alert('Inscription réussie');
|
|
||||||
router.push('/');
|
|
||||||
}).catch(err => console.error(err));
|
|
||||||
};
|
|
||||||
|
|
||||||
const RightSide = () => (
|
|
||||||
<Flex
|
|
||||||
justify={'center'}
|
|
||||||
direction={'column'}
|
|
||||||
flexBasis={'100%'}
|
|
||||||
align={'center'}
|
|
||||||
>
|
|
||||||
<Heading mb={'2.5rem'}>Inscription</Heading>
|
|
||||||
<Box w={'25vw'}>
|
|
||||||
<form onSubmit={handleSubmit(onRegister)}>
|
|
||||||
<FormControl isInvalid={errors.name}>
|
|
||||||
<Flex mb={'1rem'} gap={5}>
|
|
||||||
<FormControl>
|
|
||||||
<FormLabel>Prénom</FormLabel>
|
|
||||||
<Input
|
|
||||||
id="firstName"
|
|
||||||
type="text"
|
|
||||||
placeholder="Prénom"
|
|
||||||
{...register('firstName', {
|
|
||||||
required: 'This is required',
|
|
||||||
})}
|
|
||||||
/>
|
|
||||||
</FormControl>
|
|
||||||
<FormControl>
|
|
||||||
<FormLabel>Nom</FormLabel>
|
|
||||||
<Input
|
|
||||||
id="lastName"
|
|
||||||
type="text"
|
|
||||||
placeholder="Nom"
|
|
||||||
{...register('lastName', {
|
|
||||||
required: 'This is required',
|
|
||||||
})}
|
|
||||||
/>
|
|
||||||
</FormControl>
|
|
||||||
</Flex>
|
|
||||||
<FormControl mb={'1rem'}>
|
|
||||||
<FormLabel>Adresse email</FormLabel>
|
|
||||||
<Input
|
|
||||||
id="email"
|
|
||||||
type="email"
|
|
||||||
placeholder="Adresse@email.com"
|
|
||||||
{...register('email', {
|
|
||||||
required: 'This is required',
|
|
||||||
})}
|
|
||||||
/>
|
|
||||||
</FormControl>
|
|
||||||
<FormControl mb={'1rem'}>
|
|
||||||
<FormLabel>Mot de passe</FormLabel>
|
|
||||||
<Input
|
|
||||||
id="password"
|
|
||||||
type="password"
|
|
||||||
placeholder="Mot de passe"
|
|
||||||
{...register('password', {
|
|
||||||
required: 'This is required',
|
|
||||||
})}
|
|
||||||
/>
|
|
||||||
</FormControl>
|
|
||||||
{/* Il y a une margin en trop mais je me suis dit que c'etais mieux d'avoir plus d'expace entre les deux */}
|
|
||||||
<FormControl mb={'1rem'}>
|
|
||||||
<FormLabel>Confirmation du mot de passe</FormLabel>
|
|
||||||
<Input
|
|
||||||
id="confirmPassword"
|
|
||||||
type="password"
|
|
||||||
placeholder="Mot de passe"
|
|
||||||
{...register('confirmPassword', {
|
|
||||||
required: 'This is required',
|
|
||||||
})}
|
|
||||||
/>
|
|
||||||
</FormControl>
|
|
||||||
|
|
||||||
<FormErrorMessage>
|
|
||||||
{/*{errors.name && errors.name.message}*/}
|
|
||||||
</FormErrorMessage>
|
|
||||||
</FormControl>
|
|
||||||
<Flex mt={'1rem'} w={'100%'}>
|
|
||||||
<Button onClick={redirect_home}>Retour</Button>
|
|
||||||
<Spacer/>
|
|
||||||
<Button colorScheme="purple" type="submit" isLoading={isSubmitting}>
|
|
||||||
Je m'inscris
|
|
||||||
</Button>
|
|
||||||
</Flex>
|
|
||||||
</form>
|
|
||||||
</Box>
|
|
||||||
</Flex>
|
|
||||||
);
|
|
||||||
|
|
||||||
const LeftSide = () => (
|
|
||||||
<Box flexBasis={'100%'}>
|
|
||||||
<Image
|
|
||||||
h={'100vh'}
|
|
||||||
w={'100%'}
|
|
||||||
src={'/couple_holding_hands.png'}
|
|
||||||
alt="couple holding hands"
|
|
||||||
/>
|
|
||||||
</Box>
|
|
||||||
);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box>
|
<Box>
|
||||||
<Flex gap={10}>
|
<Flex gap={10} h={'100vh'} alignItems={'center'}>
|
||||||
<LeftSide/>
|
<Box display={{base: 'none', md: 'block'}} h={'100%'}
|
||||||
<RightSide/>
|
flexBasis={'100%'} bgImage={'/couple_holding_hands.png'}
|
||||||
</Flex>
|
bgPos={'center'} bgSize={'cover'}/>
|
||||||
</Box>
|
<RegisterForm/>
|
||||||
|
</Flex>
|
||||||
|
</Box>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user