| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- import { useState, useEffect, Dispatch, SetStateAction } from 'react'
- import { Checkbox, ControlLabel, FormControl, FormGroup } from 'react-bootstrap'
- import { useTranslation, Trans } from 'react-i18next'
- import { postJSON, FetchError } from '../../../../infrastructure/fetch-json'
- import getMeta from '../../../../utils/meta'
- import LeaveModalFormError from './modal-form-error'
- export type LeaveModalFormProps = {
- setInFlight: Dispatch<SetStateAction<boolean>>
- isFormValid: boolean
- setIsFormValid: Dispatch<SetStateAction<boolean>>
- }
- function LeaveModalForm({
- setInFlight,
- isFormValid,
- setIsFormValid,
- }: LeaveModalFormProps) {
- const { t } = useTranslation()
- const userDefaultEmail = getMeta('ol-usersEmail') as string
- const [email, setEmail] = useState('')
- const [password, setPassword] = useState('')
- const [confirmation, setConfirmation] = useState(false)
- const [error, setError] = useState<FetchError | null>(null)
- const handleEmailChange = event => {
- setEmail(event.target.value)
- }
- const handlePasswordChange = event => {
- setPassword(event.target.value)
- }
- const handleConfirmationChange = () => {
- setConfirmation(prev => !prev)
- }
- const handleSubmit = event => {
- event.preventDefault()
- if (!isFormValid) {
- return
- }
- setError(null)
- setInFlight(true)
- postJSON('/user/delete', {
- body: {
- password,
- },
- })
- .then(() => {
- window.location.assign('/login')
- })
- .catch(setError)
- .finally(() => {
- setInFlight(false)
- })
- }
- useEffect(() => {
- setIsFormValid(
- !!email &&
- email === userDefaultEmail &&
- password.length > 0 &&
- confirmation
- )
- }, [setIsFormValid, userDefaultEmail, email, password, confirmation])
- return (
- <form id="leave-form" onSubmit={handleSubmit}>
- <FormGroup>
- <ControlLabel htmlFor="email-input">{t('email')}</ControlLabel>
- <FormControl
- id="email-input"
- type="text"
- placeholder={t('email')}
- required
- value={email}
- onChange={handleEmailChange}
- />
- </FormGroup>
- <FormGroup>
- <ControlLabel htmlFor="password-input">{t('password')}</ControlLabel>
- <FormControl
- id="password-input"
- type="password"
- placeholder={t('password')}
- required
- value={password}
- onChange={handlePasswordChange}
- />
- </FormGroup>
- <Checkbox
- required
- checked={confirmation}
- onChange={handleConfirmationChange}
- >
- <Trans
- i18nKey="delete_account_confirmation_label"
- components={[<i />]} // eslint-disable-line react/jsx-key
- values={{
- userDefaultEmail,
- }}
- />
- </Checkbox>
- {error ? <LeaveModalFormError error={error} /> : null}
- </form>
- )
- }
- export default LeaveModalForm
|