modal-form.tsx 3.1 KB

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