modal-form.tsx 3.4 KB

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