|
| 1 | +import * as React from 'react'; |
| 2 | +import { RouteComponentProps } from 'react-router'; |
| 3 | +import { connect } from 'react-redux'; |
| 4 | +import { pushFlashMessage, clearAllFlashMessages } from '@/redux/actions/flash'; |
| 5 | +import NetworkErrorMessage from '@/components/NetworkErrorMessage'; |
| 6 | + |
| 7 | +type State = Readonly<{ |
| 8 | + isLoading: boolean; |
| 9 | + errorMessage?: string; |
| 10 | + code: string; |
| 11 | +}>; |
| 12 | + |
| 13 | +class LoginCheckpointContainer extends React.PureComponent<RouteComponentProps, State> { |
| 14 | + state: State = { |
| 15 | + code: '', |
| 16 | + isLoading: false, |
| 17 | + }; |
| 18 | + |
| 19 | + moveToNextInput (e: React.KeyboardEvent<HTMLInputElement>, isBackspace: boolean = false) { |
| 20 | + const form = e.currentTarget.form; |
| 21 | + |
| 22 | + if (form) { |
| 23 | + const index = Array.prototype.indexOf.call(form, e.currentTarget); |
| 24 | + const element = form.elements[index + (isBackspace ? -1 : 1)]; |
| 25 | + |
| 26 | + // @ts-ignore |
| 27 | + element && element.focus(); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + handleNumberInput = (e: React.KeyboardEvent<HTMLInputElement>) => { |
| 32 | + const number = Number(e.key); |
| 33 | + if (isNaN(number)) { |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + this.setState(s => ({ code: s.code + number.toString() })); |
| 38 | + this.moveToNextInput(e); |
| 39 | + }; |
| 40 | + |
| 41 | + handleBackspace = (e: React.KeyboardEvent<HTMLInputElement>) => { |
| 42 | + const isBackspace = e.key === 'Delete' || e.key === 'Backspace'; |
| 43 | + |
| 44 | + if (!isBackspace || e.currentTarget.value.length > 0) { |
| 45 | + e.currentTarget.value = ''; |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + this.setState(s => ({ code: s.code.substring(0, s.code.length - 2) })); |
| 50 | + e.currentTarget.value = ''; |
| 51 | + this.moveToNextInput(e, true); |
| 52 | + }; |
| 53 | + |
| 54 | + render () { |
| 55 | + return ( |
| 56 | + <React.Fragment> |
| 57 | + <h2 className={'text-center text-neutral-100 font-medium py-4'}> |
| 58 | + Device Checkpoint |
| 59 | + </h2> |
| 60 | + <NetworkErrorMessage message={this.state.errorMessage}/> |
| 61 | + <form className={'login-box'} onSubmit={() => null}> |
| 62 | + <p className={'text-sm text-neutral-700'}> |
| 63 | + This account is protected with two-factor authentication. Please provide an authentication |
| 64 | + code from your device in order to continue. |
| 65 | + </p> |
| 66 | + <div className={'flex mt-6'}> |
| 67 | + { |
| 68 | + [1, 2, 3, 4, 5, 6].map((_, index) => ( |
| 69 | + <input |
| 70 | + autoFocus={index === 0} |
| 71 | + key={`input_${index}`} |
| 72 | + type={'number'} |
| 73 | + onKeyPress={this.handleNumberInput} |
| 74 | + onKeyDown={this.handleBackspace} |
| 75 | + maxLength={1} |
| 76 | + className={`input block flex-1 text-center text-lg ${index === 5 ? undefined : 'mr-6'}`} |
| 77 | + /> |
| 78 | + )) |
| 79 | + } |
| 80 | + </div> |
| 81 | + <div className={'mt-6'}> |
| 82 | + <button |
| 83 | + type={'submit'} |
| 84 | + className={'btn btn-primary btn-jumbo'} |
| 85 | + disabled={this.state.isLoading || this.state.code.length !== 6} |
| 86 | + > |
| 87 | + {this.state.isLoading ? |
| 88 | + <span className={'spinner white'}> </span> |
| 89 | + : |
| 90 | + 'Continue' |
| 91 | + } |
| 92 | + </button> |
| 93 | + </div> |
| 94 | + </form> |
| 95 | + </React.Fragment> |
| 96 | + ); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +const mapDispatchToProps = { |
| 101 | + pushFlashMessage, |
| 102 | + clearAllFlashMessages, |
| 103 | +}; |
| 104 | + |
| 105 | +export default connect(null, mapDispatchToProps)(LoginCheckpointContainer); |
0 commit comments