[ABANDONED] React/Redux front end for the Flexor social network.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
3.9 KiB

5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. import React, { FC, useEffect } from 'react'
  2. import { useSelector, useDispatch } from 'react-redux'
  3. import { useParams, useHistory } from 'react-router'
  4. import { faUserPlus } from '@fortawesome/free-solid-svg-icons'
  5. import { handleApiError } from '../../api/errors'
  6. import { fetchGroup } from '../../actions/groups'
  7. import { initField } from '../../actions/forms'
  8. import { showNotification } from '../../actions/notifications'
  9. import { register } from '../../actions/registration'
  10. import { getEntity } from '../../selectors/entities'
  11. import { getForm } from '../../selectors/forms'
  12. import { setTitle, valueFromForm, getDefaultThemeName } from '../../utils'
  13. import { useDeepCompareEffect } from '../../hooks'
  14. import { AppState, AppThunkDispatch, Group, EntityType, NotificationType } from '../../types'
  15. import Title from '../../components/title'
  16. import Subtitle from '../../components/subtitle'
  17. import Section from '../../components/section'
  18. import HorizontalRule from '../../components/horizontal-rule'
  19. import PrimaryButton from '../../components/controls/primary-button'
  20. import Loading from '../../components/pages/loading'
  21. import CreateUserForm from '../../components/create-user-form'
  22. interface Params {
  23. id: string
  24. }
  25. const RegisterGroup: FC = () => {
  26. const { id } = useParams<Params>()
  27. const group = useSelector<AppState, Group | undefined>(state => getEntity(state, EntityType.Group, id))
  28. const form = useSelector(getForm)
  29. const dispatch = useDispatch<AppThunkDispatch>()
  30. const history = useHistory()
  31. useEffect(() => {
  32. try {
  33. dispatch(fetchGroup(id))
  34. } catch (err) {
  35. handleApiError(err, dispatch, history)
  36. }
  37. dispatch(initField('user-id', '', 'id'))
  38. dispatch(initField('user-name', '', 'name'))
  39. dispatch(initField('user-email', '', 'email'))
  40. dispatch(initField('password', ''))
  41. dispatch(initField('user-theme', getDefaultThemeName(), 'theme'))
  42. dispatch(initField('user-image', '', 'imageUrl'))
  43. dispatch(initField('user-cover-image', '', 'coverImageUrl'))
  44. dispatch(initField('user-agree', false))
  45. setTitle('Register')
  46. }, [])
  47. useDeepCompareEffect(() => {
  48. if (group) setTitle(`Register with ${group.name}`)
  49. }, [group])
  50. const handleRegister = async () => {
  51. if (!valueFromForm<boolean>(form, 'user-agree', false)) {
  52. dispatch(showNotification(NotificationType.Error, 'You must agree to the terms and conditions.'))
  53. return
  54. }
  55. try {
  56. await dispatch(register({
  57. id: valueFromForm<string>(form, 'user-id', ''),
  58. email: valueFromForm<string>(form, 'user-email', ''),
  59. password: valueFromForm<string>(form, 'password', ''),
  60. name: valueFromForm<string>(form, 'user-name', ''),
  61. imageUrl: valueFromForm<string>(form, 'user-image', ''),
  62. coverImageUrl: valueFromForm<string>(form, 'user-cover-image', ''),
  63. requiresApproval: valueFromForm<boolean>(form, 'user-requires-approval', false),
  64. privacy: valueFromForm<string>(form, 'user-privacy', 'public'),
  65. group: id,
  66. theme: valueFromForm<string>(form, 'user-theme', getDefaultThemeName()),
  67. }))
  68. dispatch(showNotification(NotificationType.Welcome, `Welcome to Flexor!`))
  69. history.push('/self')
  70. } catch (err) {
  71. handleApiError(err, dispatch, history)
  72. }
  73. }
  74. if (!group) return <Loading />
  75. return (
  76. <div>
  77. <Section>
  78. <Title>Register</Title>
  79. <Subtitle>{group.name}</Subtitle>
  80. <HorizontalRule />
  81. <CreateUserForm />
  82. <br />
  83. <PrimaryButton text="Create Your Account" icon={faUserPlus} onClick={() => handleRegister()} />
  84. </Section>
  85. </div>
  86. )
  87. }
  88. export default RegisterGroup