Implement register request

This commit is contained in:
rui hildt 2020-08-22 01:40:32 +02:00
parent 672044e905
commit 62e85f91d1
2 changed files with 90 additions and 12 deletions

View File

@ -1,11 +1,13 @@
import React, { useState } from 'react'; import React from 'react';
import { InputPicker } from 'rsuite'; import { InputPicker } from 'rsuite';
import { timezones } from '../assets/data/timezonesFlat'; import { timezones } from '../assets/data/timezonesFlat';
export default function TimezonePicker() { export default function TimezonePicker({
const [timezone, setTimezone] = useState(''); handleSelect,
handleClean,
timezone,
}) {
return ( return (
<InputPicker <InputPicker
data={timezones} data={timezones}
@ -14,8 +16,8 @@ export default function TimezonePicker() {
labelKey='timezone' labelKey='timezone'
groupBy='area' groupBy='area'
placeholder='type to search your timezone' placeholder='type to search your timezone'
onSelect={(value, item, event) => setTimezone(item.timezone)} onSelect={handleSelect}
onClean={(event) => setTimezone('')} onClean={handleClean}
value={timezone} value={timezone}
valueKey='timezone' valueKey='timezone'
/> />

View File

@ -1,4 +1,6 @@
import React from 'react'; import React, { useState } from 'react';
import { Redirect } from 'react-router-dom';
import { import {
Form, Form,
FormGroup, FormGroup,
@ -7,38 +9,112 @@ import {
HelpBlock, HelpBlock,
Button, Button,
Panel, Panel,
Message,
} from 'rsuite'; } from 'rsuite';
import { NavBar, TimezonePicker } from './../components'; import { NavBar, TimezonePicker } from './../components';
import { backend } from '../helpers/http-common';
import { useAuth } from '../helpers/authContext';
import './styles/layout.less'; import './styles/layout.less';
export default function Register() { export default function Register() {
const [error, setError] = useState(false);
const [newUser, setNewUser] = useState({
username: '',
email: '',
password: '',
timezone: '',
});
const { setAuthToken, authToken } = useAuth();
const handleChange = (value, evt) => {
setNewUser({
...newUser,
[evt.target.name]: value,
});
};
const handleSelect = (value, item, event) => {
setNewUser({
...newUser,
timezone: item.timezone,
});
};
const handleClean = (event) => {
setNewUser({
...newUser,
timezone: '',
});
};
const handleRegister = () => {
backend
.post('/auth/register', newUser)
.then((response) => {
setAuthToken(response.data.token);
})
.catch((error) => {
setError('Failed to add new account.');
});
};
if (authToken) {
return <Redirect to='/dashboard' />;
}
return ( return (
<> <>
<NavBar title='Register' /> <NavBar title='Register' />
{error && <Message type='error' description={error} />}
<Panel bordered className={'form-container'}> <Panel bordered className={'form-container'}>
<Form> <Form>
<FormGroup> <FormGroup>
<ControlLabel>Username</ControlLabel> <ControlLabel>Username</ControlLabel>
<FormControl name='username' type='text' /> <FormControl
name='username'
type='text'
formValue={newUser.username}
onChange={handleChange}
/>
</FormGroup> </FormGroup>
<FormGroup> <FormGroup>
<ControlLabel>Email</ControlLabel> <ControlLabel>Email</ControlLabel>
<FormControl name='email' type='email' /> <FormControl
name='email'
type='email'
formValue={newUser.email}
onChange={handleChange}
/>
</FormGroup> </FormGroup>
<ControlLabel>Timezone</ControlLabel> <ControlLabel>Timezone</ControlLabel>
<FormGroup> <FormGroup>
<TimezonePicker /> <TimezonePicker
handleSelect={handleSelect}
timezone={newUser.timezone}
handleClean={handleClean}
/>
</FormGroup> </FormGroup>
<ControlLabel>Password</ControlLabel> <ControlLabel>Password</ControlLabel>
<FormGroup> <FormGroup>
<FormControl name='password' type='password' /> <FormControl
name='password'
type='password'
formValue={newUser.password}
onChange={handleChange}
/>
<HelpBlock> <HelpBlock>
Minimum password length is 8 characters Minimum password length is 8 characters
</HelpBlock> </HelpBlock>
</FormGroup> </FormGroup>
<FormGroup> <FormGroup>
<Button appearance='primary' block size='lg'> <Button
appearance='primary'
block
size='lg'
onClick={handleRegister}
>
Register Register
</Button> </Button>
</FormGroup> </FormGroup>