register-form.jsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { useState } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import PropTypes from 'prop-types'
  4. import { postJSON } from '@/infrastructure/fetch-json'
  5. import OLButton from '@/shared/components/ol/ol-button'
  6. import OLForm from '@/shared/components/ol/ol-form'
  7. import OLFormControl from '@/shared/components/ol/ol-form-control'
  8. import OLFormLabel from '@/shared/components/ol/ol-form-label'
  9. import OLFormText from '@/shared/components/ol/ol-form-text'
  10. import OLCol from '@/shared/components/ol/ol-col'
  11. import OLRow from '@/shared/components/ol/ol-row'
  12. function RegisterForm({
  13. setRegistrationSuccess,
  14. setEmails,
  15. setRegisterError,
  16. setFailedEmails,
  17. }) {
  18. const [isLoading, setIsLoading] = useState(false)
  19. const { t } = useTranslation()
  20. function handleRegister(event) {
  21. event.preventDefault()
  22. const formData = new FormData(event.target)
  23. const formDataAsEntries = formData.entries()
  24. const formDataAsObject = Object.fromEntries(formDataAsEntries)
  25. const emailString = formDataAsObject.email
  26. setRegistrationSuccess(false)
  27. setRegisterError(false)
  28. setEmails([])
  29. registerGivenUsers(parseEmails(emailString))
  30. }
  31. async function registerGivenUsers(emails) {
  32. const registeredEmails = []
  33. const failingEmails = []
  34. setIsLoading(true)
  35. for (const email of emails) {
  36. try {
  37. const result = await registerUser(email)
  38. registeredEmails.push(result)
  39. } catch {
  40. failingEmails.push(email)
  41. }
  42. }
  43. setIsLoading(false)
  44. if (registeredEmails.length > 0) setRegistrationSuccess(true)
  45. if (failingEmails.length > 0) {
  46. setRegisterError(true)
  47. setFailedEmails(failingEmails)
  48. }
  49. setEmails(registeredEmails)
  50. }
  51. function registerUser(email) {
  52. const options = { email }
  53. const url = `/admin/register`
  54. return postJSON(url, { body: options })
  55. }
  56. return (
  57. <OLForm onSubmit={handleRegister}>
  58. <OLRow>
  59. <OLCol lg={8}>
  60. <OLFormLabel htmlFor="register-new-user-email">
  61. Emails to register new users
  62. </OLFormLabel>
  63. <OLFormControl
  64. id="register-new-user-email"
  65. name="email"
  66. type="text"
  67. aria-describedby="register-new-user-email-helper"
  68. />
  69. <OLFormText id="register-new-user-email-helper">
  70. {t('add_comma_separated_emails_help')}
  71. </OLFormText>
  72. </OLCol>
  73. <OLCol
  74. lg={4}
  75. className="mt-3 mt-lg-0 d-flex align-items-center flex-column flex-lg-row"
  76. >
  77. <OLButton
  78. type="submit"
  79. isLoading={isLoading}
  80. loadingLabel={t('registering')}
  81. >
  82. Register
  83. </OLButton>
  84. </OLCol>
  85. </OLRow>
  86. </OLForm>
  87. )
  88. }
  89. function parseEmails(emailsText) {
  90. const regexBySpaceOrComma = /[\s,]+/
  91. let emails = emailsText.split(regexBySpaceOrComma)
  92. emails.map(email => email.trim())
  93. emails = emails.filter(email => email.indexOf('@') !== -1)
  94. return emails
  95. }
  96. RegisterForm.propTypes = {
  97. setRegistrationSuccess: PropTypes.func,
  98. setEmails: PropTypes.func,
  99. setRegisterError: PropTypes.func,
  100. setFailedEmails: PropTypes.func,
  101. }
  102. export default RegisterForm