|
| 1 | +import * as React from 'react'; |
| 2 | +import OpenInputField from '@/components/forms/OpenInputField'; |
| 3 | +import { Link } from 'react-router-dom'; |
| 4 | +import login from '@/api/auth/login'; |
| 5 | +import { httpErrorToHuman } from '@/api/http'; |
| 6 | +import MessageBox from '@/components/MessageBox'; |
| 7 | + |
| 8 | +type State = Readonly<{ |
| 9 | + errorMessage?: string; |
| 10 | + isLoading: boolean; |
| 11 | + username?: string; |
| 12 | + password?: string; |
| 13 | +}>; |
| 14 | + |
| 15 | +export default class LoginContainer extends React.PureComponent<{}, State> { |
| 16 | + username = React.createRef<HTMLInputElement>(); |
| 17 | + |
| 18 | + state: State = { |
| 19 | + isLoading: false, |
| 20 | + }; |
| 21 | + |
| 22 | + submit = (e: React.FormEvent<HTMLFormElement>) => { |
| 23 | + e.preventDefault(); |
| 24 | + |
| 25 | + const { username, password } = this.state; |
| 26 | + |
| 27 | + this.setState({ isLoading: true }, () => { |
| 28 | + login(username!, password!) |
| 29 | + .then(response => { |
| 30 | + |
| 31 | + }) |
| 32 | + .catch(error => this.setState({ |
| 33 | + isLoading: false, |
| 34 | + errorMessage: httpErrorToHuman(error), |
| 35 | + }, () => console.error(error))); |
| 36 | + }); |
| 37 | + }; |
| 38 | + |
| 39 | + canSubmit () { |
| 40 | + if (!this.state.username || !this.state.password) { |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 44 | + return this.state.username.length > 0 && this.state.password.length > 0; |
| 45 | + } |
| 46 | + |
| 47 | + // @ts-ignore |
| 48 | + handleFieldUpdate = (e: React.ChangeEvent<HTMLInputElement>) => this.setState({ |
| 49 | + [e.target.id]: e.target.value, |
| 50 | + }); |
| 51 | + |
| 52 | + render () { |
| 53 | + return ( |
| 54 | + <React.Fragment> |
| 55 | + {this.state.errorMessage && |
| 56 | + <div className={'mb-4'}> |
| 57 | + <MessageBox |
| 58 | + type={'error'} |
| 59 | + title={'Error'} |
| 60 | + message={this.state.errorMessage} |
| 61 | + /> |
| 62 | + </div> |
| 63 | + } |
| 64 | + <form className={'login-box'} onSubmit={this.submit}> |
| 65 | + <div className={'-mx-3'}> |
| 66 | + <OpenInputField |
| 67 | + autoFocus={true} |
| 68 | + label={'Username or Email'} |
| 69 | + type={'text'} |
| 70 | + required={true} |
| 71 | + id={'username'} |
| 72 | + onChange={this.handleFieldUpdate} |
| 73 | + disabled={this.state.isLoading} |
| 74 | + /> |
| 75 | + </div> |
| 76 | + <div className={'-mx-3 mt-6'}> |
| 77 | + <OpenInputField |
| 78 | + label={'Password'} |
| 79 | + type={'password'} |
| 80 | + required={true} |
| 81 | + id={'password'} |
| 82 | + onChange={this.handleFieldUpdate} |
| 83 | + disabled={this.state.isLoading} |
| 84 | + /> |
| 85 | + </div> |
| 86 | + <div className={'mt-6'}> |
| 87 | + <button |
| 88 | + type={'submit'} |
| 89 | + className={'btn btn-primary btn-jumbo'} |
| 90 | + disabled={this.state.isLoading || !this.canSubmit()} |
| 91 | + > |
| 92 | + {this.state.isLoading ? |
| 93 | + <span className={'spinner white'}> </span> |
| 94 | + : |
| 95 | + 'Login' |
| 96 | + } |
| 97 | + </button> |
| 98 | + </div> |
| 99 | + <div className={'mt-6 text-center'}> |
| 100 | + <Link |
| 101 | + to={'/auth/forgot-password'} |
| 102 | + className={'text-xs text-neutral-500 tracking-wide no-underline uppercase hover:text-neutral-600'} |
| 103 | + > |
| 104 | + Forgot password? |
| 105 | + </Link> |
| 106 | + </div> |
| 107 | + </form> |
| 108 | + </React.Fragment> |
| 109 | + ); |
| 110 | + } |
| 111 | +} |
0 commit comments