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.
65 lines
2.3 KiB
65 lines
2.3 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 } 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 } 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'))
|
|
dispatch(initField('group-registration'))
|
|
dispatch(initField('group-agree'))
|
|
dispatch(initField('user-id'))
|
|
dispatch(initField('user-name'))
|
|
dispatch(initField('user-email'))
|
|
dispatch(initField('password'))
|
|
dispatch(initField('user-agree'))
|
|
},
|
|
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('error', 'You must agree to both Community and User terms and conditions.'))
|
|
return
|
|
}
|
|
|
|
try {
|
|
const group = await dispatch(createGroup({
|
|
name: valueFromForm<string>(form, 'group-name', ''),
|
|
registration: valueFromForm<string>(form, 'group-registration', ''),
|
|
}))
|
|
|
|
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', ''),
|
|
group,
|
|
}))
|
|
|
|
ownProps.history.push('/self')
|
|
} catch (err) {
|
|
handleApiError(err, dispatch, ownProps.history)
|
|
}
|
|
}
|
|
})
|
|
|
|
export default connect(
|
|
mapStateToProps,
|
|
mapDispatchToProps
|
|
)(Register)
|