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.
68 lines
2.5 KiB
68 lines
2.5 KiB
import { connect } from 'react-redux'
|
|
|
|
import { handleApiError } from 'src/api/errors'
|
|
import { getForm } from 'src/selectors/forms'
|
|
import { getStep } from 'src/selectors/registration'
|
|
import { initForm, initField, setFieldValue } from 'src/actions/forms'
|
|
import { showNotification } from 'src/actions/notifications'
|
|
import { createGroup, register } from 'src/actions/registration'
|
|
|
|
import { valueFromForm } from 'src/utils'
|
|
import { AppState, AppThunkDispatch, Form, NotificationType } from 'src/types'
|
|
|
|
import Register, { Props } from './register'
|
|
|
|
const mapStateToProps = (state: AppState) => ({
|
|
stepIndex: getStep(state),
|
|
form: getForm(state),
|
|
})
|
|
|
|
const mapDispatchToProps = (dispatch: AppThunkDispatch, ownProps: Props) => ({
|
|
initForm: () => {
|
|
dispatch(initForm())
|
|
dispatch(initField('group-name', 'name'))
|
|
dispatch(initField('group-registration', 'registration'))
|
|
dispatch(initField('group-agree'))
|
|
dispatch(initField('user-id', 'id'))
|
|
dispatch(initField('user-name', 'name'))
|
|
dispatch(initField('user-email', 'email'))
|
|
dispatch(initField('password'))
|
|
dispatch(initField('user-agree'))
|
|
|
|
dispatch(setFieldValue('group-registration', 'open'))
|
|
},
|
|
register: async (form: Form) => {
|
|
const groupAgree = valueFromForm<boolean>(form, 'group-agree', false)
|
|
const userAgree = valueFromForm<boolean>(form, 'user-agree', false)
|
|
|
|
if (!groupAgree || !userAgree) {
|
|
dispatch(showNotification(NotificationType.Error, 'You must agree to both Community and User terms and conditions.'))
|
|
return
|
|
}
|
|
|
|
try {
|
|
await dispatch(register({
|
|
id: valueFromForm<string>(form, 'user-id', ''),
|
|
email: valueFromForm<string>(form, 'user-email', ''),
|
|
password: valueFromForm<string>(form, 'password', ''),
|
|
name: valueFromForm<string>(form, 'user-name', ''),
|
|
}))
|
|
|
|
await dispatch(createGroup({
|
|
name: valueFromForm<string>(form, 'group-name', ''),
|
|
registration: valueFromForm<string>(form, 'group-registration', ''),
|
|
}))
|
|
|
|
dispatch(showNotification(NotificationType.Welcome, `Welcome to Flexor!`))
|
|
|
|
ownProps.history.push('/self')
|
|
} catch (err) {
|
|
handleApiError(err, dispatch, ownProps.history)
|
|
}
|
|
}
|
|
})
|
|
|
|
export default connect(
|
|
mapStateToProps,
|
|
mapDispatchToProps
|
|
)(Register)
|