Improve register responses states
This commit is contained in:
+12
-8
@@ -1,19 +1,22 @@
|
|||||||
'use client'
|
'use client'
|
||||||
|
|
||||||
import {useRouter} from 'next/navigation'
|
|
||||||
import {useState} from 'react'
|
import {useState} from 'react'
|
||||||
import AuthForm from '@/components/auth-form/index.js'
|
import AuthForm from '@/components/auth-form/index.js'
|
||||||
import AuthAlert from '@/components/auth-form/auth-alert.js'
|
import AuthAlert from '@/components/auth-form/auth-alert.js'
|
||||||
|
|
||||||
export default function RegistrationForm() {
|
export default function RegistrationForm() {
|
||||||
const router = useRouter()
|
|
||||||
const [error, setError] = useState('')
|
const [error, setError] = useState('')
|
||||||
const [isOpen, setIsOpen] = useState(false)
|
const [success, setSuccess] = useState('')
|
||||||
|
const [isSuccessAlertOpen, setIsSuccessAlertOpen] = useState(false)
|
||||||
|
const [isErrorAlertOpen, setIsErrorAlertOpen] = useState(false)
|
||||||
|
|
||||||
const handleFormSubmit = async data => {
|
const handleFormSubmit = async data => {
|
||||||
|
setIsSuccessAlertOpen(false)
|
||||||
|
setIsErrorAlertOpen(false)
|
||||||
|
|
||||||
if (data.password_verification && data.password_verification !== data.password) {
|
if (data.password_verification && data.password_verification !== data.password) {
|
||||||
setError('Les mots de passe ne correspondent pas !')
|
setError('Les mots de passe ne correspondent pas !')
|
||||||
setIsOpen(true)
|
setIsErrorAlertOpen(true)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -25,18 +28,19 @@ export default function RegistrationForm() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
if (response.status === 201) {
|
if (response.status === 201) {
|
||||||
router.push('/login')
|
setSuccess('Votre compte est en attente de validation !')
|
||||||
router.refresh()
|
setIsSuccessAlertOpen(true)
|
||||||
} else {
|
} else {
|
||||||
const errorResponse = await response.json()
|
const errorResponse = await response.json()
|
||||||
setError(errorResponse.message)
|
setError(errorResponse.message)
|
||||||
setIsOpen(true)
|
setIsErrorAlertOpen(true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{error && <AuthAlert isOpen={isOpen} setIsOpen={setIsOpen} message={error} severity='error' />}
|
{error && <AuthAlert isOpen={isErrorAlertOpen} setIsOpen={setIsErrorAlertOpen} message={error} severity='error' />}
|
||||||
|
{success && <AuthAlert isOpen={isSuccessAlertOpen} setIsOpen={setIsSuccessAlertOpen} message={success} severity='success' />}
|
||||||
<AuthForm
|
<AuthForm
|
||||||
isRegister
|
isRegister
|
||||||
title='Inscrivez-vous et devenez constituant'
|
title='Inscrivez-vous et devenez constituant'
|
||||||
|
|||||||
Reference in New Issue
Block a user