Files
konstitisyon.nu/app/register/form.js
T

56 lines
1.7 KiB
JavaScript
Raw Normal View History

2024-05-20 04:13:44 +04:00
'use client'
import {useState} from 'react'
import AuthForm from '@/components/auth-form/index.js'
import AuthAlert from '@/components/auth-form/auth-alert.js'
export default function RegistrationForm() {
const [error, setError] = useState('')
2024-07-02 19:58:15 +02:00
const [success, setSuccess] = useState('')
const [isSuccessAlertOpen, setIsSuccessAlertOpen] = useState(false)
const [isErrorAlertOpen, setIsErrorAlertOpen] = useState(false)
2024-05-20 04:13:44 +04:00
const handleFormSubmit = async data => {
2024-07-02 19:58:15 +02:00
setIsSuccessAlertOpen(false)
setIsErrorAlertOpen(false)
if (data.password_verification && data.password_verification !== data.password) {
setError('Les mots de passe ne correspondent pas !')
2024-07-02 19:58:15 +02:00
setIsErrorAlertOpen(true)
return
}
2024-05-20 04:13:44 +04:00
const response = await fetch('/api/auth/register', {
method: 'POST',
body: JSON.stringify({
...data
})
})
if (response.status === 201) {
2024-07-02 19:58:15 +02:00
setSuccess('Votre compte est en attente de validation !')
setIsSuccessAlertOpen(true)
2024-05-20 04:13:44 +04:00
} else {
const errorResponse = await response.json()
setError(errorResponse.message)
2024-07-02 19:58:15 +02:00
setIsErrorAlertOpen(true)
2024-05-20 04:13:44 +04:00
}
}
return (
<>
2024-07-02 19:58:15 +02:00
{error && <AuthAlert isOpen={isErrorAlertOpen} setIsOpen={setIsErrorAlertOpen} message={error} severity='error' />}
{success && <AuthAlert isOpen={isSuccessAlertOpen} setIsOpen={setIsSuccessAlertOpen} message={success} severity='success' />}
2024-05-20 04:13:44 +04:00
<AuthForm
isRegister
title='Inscrivez-vous et devenez constituant'
buttonText='Senregistrer'
linkDescription='Vous avez déjà un compte ?'
linkText='Se connecter'
linkHref='/login'
onSubmit={handleFormSubmit}
/>
</>
)
}