modal-form.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 = (
  24. event: React.ChangeEvent<HTMLFormElement & FormControl>
  25. ) => {
  26. setEmail(event.target.value)
  27. }
  28. const handlePasswordChange = (
  29. event: React.ChangeEvent<HTMLFormElement & FormControl>
  30. ) => {
  31. setPassword(event.target.value)
  32. }
  33. const handleConfirmationChange = () => {
  34. setConfirmation(prev => !prev)
  35. }
  36. const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
  37. event.preventDefault()
  38. if (!isFormValid) {
  39. return
  40. }
  41. setError(null)
  42. setInFlight(true)
  43. postJSON('/user/delete', {
  44. body: {
  45. password,
  46. },
  47. })
  48. .then(() => {
  49. window.location.assign('/login')
  50. })
  51. .catch(setError)
  52. .finally(() => {
  53. setInFlight(false)
  54. })
  55. }
  56. useEffect(() => {
  57. setIsFormValid(
  58. !!email &&
  59. email.toLowerCase() === userDefaultEmail.toLowerCase() &&
  60. password.length > 0 &&
  61. confirmation
  62. )
  63. }, [setIsFormValid, userDefaultEmail, email, password, confirmation])
  64. return (
  65. <form id="leave-form" onSubmit={handleSubmit}>
  66. <FormGroup>
  67. <ControlLabel htmlFor="email-input">{t('email')}</ControlLabel>
  68. <FormControl
  69. id="email-input"
  70. type="text"
  71. placeholder={t('email')}
  72. required
  73. value={email}
  74. onChange={handleEmailChange}
  75. />
  76. </FormGroup>
  77. <FormGroup>
  78. <ControlLabel htmlFor="password-input">{t('password')}</ControlLabel>
  79. <FormControl
  80. id="password-input"
  81. type="password"
  82. placeholder={t('password')}
  83. required
  84. value={password}
  85. onChange={handlePasswordChange}
  86. />
  87. </FormGroup>
  88. <Checkbox
  89. required
  90. checked={confirmation}
  91. onChange={handleConfirmationChange}
  92. >
  93. <Trans
  94. i18nKey="delete_account_confirmation_label"
  95. components={[<i />]} // eslint-disable-line react/jsx-key
  96. values={{
  97. userDefaultEmail,
  98. }}
  99. />
  100. </Checkbox>
  101. {error ? <LeaveModalFormError error={error} /> : null}
  102. </form>
  103. )
  104. }
  105. export default LeaveModalForm