[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.

117 lines
4.7 KiB

5 years ago
5 years ago
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
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
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 { useHistory } from 'react-router'
  4. import { handleApiError } from '../../api/errors'
  5. import { getForm } from '../../selectors/forms'
  6. import { getStep } from '../../selectors/registration'
  7. import { initForm, initField } from '../../actions/forms'
  8. import { showNotification } from '../../actions/notifications'
  9. import { createGroup, register } from '../../actions/registration'
  10. import { setTitle, valueFromForm, getDefaultThemeName } from '../../utils'
  11. import { AppThunkDispatch, NotificationType } from '../../types'
  12. import Title from '../../components/title'
  13. import Section from '../../components/section'
  14. import HorizontalRule from '../../components/horizontal-rule'
  15. import CreateGroupStep from '../../components/create-group-step'
  16. import CreateUserStep from '../../components/create-user-step'
  17. const Register: FC = () => {
  18. const stepIndex = useSelector(getStep)
  19. const form = useSelector(getForm)
  20. const dispatch = useDispatch<AppThunkDispatch>()
  21. const history = useHistory()
  22. const handleRegister = async () => {
  23. const groupAgree = valueFromForm<boolean>(form, 'group-agree', false)
  24. const userAgree = valueFromForm<boolean>(form, 'user-agree', false)
  25. if (!groupAgree || !userAgree) {
  26. dispatch(showNotification(NotificationType.Error, 'You must agree to both Community and User terms and conditions.'))
  27. return
  28. }
  29. try {
  30. await dispatch(register({
  31. id: valueFromForm<string>(form, 'user-id', ''),
  32. email: valueFromForm<string>(form, 'user-email', ''),
  33. password: valueFromForm<string>(form, 'password', ''),
  34. name: valueFromForm<string>(form, 'user-name', ''),
  35. imageUrl: valueFromForm<string>(form, 'user-image', ''),
  36. coverImageUrl: valueFromForm<string>(form, 'user-cover-image', ''),
  37. requiresApproval: valueFromForm<boolean>(form, 'user-requires-approval', false),
  38. privacy: valueFromForm<string>(form, 'user-privacy', 'open'),
  39. theme: valueFromForm<string>(form, 'user-theme', getDefaultThemeName()),
  40. }))
  41. await dispatch(createGroup({
  42. name: valueFromForm<string>(form, 'group-name', ''),
  43. registration: valueFromForm<string>(form, 'group-registration', ''),
  44. imageUrl: valueFromForm<string>(form, 'group-image', ''),
  45. coverImageUrl: valueFromForm<string>(form, 'group-cover-image', ''),
  46. iconImageUrl: valueFromForm<string>(form, 'group-icon-image', ''),
  47. theme: valueFromForm<string>(form, 'group-theme', getDefaultThemeName()),
  48. }))
  49. dispatch(showNotification(NotificationType.Welcome, `Welcome to Flexor!`))
  50. location.href = '/self'
  51. } catch (err) {
  52. handleApiError(err, dispatch, history)
  53. }
  54. }
  55. const title = () => {
  56. switch (stepIndex) {
  57. case 0: return 'Create Your Account'
  58. default: return 'Create a Community'
  59. }
  60. }
  61. const component = () => {
  62. switch (stepIndex) {
  63. case 0: return <CreateUserStep />
  64. default: return <CreateGroupStep register={() => handleRegister()} />
  65. }
  66. }
  67. useEffect(() => {
  68. dispatch(initForm())
  69. dispatch(initField('group-name', '', 'name'))
  70. dispatch(initField('group-registration', 'open', 'registration'))
  71. dispatch(initField('group-image', '', 'imageUrl'))
  72. dispatch(initField('group-cover-image', '', 'coverImageUrl'))
  73. dispatch(initField('group-icon-image', '', 'iconImageUrl'))
  74. dispatch(initField('group-theme', getDefaultThemeName(), 'theme'))
  75. dispatch(initField('group-agree', false))
  76. dispatch(initField('user-id', '', 'id'))
  77. dispatch(initField('user-name', '', 'name'))
  78. dispatch(initField('user-email', '', 'email'))
  79. dispatch(initField('password', ''))
  80. dispatch(initField('user-image', '', 'imageUrl'))
  81. dispatch(initField('user-cover-image', '', 'coverImageUrl'))
  82. dispatch(initField('user-requires-approval', false, 'requiresApproval'))
  83. dispatch(initField('user-privacy', 'public', 'privacy'))
  84. dispatch(initField('user-theme', getDefaultThemeName(), 'theme'))
  85. dispatch(initField('user-agree', false))
  86. }, [])
  87. useEffect(() => {
  88. setTitle(title())
  89. }, [stepIndex])
  90. return (
  91. <div>
  92. <Section>
  93. <Title>{title()}</Title>
  94. <HorizontalRule />
  95. {component()}
  96. </Section>
  97. </div>
  98. )
  99. }
  100. export default Register