modal-form.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { useState, useEffect, Dispatch, SetStateAction } from 'react'
  2. import { Checkbox, ControlLabel, FormControl, FormGroup } from 'react-bootstrap'
  3. import { useTranslation, Trans } from 'react-i18next'
  4. import { postJSON, FetchError } from '../../../../infrastructure/fetch-json'
  5. import getMeta from '../../../../utils/meta'
  6. import LeaveModalFormError from './modal-form-error'
  7. export type LeaveModalFormProps = {
  8. setInFlight: Dispatch<SetStateAction<boolean>>
  9. isFormValid: boolean
  10. setIsFormValid: Dispatch<SetStateAction<boolean>>
  11. }
  12. function LeaveModalForm({
  13. setInFlight,
  14. isFormValid,
  15. setIsFormValid,
  16. }: LeaveModalFormProps) {
  17. const { t } = useTranslation()
  18. const userDefaultEmail = getMeta('ol-usersEmail') as string
  19. const [email, setEmail] = useState('')
  20. const [password, setPassword] = useState('')
  21. const [confirmation, setConfirmation] = useState(false)
  22. const [error, setError] = useState<FetchError | null>(null)
  23. const handleEmailChange = event => {
  24. setEmail(event.target.value)
  25. }
  26. const handlePasswordChange = event => {
  27. setPassword(event.target.value)
  28. }
  29. const handleConfirmationChange = () => {
  30. setConfirmation(prev => !prev)
  31. }
  32. const handleSubmit = event => {
  33. event.preventDefault()
  34. if (!isFormValid) {
  35. return
  36. }
  37. setError(null)
  38. setInFlight(true)
  39. postJSON('/user/delete', {
  40. body: {
  41. password,
  42. },
  43. })
  44. .then(() => {
  45. window.location.assign('/login')
  46. })
  47. .catch(setError)
  48. .finally(() => {
  49. setInFlight(false)
  50. })
  51. }
  52. useEffect(() => {
  53. setIsFormValid(
  54. !!email &&
  55. email === userDefaultEmail &&
  56. password.length > 0 &&
  57. confirmation
  58. )
  59. }, [setIsFormValid, userDefaultEmail, email, password, confirmation])
  60. return (
  61. <form id="leave-form" onSubmit={handleSubmit}>
  62. <FormGroup>
  63. <ControlLabel htmlFor="email-input">{t('email')}</ControlLabel>
  64. <FormControl
  65. id="email-input"
  66. type="text"
  67. placeholder={t('email')}
  68. required
  69. value={email}
  70. onChange={handleEmailChange}
  71. />
  72. </FormGroup>
  73. <FormGroup>
  74. <ControlLabel htmlFor="password-input">{t('password')}</ControlLabel>
  75. <FormControl
  76. id="password-input"
  77. type="password"
  78. placeholder={t('password')}
  79. required
  80. value={password}
  81. onChange={handlePasswordChange}
  82. />
  83. </FormGroup>
  84. <Checkbox
  85. required
  86. checked={confirmation}
  87. onChange={handleConfirmationChange}
  88. >
  89. <Trans
  90. i18nKey="delete_account_confirmation_label"
  91. components={[<i />]} // eslint-disable-line react/jsx-key
  92. values={{
  93. userDefaultEmail,
  94. }}
  95. />
  96. </Checkbox>
  97. {error ? <LeaveModalFormError error={error} /> : null}
  98. </form>
  99. )
  100. }
  101. export default LeaveModalForm