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

103 lines
3.8 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
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. // create-user-step.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 } from 'react'
  14. import { useSelector, useDispatch } from 'react-redux'
  15. import zxcvbn from 'zxcvbn'
  16. import { setFieldNotification } from '../actions/forms'
  17. import { showNotification } from '../actions/notifications'
  18. import { setStep } from '../actions/registration'
  19. import { getForm } from '../selectors/forms'
  20. import { MAX_ID_LENGTH, MAX_NAME_LENGTH } from '../constants'
  21. import { valueFromForm } from '../utils'
  22. import { AppThunkDispatch, NotificationType } from '../types'
  23. import CreateUserForm from './create-user-form'
  24. import HorizontalRule from '../components/horizontal-rule'
  25. import PrimaryButton from '../components/controls/primary-button'
  26. const CreateUserStep: FC = () => {
  27. const form = useSelector(getForm)
  28. const dispatch = useDispatch<AppThunkDispatch>()
  29. const next = () => {
  30. let invalid = false
  31. const userId = valueFromForm<string>(form, 'user-id')
  32. const name = valueFromForm<string>(form, 'user-name')
  33. const email = valueFromForm<string>(form, 'user-email')
  34. const password = valueFromForm<string>(form, 'password')
  35. const agree = valueFromForm<boolean>(form, 'user-agree')
  36. if (!userId || userId === '') {
  37. dispatch(setFieldNotification('user-id', NotificationType.Error, 'This is required'))
  38. invalid = true
  39. }
  40. if (userId && userId.length > MAX_ID_LENGTH) {
  41. dispatch(setFieldNotification('user-id', NotificationType.Error, `This must be less than ${MAX_ID_LENGTH} characters`))
  42. invalid = true
  43. }
  44. if (name && name.length > MAX_NAME_LENGTH) {
  45. dispatch(setFieldNotification('user-name', NotificationType.Error, `This must be less than ${MAX_NAME_LENGTH} characters`))
  46. invalid = true
  47. }
  48. if (!email || email === '') {
  49. dispatch(setFieldNotification('user-email', NotificationType.Error, 'This is required'))
  50. invalid = true
  51. }
  52. if (!agree) {
  53. dispatch(setFieldNotification('user-agree', NotificationType.Error, 'You must agree to the terms and conditions to continue'))
  54. dispatch(showNotification(NotificationType.Error, 'You must agree to the terms and conditions to continue.'))
  55. invalid = true
  56. }
  57. if (!password || password === '') {
  58. dispatch(setFieldNotification('password', NotificationType.Error, 'This is required'))
  59. invalid = true
  60. } else {
  61. const { score } = zxcvbn(password)
  62. if (score === 0) {
  63. dispatch(setFieldNotification('password', NotificationType.Error, 'Try another password'))
  64. invalid = true
  65. }
  66. }
  67. if (invalid) return
  68. dispatch(setStep(1))
  69. }
  70. return (
  71. <div>
  72. <CreateUserForm />
  73. <HorizontalRule />
  74. <nav className="level" style={{ flexDirection: 'row-reverse'}}>
  75. <div>
  76. <PrimaryButton text="To Community" onClick={() => next()} />
  77. </div>
  78. </nav>
  79. </div>
  80. )
  81. }
  82. export default CreateUserStep