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

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