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

133 lines
5.4 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. // register.tsx
  2. // Copyright (C) 2020 Dwayne Harris
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation, either version 3 of the License, or
  6. // (at your option) any later version.
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU General Public License for more details.
  11. // You should have received a copy of the GNU General Public License
  12. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  13. import React, { FC, useEffect } from 'react'
  14. import { useSelector, useDispatch } from 'react-redux'
  15. import { useHistory } from 'react-router'
  16. import { handleApiError } from '../../api/errors'
  17. import { getForm } from '../../selectors/forms'
  18. import { getStep } from '../../selectors/registration'
  19. import { initForm, initField } from '../../actions/forms'
  20. import { showNotification } from '../../actions/notifications'
  21. import { createGroup, register } from '../../actions/registration'
  22. import { setTitle, valueFromForm, getDefaultThemeName } from '../../utils'
  23. import { AppThunkDispatch, NotificationType } from '../../types'
  24. import Title from '../../components/title'
  25. import Section from '../../components/section'
  26. import HorizontalRule from '../../components/horizontal-rule'
  27. import CreateGroupStep from '../../components/create-group-step'
  28. import CreateUserStep from '../../components/create-user-step'
  29. const Register: FC = () => {
  30. const stepIndex = useSelector(getStep)
  31. const form = useSelector(getForm)
  32. const dispatch = useDispatch<AppThunkDispatch>()
  33. const history = useHistory()
  34. const handleRegister = async () => {
  35. const groupAgree = valueFromForm<boolean>(form, 'group-agree', false)
  36. const userAgree = valueFromForm<boolean>(form, 'user-agree', false)
  37. if (!groupAgree || !userAgree) {
  38. dispatch(showNotification(NotificationType.Error, 'You must agree to both Community and User terms and conditions.'))
  39. return
  40. }
  41. try {
  42. await dispatch(register({
  43. id: valueFromForm<string>(form, 'user-id', ''),
  44. email: valueFromForm<string>(form, 'user-email', ''),
  45. password: valueFromForm<string>(form, 'password', ''),
  46. name: valueFromForm<string>(form, 'user-name', ''),
  47. imageUrl: valueFromForm<string>(form, 'user-image', ''),
  48. coverImageUrl: valueFromForm<string>(form, 'user-cover-image', ''),
  49. requiresApproval: valueFromForm<boolean>(form, 'user-requires-approval', false),
  50. privacy: valueFromForm<string>(form, 'user-privacy', 'open'),
  51. theme: valueFromForm<string>(form, 'user-theme', getDefaultThemeName()),
  52. }))
  53. await dispatch(createGroup({
  54. name: valueFromForm<string>(form, 'group-name', ''),
  55. registration: valueFromForm<string>(form, 'group-registration', ''),
  56. imageUrl: valueFromForm<string>(form, 'group-image', ''),
  57. coverImageUrl: valueFromForm<string>(form, 'group-cover-image', ''),
  58. iconImageUrl: valueFromForm<string>(form, 'group-icon-image', ''),
  59. theme: valueFromForm<string>(form, 'group-theme', getDefaultThemeName()),
  60. }))
  61. dispatch(showNotification(NotificationType.Welcome, `Welcome to Flexor!`))
  62. location.href = '/self'
  63. } catch (err) {
  64. handleApiError(err, dispatch, history)
  65. }
  66. }
  67. const title = () => {
  68. switch (stepIndex) {
  69. case 0: return 'Create Your Account'
  70. default: return 'Create a Community'
  71. }
  72. }
  73. const component = () => {
  74. switch (stepIndex) {
  75. case 0: return <CreateUserStep />
  76. default: return <CreateGroupStep register={() => handleRegister()} />
  77. }
  78. }
  79. useEffect(() => {
  80. dispatch(initForm())
  81. dispatch(initField('group-name', '', 'name'))
  82. dispatch(initField('group-registration', 'open', 'registration'))
  83. dispatch(initField('group-image', '', 'imageUrl'))
  84. dispatch(initField('group-cover-image', '', 'coverImageUrl'))
  85. dispatch(initField('group-icon-image', '', 'iconImageUrl'))
  86. dispatch(initField('group-theme', getDefaultThemeName(), 'theme'))
  87. dispatch(initField('group-agree', false))
  88. dispatch(initField('user-id', '', 'id'))
  89. dispatch(initField('user-name', '', 'name'))
  90. dispatch(initField('user-email', '', 'email'))
  91. dispatch(initField('password', ''))
  92. dispatch(initField('user-image', '', 'imageUrl'))
  93. dispatch(initField('user-cover-image', '', 'coverImageUrl'))
  94. dispatch(initField('user-requires-approval', false, 'requiresApproval'))
  95. dispatch(initField('user-privacy', 'public', 'privacy'))
  96. dispatch(initField('user-theme', getDefaultThemeName(), 'theme'))
  97. dispatch(initField('user-agree', false))
  98. }, [])
  99. useEffect(() => {
  100. setTitle(title())
  101. }, [stepIndex])
  102. return (
  103. <div>
  104. <Section>
  105. <Title>{title()}</Title>
  106. <HorizontalRule />
  107. {component()}
  108. </Section>
  109. </div>
  110. )
  111. }
  112. export default Register