import {useState, forwardRef} from 'react' import PropTypes from 'prop-types' import axios from 'axios' import {useRouter} from 'next/navigation' import {FormControl, Snackbar, IconButton, Button, Input, InputAdornment, InputLabel, Box, Container, Typography, LinearProgress} from '@mui/material' import MuiAlert from '@mui/material/Alert' import Visibility from '@mui/icons-material/Visibility' import VisibilityOff from '@mui/icons-material/VisibilityOff' import Link from '@mui/material/Link' const API_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:1337' const Alert = forwardRef(function Alert(props, ref) { return }) export default function NewPassword({code}) { const router = useRouter() const [showPassword, setShowPassword] = useState('') const [showPasswordConfirmation, setShowPasswordConfirmation] = useState('') const [password, setPassword] = useState('') const [passwordConfirmation, setPasswordConfirmation] = useState('') const [loading, setLoading] = useState(false) const [error, setError] = useState('') const [open, setOpen] = useState(true) const [passwordSuccess, setPasswordSuccess] = useState(false) const resetForm = () => { setPassword('') setPasswordConfirmation('') } const changePassword = async () => { setLoading(true) try { await axios.post(`${API_URL}/auth/reset-password`, { code, password, passwordConfirmation }) setLoading(false) setPasswordSuccess(true) setTimeout(() => { router.push('/pwopose') }, 10_000) } catch { setOpen(true) setError('Une erreur s’est produite. Veuillez réessayer ultérieurement') } } const handleMouseDownPassword = event => { event.preventDefault() } const handleMouseDownPasswordConfirmation = event => { event.preventDefault() } const handleKeyUpPassword = event => { if (event.keyCode === 13) { handleClick() } } const handleKeyUpPasswordConfirmation = event => { if (event.keyCode === 13) { handleClick() } } const handleClick = async () => { if (password !== passwordConfirmation) { setOpen(true) setError('Les 2 mots de passe de correspondent pas') return } if (password.length < 6) { setOpen(true) setError('Le mot de passe est trop court, 6 caratères minimum') return } await changePassword() setLoading(false) resetForm() } const handleClose = (event, reason) => { if (reason === 'clickaway') { return } setOpen(false) setError('') } return ( Nouveau mot de passe Mot de passe setShowPassword(!showPassword)} onMouseDown={handleMouseDownPassword} > {showPassword ? : } } onChange={event => setPassword(event.target.value)} onKeyUp={handleKeyUpPassword} /> Vérification du mot de passe setShowPasswordConfirmation(!showPassword)} onMouseDown={handleMouseDownPasswordConfirmation} > {showPasswordConfirmation ? : } } onChange={event => setPasswordConfirmation(event.target.value)} onKeyUp={handleKeyUpPasswordConfirmation} /> {loading && } {error && ( {error} )} {passwordSuccess && ( Votre changement de mot de passe a été pris en compte. Vous serez redirigé vers la page de connexion. Cliquez ici si vous n’êtes pas redirigé vers la page de connexion )} ) } NewPassword.propTypes = { code: PropTypes.string.isRequired }