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.
74 lines
2.6 KiB
74 lines
2.6 KiB
import { connect } 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 { getFieldValue } from 'src/selectors/forms'
|
|
import { MAX_ID_LENGTH, MAX_NAME_LENGTH } from 'src/constants'
|
|
import { AppState, AppThunkDispatch } from 'src/types'
|
|
|
|
import CreateUserStep, { Props } from './create-user-step'
|
|
|
|
const mapStateToProps = (state: AppState) => ({
|
|
userId: getFieldValue<string>(state, 'user-id', ''),
|
|
name: getFieldValue<string>(state, 'user-name', ''),
|
|
email: getFieldValue<string>(state, 'user-email', ''),
|
|
password: getFieldValue<string>(state, 'password', ''),
|
|
agree: getFieldValue<boolean>(state, 'user-agree', false),
|
|
})
|
|
|
|
const mapDispatchToProps = (dispatch: AppThunkDispatch, ownProps: Props) => ({
|
|
previous: () => {
|
|
dispatch(setStep(0))
|
|
},
|
|
next: (userId: string, name: string, email: string, password: string, agree: boolean) => {
|
|
let invalid = false
|
|
|
|
if (!userId) {
|
|
dispatch(setFieldNotification('user-id', 'error', 'This is required'))
|
|
invalid = true
|
|
}
|
|
|
|
if (userId.length > MAX_ID_LENGTH) {
|
|
dispatch(setFieldNotification('user-id', 'error', `This must be less than ${MAX_ID_LENGTH} characters`))
|
|
invalid = true
|
|
}
|
|
|
|
if (name.length > MAX_NAME_LENGTH) {
|
|
dispatch(setFieldNotification('user-name', 'error', `This must be less than ${MAX_NAME_LENGTH} characters`))
|
|
invalid = true
|
|
}
|
|
|
|
if (email === '') {
|
|
dispatch(setFieldNotification('user-email', 'error', 'This is required'))
|
|
invalid = true
|
|
}
|
|
|
|
if (!agree) {
|
|
dispatch(setFieldNotification('user-agree', 'error', 'You must agree to the terms and conditions to continue'))
|
|
dispatch(showNotification('error', 'You must agree to the terms and conditions to continue.'))
|
|
invalid = true
|
|
}
|
|
|
|
if (password === '') {
|
|
dispatch(setFieldNotification('password', 'error', 'This is required'))
|
|
invalid = true
|
|
} else {
|
|
const { score } = zxcvbn(password)
|
|
|
|
if (score === 0) {
|
|
dispatch(setFieldNotification('password', 'error', 'Try another password'))
|
|
invalid = true
|
|
}
|
|
}
|
|
|
|
if (invalid) return
|
|
if (ownProps.register) ownProps.register()
|
|
},
|
|
})
|
|
|
|
export default connect(
|
|
mapStateToProps,
|
|
mapDispatchToProps
|
|
)(CreateUserStep)
|