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.
101 lines
3.4 KiB
101 lines
3.4 KiB
import React, { FC, useEffect } from 'react'
|
|
import { useSelector, useDispatch } from 'react-redux'
|
|
import { useParams, useHistory } from 'react-router'
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
|
|
import { faUserPlus } from '@fortawesome/free-solid-svg-icons'
|
|
|
|
import { handleApiError } from 'src/api/errors'
|
|
import { fetchGroup } from 'src/actions/groups'
|
|
import { initField } from 'src/actions/forms'
|
|
import { showNotification } from 'src/actions/notifications'
|
|
import { register } from 'src/actions/registration'
|
|
import { getEntity } from 'src/selectors/entities'
|
|
import { getForm } from 'src/selectors/forms'
|
|
|
|
import { setTitle, valueFromForm } from 'src/utils'
|
|
import { useDeepCompareEffect } from 'src/hooks'
|
|
|
|
import { AppState, AppThunkDispatch, Group, EntityType, NotificationType, Form } from 'src/types'
|
|
|
|
import PageHeader from 'src/components/page-header'
|
|
import Loading from 'src/components/pages/loading'
|
|
import CreateUserForm from 'src/components/create-user-form'
|
|
|
|
interface Params {
|
|
id: string
|
|
}
|
|
|
|
const RegisterGroup: FC = () => {
|
|
const { id } = useParams<Params>()
|
|
const group = useSelector<AppState, Group | undefined>(state => getEntity(state, EntityType.Group, id))
|
|
const form = useSelector<AppState, Form>(getForm)
|
|
const dispatch = useDispatch<AppThunkDispatch>()
|
|
const history = useHistory()
|
|
|
|
useEffect(() => {
|
|
try {
|
|
dispatch(fetchGroup(id))
|
|
} catch (err) {
|
|
handleApiError(err, dispatch, history)
|
|
}
|
|
|
|
dispatch(initField('user-id', 'id'))
|
|
dispatch(initField('user-name', 'name'))
|
|
dispatch(initField('user-email', 'email'))
|
|
dispatch(initField('password'))
|
|
dispatch(initField('user-agree'))
|
|
|
|
setTitle('Register')
|
|
}, [])
|
|
|
|
useDeepCompareEffect(() => {
|
|
if (group) setTitle(`Register with ${group.name}`)
|
|
}, [group])
|
|
|
|
const handleRegister = async () => {
|
|
if (!valueFromForm<boolean>(form, 'user-agree', false)) {
|
|
dispatch(showNotification(NotificationType.Error, 'You must agree to the 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', ''),
|
|
group: id,
|
|
}))
|
|
|
|
dispatch(showNotification(NotificationType.Welcome, `Welcome to Flexor!`))
|
|
|
|
history.push('/self')
|
|
} catch (err) {
|
|
handleApiError(err, dispatch, history)
|
|
}
|
|
}
|
|
|
|
if (!group) return <Loading />
|
|
|
|
return (
|
|
<div>
|
|
<PageHeader title="Register" subtitle={group.name} />
|
|
|
|
<div className="main-content">
|
|
<div className="centered-content">
|
|
<CreateUserForm />
|
|
<br />
|
|
|
|
<button className="button is-primary" onClick={() => handleRegister()}>
|
|
<span className="icon is-small">
|
|
<FontAwesomeIcon icon={faUserPlus} />
|
|
</span>
|
|
<span>Create Your Account</span>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export default RegisterGroup
|