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.
87 lines
3.1 KiB
87 lines
3.1 KiB
import React, { FC } from 'react'
|
|
import { useSelector, useDispatch } from 'react-redux'
|
|
import zxcvbn from 'zxcvbn'
|
|
|
|
import { setFieldNotification } from 'src/actions/forms'
|
|
import { showNotification } from 'src/actions/notifications'
|
|
import { setStep } from 'src/actions/registration'
|
|
import { getForm } from 'src/selectors/forms'
|
|
import { MAX_ID_LENGTH, MAX_NAME_LENGTH } from 'src/constants'
|
|
import { valueFromForm } from 'src/utils'
|
|
|
|
import { AppState, AppThunkDispatch, NotificationType, Form } from 'src/types'
|
|
|
|
import CreateUserForm from './create-user-form'
|
|
import HorizontalRule from 'src/components/horizontal-rule'
|
|
import PrimaryButton from 'src/components/controls/primary-button'
|
|
|
|
const CreateUserStep: FC = () => {
|
|
const form = useSelector<AppState, Form>(getForm)
|
|
const dispatch = useDispatch<AppThunkDispatch>()
|
|
|
|
const next = () => {
|
|
let invalid = false
|
|
|
|
const userId = valueFromForm<string>(form, 'user-id')
|
|
const name = valueFromForm<string>(form, 'user-name')
|
|
const email = valueFromForm<string>(form, 'user-email')
|
|
const password = valueFromForm<string>(form, 'password')
|
|
const agree = valueFromForm<boolean>(form, 'user-agree')
|
|
|
|
if (!userId || userId === '') {
|
|
dispatch(setFieldNotification('user-id', NotificationType.Error, 'This is required'))
|
|
invalid = true
|
|
}
|
|
|
|
if (userId && userId.length > MAX_ID_LENGTH) {
|
|
dispatch(setFieldNotification('user-id', NotificationType.Error, `This must be less than ${MAX_ID_LENGTH} characters`))
|
|
invalid = true
|
|
}
|
|
|
|
if (name && name.length > MAX_NAME_LENGTH) {
|
|
dispatch(setFieldNotification('user-name', NotificationType.Error, `This must be less than ${MAX_NAME_LENGTH} characters`))
|
|
invalid = true
|
|
}
|
|
|
|
if (!email || email === '') {
|
|
dispatch(setFieldNotification('user-email', NotificationType.Error, 'This is required'))
|
|
invalid = true
|
|
}
|
|
|
|
if (!agree) {
|
|
dispatch(setFieldNotification('user-agree', NotificationType.Error, 'You must agree to the terms and conditions to continue'))
|
|
dispatch(showNotification(NotificationType.Error, 'You must agree to the terms and conditions to continue.'))
|
|
invalid = true
|
|
}
|
|
|
|
if (!password || password === '') {
|
|
dispatch(setFieldNotification('password', NotificationType.Error, 'This is required'))
|
|
invalid = true
|
|
} else {
|
|
const { score } = zxcvbn(password)
|
|
|
|
if (score === 0) {
|
|
dispatch(setFieldNotification('password', NotificationType.Error, 'Try another password'))
|
|
invalid = true
|
|
}
|
|
}
|
|
|
|
if (invalid) return
|
|
dispatch(setStep(1))
|
|
}
|
|
|
|
return (
|
|
<div>
|
|
<CreateUserForm />
|
|
<HorizontalRule />
|
|
|
|
<nav className="level" style={{ flexDirection: 'row-reverse'}}>
|
|
<div>
|
|
<PrimaryButton text="To Community" onClick={() => next()} />
|
|
</div>
|
|
</nav>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export default CreateUserStep
|