aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2022-02-12 18:08:04 -0600
committerJP Dillingham <jpdillingham@gmail.com>2022-02-12 18:08:04 -0600
commit8d2120d59acf3a3d5bdb86fbeb59355f74bee14e (patch)
treeb2c4859b33c9bcd048a948f1324f839c0fc406f4
parenta899da7655b1e7097b0c06f75512eb3e65541bae (diff)
downloadslskd-8d2120d59acf3a3d5bdb86fbeb59355f74bee14e.tar.gz
convert to functional component, use hooks
-rw-r--r--src/web/src/components/LoginForm.js146
1 files changed, 73 insertions, 73 deletions
diff --git a/src/web/src/components/LoginForm.js b/src/web/src/components/LoginForm.js
index 6704c498..44043ddb 100644
--- a/src/web/src/components/LoginForm.js
+++ b/src/web/src/components/LoginForm.js
@@ -1,4 +1,4 @@
-import React, { Component } from 'react'
+import React, { useState, useEffect } from 'react'
import { Button, Form, Grid, Header, Icon, Segment, Checkbox, Message } from 'semantic-ui-react'
import Logos from './Shared/Logo';
@@ -6,86 +6,86 @@ import Logos from './Shared/Logo';
const initialState = {
username: '',
password: '',
- rememberMe: true,
- ready: false,
+ rememberMe: true
}
-class LoginForm extends Component {
- state = initialState;
+const LoginForm = ({ onLoginAttempt, loading, error }) => {
+ const [state, setState] = useState(initialState);
+ const [ready, setReady] = useState(false);
- handleChange = (field, value) => {
- this.setState({
+ useEffect(() => {
+ if (state.username !== '' && state.password !== '') {
+ setReady(true);
+ } else {
+ setReady(false);
+ }
+ }, [state])
+
+ const handleChange = (field, value) => {
+ setState({
+ ...state,
[field]: value,
- }, () => {
- if (this.state.username !== '' && this.state.password !== '') {
- this.setState({ ready: true })
- } else {
- this.setState({ ready: false })
- }
});
}
- render = () => {
- const { onLoginAttempt, loading, error } = this.props;
- const { username, password, rememberMe, ready } = this.state;
+ const { username, password, rememberMe } = state;
- return (
- <>
- <Grid textAlign='center' style={{ height: '100vh' }} verticalAlign='middle'>
- <Grid.Column style={{ maxWidth: 372 }}>
- <Header as='h2' textAlign='center' style={{
- whiteSpace: 'pre',
- fontFamily: 'monospace',
- lineHeight: 1.1,
- fontSize: 'inherit',
- letterSpacing: -1
- }}>
- {Logos[Math.floor(Math.random() * Logos.length)]}
- </Header>
- <Form size='large'>
- <Segment loading={loading}>
- <Form.Input
- fluid icon='user'
- iconPosition='left'
- placeholder='Username'
- onChange={(event) => this.handleChange('username', event.target.value)}
- />
- <Form.Input
- fluid
- icon='lock'
- iconPosition='left'
- placeholder='Password'
- type='password'
- onChange={(event) => this.handleChange('password', event.target.value)}
- />
- <Checkbox
- label='Remember Me'
- onChange={() => this.handleChange('rememberMe', !rememberMe)}
- checked={rememberMe}
- />
- </Segment>
- <Button
- primary
- fluid
- size='large'
- className='login-button'
- loading={loading}
- disabled={!ready || loading}
- onClick={() => onLoginAttempt(username, password, rememberMe)}
- >
- <Icon name='sign in'/>
- Login
- </Button>
- {error && <Message className='login-failure' negative floating>
- <Icon name='x' />
- {error.message}
- </Message>}
- </Form>
- </Grid.Column>
- </Grid>
- </>
- )
- }
+ return (
+ <>
+ <Grid textAlign='center' style={{ height: '100vh' }} verticalAlign='middle'>
+ <Grid.Column style={{ maxWidth: 372 }}>
+ <Header as='h2' textAlign='center' style={{
+ whiteSpace: 'pre',
+ fontFamily: 'monospace',
+ lineHeight: 1.1,
+ fontSize: 'inherit',
+ letterSpacing: -1
+ }}>
+ {Logos[Math.floor(Math.random() * Logos.length)]}
+ </Header>
+ <Form size='large'>
+ <Segment loading={loading}>
+ <Form.Input
+ fluid icon='user'
+ iconPosition='left'
+ placeholder='Username'
+ onChange={(event) => handleChange('username', event.target.value)}
+ />
+ <Form.Input
+ fluid
+ icon='lock'
+ iconPosition='left'
+ placeholder='Password'
+ type='password'
+ onChange={(event) => handleChange('password', event.target.value)}
+ />
+ <Checkbox
+ label='Remember Me'
+ onChange={() => handleChange('rememberMe', !rememberMe)}
+ checked={rememberMe}
+ />
+ </Segment>
+ <Button
+ primary
+ fluid
+ size='large'
+ className='login-button'
+ loading={loading}
+ disabled={!ready || loading}
+ onClick={() => onLoginAttempt(username, password, rememberMe)}
+ >
+ <Icon name='sign in'/>
+ Login
+ </Button>
+ {error && <Message className='login-failure' negative floating>
+ <Icon name='x' />
+ {error.message}
+ </Message>}
+ </Form>
+ </Grid.Column>
+ </Grid>
+ </>
+ )
}
export default LoginForm \ No newline at end of file