account-info-section.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import { useState } from 'react'
  2. import { Alert, ControlLabel, FormControl, FormGroup } from 'react-bootstrap'
  3. import { useTranslation } from 'react-i18next'
  4. import {
  5. getUserFacingMessage,
  6. postJSON,
  7. } from '../../../infrastructure/fetch-json'
  8. import getMeta from '../../../utils/meta'
  9. import { ExposedSettings } from '../../../../../types/exposed-settings'
  10. import useAsync from '../../../shared/hooks/use-async'
  11. import { useUserContext } from '../../../shared/context/user-context'
  12. import ButtonWrapper from '@/features/ui/components/bootstrap-5/wrappers/button-wrapper'
  13. function AccountInfoSection() {
  14. const { t } = useTranslation()
  15. const { hasAffiliationsFeature } = getMeta(
  16. 'ol-ExposedSettings'
  17. ) as ExposedSettings
  18. const isExternalAuthenticationSystemUsed = getMeta(
  19. 'ol-isExternalAuthenticationSystemUsed'
  20. ) as boolean
  21. const shouldAllowEditingDetails = getMeta(
  22. 'ol-shouldAllowEditingDetails'
  23. ) as boolean
  24. const {
  25. first_name: initialFirstName,
  26. last_name: initialLastName,
  27. email: initialEmail,
  28. } = useUserContext()
  29. const [email, setEmail] = useState(initialEmail)
  30. const [firstName, setFirstName] = useState(initialFirstName)
  31. const [lastName, setLastName] = useState(initialLastName)
  32. const { isLoading, isSuccess, isError, error, runAsync } = useAsync()
  33. const [isFormValid, setIsFormValid] = useState(true)
  34. const handleEmailChange = (event: React.ChangeEvent<HTMLInputElement>) => {
  35. setEmail(event.target.value)
  36. setIsFormValid(event.target.validity.valid)
  37. }
  38. const handleFirstNameChange = (
  39. event: React.ChangeEvent<HTMLInputElement>
  40. ) => {
  41. setFirstName(event.target.value)
  42. }
  43. const handleLastNameChange = (event: React.ChangeEvent<HTMLInputElement>) => {
  44. setLastName(event.target.value)
  45. }
  46. const canUpdateEmail =
  47. !hasAffiliationsFeature && !isExternalAuthenticationSystemUsed
  48. const canUpdateNames = shouldAllowEditingDetails
  49. const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
  50. event.preventDefault()
  51. if (!isFormValid) {
  52. return
  53. }
  54. runAsync(
  55. postJSON('/user/settings', {
  56. body: {
  57. email: canUpdateEmail ? email : undefined,
  58. first_name: canUpdateNames ? firstName : undefined,
  59. last_name: canUpdateNames ? lastName : undefined,
  60. },
  61. })
  62. ).catch(() => {})
  63. }
  64. return (
  65. <>
  66. <h3>{t('update_account_info')}</h3>
  67. <form id="account-info-form" onSubmit={handleSubmit}>
  68. {hasAffiliationsFeature ? null : (
  69. <ReadOrWriteFormGroup
  70. id="email-input"
  71. type="email"
  72. label={t('email')}
  73. value={email}
  74. handleChange={handleEmailChange}
  75. canEdit={canUpdateEmail}
  76. required
  77. />
  78. )}
  79. <ReadOrWriteFormGroup
  80. id="first-name-input"
  81. type="text"
  82. label={t('first_name')}
  83. value={firstName}
  84. handleChange={handleFirstNameChange}
  85. canEdit={canUpdateNames}
  86. required={false}
  87. />
  88. <ReadOrWriteFormGroup
  89. id="last-name-input"
  90. type="text"
  91. label={t('last_name')}
  92. value={lastName}
  93. handleChange={handleLastNameChange}
  94. canEdit={canUpdateNames}
  95. required={false}
  96. />
  97. {isSuccess ? (
  98. <FormGroup>
  99. <Alert bsStyle="success">{t('thanks_settings_updated')}</Alert>
  100. </FormGroup>
  101. ) : null}
  102. {isError ? (
  103. <FormGroup>
  104. <Alert bsStyle="danger">{getUserFacingMessage(error)}</Alert>
  105. </FormGroup>
  106. ) : null}
  107. {canUpdateEmail || canUpdateNames ? (
  108. <ButtonWrapper
  109. type="submit"
  110. variant="primary"
  111. form="account-info-form"
  112. isLoading={isLoading || !isFormValid}
  113. bs3Props={{
  114. loading: isLoading ? `${t('saving')}…` : t('update'),
  115. }}
  116. >
  117. {t('update')}
  118. </ButtonWrapper>
  119. ) : null}
  120. </form>
  121. </>
  122. )
  123. }
  124. type ReadOrWriteFormGroupProps = {
  125. id: string
  126. type: string
  127. label: string
  128. value?: string
  129. handleChange: (event: any) => void
  130. canEdit: boolean
  131. required: boolean
  132. }
  133. function ReadOrWriteFormGroup({
  134. id,
  135. type,
  136. label,
  137. value,
  138. handleChange,
  139. canEdit,
  140. required,
  141. }: ReadOrWriteFormGroupProps) {
  142. const [validationMessage, setValidationMessage] = useState('')
  143. const handleInvalid = (
  144. event: React.InvalidEvent<HTMLInputElement & FormControl>
  145. ) => {
  146. event.preventDefault()
  147. }
  148. const handleChangeAndValidity = (
  149. event: React.ChangeEvent<HTMLInputElement & FormControl>
  150. ) => {
  151. handleChange(event)
  152. setValidationMessage(event.target.validationMessage)
  153. }
  154. if (!canEdit) {
  155. return (
  156. <FormGroup>
  157. <ControlLabel htmlFor={id}>{label}</ControlLabel>
  158. <FormControl id={id} type="text" readOnly value={value} />
  159. </FormGroup>
  160. )
  161. }
  162. return (
  163. <FormGroup>
  164. <ControlLabel htmlFor={id}>{label}</ControlLabel>
  165. <FormControl
  166. id={id}
  167. type={type}
  168. required={required}
  169. value={value}
  170. data-ol-dirty={!!validationMessage}
  171. onChange={handleChangeAndValidity}
  172. onInvalid={handleInvalid}
  173. />
  174. {validationMessage ? (
  175. <span className="small text-danger">{validationMessage}</span>
  176. ) : null}
  177. </FormGroup>
  178. )
  179. }
  180. export default AccountInfoSection