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.
113 lines
4.4 KiB
113 lines
4.4 KiB
import React, { FC, useEffect } from 'react'
|
|
import { useSelector, useDispatch } from 'react-redux'
|
|
import { useHistory } from 'react-router'
|
|
|
|
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 Title from 'src/components/title'
|
|
import CreateGroupStep from 'src/components/create-group-step'
|
|
import CreateUserStep from 'src/components/create-user-step'
|
|
|
|
import { setTitle, valueFromForm } from 'src/utils'
|
|
import { AppState, AppThunkDispatch, Form, NotificationType } from 'src/types'
|
|
|
|
const Register: FC = () => {
|
|
const stepIndex = useSelector<AppState, number>(getStep)
|
|
const form = useSelector<AppState, Form>(getForm)
|
|
|
|
const dispatch = useDispatch<AppThunkDispatch>()
|
|
const history = useHistory()
|
|
|
|
const handleRegister = async () => {
|
|
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', ''),
|
|
imageUrl: valueFromForm<string>(form, 'user-image', ''),
|
|
coverImageUrl: valueFromForm<string>(form, 'user-cover-image', ''),
|
|
requiresApproval: valueFromForm<boolean>(form, 'user-requires-approval', true),
|
|
privacy: valueFromForm<string>(form, 'user-privacy', 'open'),
|
|
theme: valueFromForm<string>(form, 'user-theme', ''),
|
|
}))
|
|
|
|
await dispatch(createGroup({
|
|
name: valueFromForm<string>(form, 'group-name', ''),
|
|
registration: valueFromForm<string>(form, 'group-registration', ''),
|
|
imageUrl: valueFromForm<string>(form, 'group-image', ''),
|
|
coverImageUrl: valueFromForm<string>(form, 'group-cover-image', ''),
|
|
iconImageUrl: valueFromForm<string>(form, 'group-icon-image', ''),
|
|
theme: valueFromForm<string>(form, 'group-theme', ''),
|
|
}))
|
|
|
|
dispatch(showNotification(NotificationType.Welcome, `Welcome to Flexor!`))
|
|
|
|
history.push('/self')
|
|
} catch (err) {
|
|
handleApiError(err, dispatch, history)
|
|
}
|
|
}
|
|
|
|
const title = () => {
|
|
switch (stepIndex) {
|
|
case 0: return 'Create Your Account'
|
|
default: return 'Create a Community'
|
|
}
|
|
}
|
|
|
|
const component = () => {
|
|
switch (stepIndex) {
|
|
case 0: return <CreateUserStep />
|
|
default: return <CreateGroupStep register={() => handleRegister()} />
|
|
}
|
|
}
|
|
|
|
useEffect(() => {
|
|
dispatch(initForm())
|
|
dispatch(initField('group-name', '', 'name'))
|
|
dispatch(initField('group-registration', 'open', 'registration'))
|
|
dispatch(initField('group-image', '', 'imageUrl'))
|
|
dispatch(initField('group-cover-image', '', 'coverImageUrl'))
|
|
dispatch(initField('group-icon-image', '', 'iconImageUrl'))
|
|
dispatch(initField('group-theme', '', 'theme'))
|
|
dispatch(initField('group-agree', false))
|
|
dispatch(initField('user-id', '', 'id'))
|
|
dispatch(initField('user-name', '', 'name'))
|
|
dispatch(initField('user-email', '', 'email'))
|
|
dispatch(initField('password', ''))
|
|
dispatch(initField('user-image', '', 'imageUrl'))
|
|
dispatch(initField('user-cover-image', '', 'coverImageUrl'))
|
|
dispatch(initField('user-requires-approval', true, 'requiresApproval'))
|
|
dispatch(initField('user-privacy', 'public', 'privacy'))
|
|
dispatch(initField('user-theme', '', 'theme'))
|
|
dispatch(initField('user-agree', false))
|
|
}, [])
|
|
|
|
useEffect(() => {
|
|
setTitle(title())
|
|
}, [stepIndex])
|
|
|
|
return (
|
|
<div>
|
|
<Title>{title()}</Title>
|
|
|
|
{component()}
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export default Register
|