1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
// @flow
import React, { Component } from 'react';
import { If, Then } from 'react-if';
import { Layout, Container, Header } from './Layout';
import AccountInput from './AccountInput';
import ExternalLinkSVG from '../assets/images/icon-extLink.svg';
import LoginArrowSVG from '../assets/images/icon-arrow.svg';
import type { AccountReduxState, LoginState } from '../redux/account/reducers';
export type LoginPropTypes = {
account: AccountReduxState,
onLogin: (accountNumber: string) => void,
onSettings: ?(() => void),
onChange: (input: string) => void,
onFirstChangeAfterFailure: () => void,
onExternalLink: (type: string) => void,
};
export default class Login extends Component {
props: LoginPropTypes;
state = {
notifyOnFirstChangeAfterFailure: false,
isActive: false
}
onCreateAccount = () => this.props.onExternalLink('createAccount');
onFocus = () => this.setState({ isActive: true });
onBlur = () => this.setState({ isActive: false });
onLogin = () => {
const { accountNumber } = this.props.account;
if(accountNumber && accountNumber.length > 0) {
this.props.onLogin(accountNumber);
}
}
onInputChange = (val: string) => {
// notify delegate on first change after login failure
if(this.state.notifyOnFirstChangeAfterFailure) {
this.setState({ notifyOnFirstChangeAfterFailure: false });
this.props.onFirstChangeAfterFailure();
}
this.props.onChange(val);
}
formTitle(s: LoginState): string {
switch(s) {
case 'connecting': return 'Logging in...';
case 'failed': return 'Login failed';
case 'ok': return 'Login successful';
default: return 'Login';
}
}
formSubtitle(s: LoginState, e: ?Error): string {
switch(s) {
case 'failed': return (e && e.message) || 'Unknown error';
case 'connecting': return 'Checking account number';
default: return 'Enter your account number';
}
}
inputWrapClass(s: LoginState): string {
const classes = ['login-form__input-wrap'];
if(this.state.isActive) {
classes.push('login-form__input-wrap--active');
}
switch(s) {
case 'connecting':
classes.push('login-form__input-wrap--inactive');
break;
case 'failed':
classes.push('login-form__input-wrap--error');
break;
}
return classes.join(' ');
}
footerClass(s: LoginState): string {
const classes = ['login-footer'];
switch(s) {
case 'ok':
case 'connecting':
classes.push('login-footer--invisible');
break;
}
return classes.join(' ');
}
submitClass(s: LoginState, accountNumber: ?string): string {
const classes = ['login-form__submit'];
if(accountNumber && accountNumber.length > 0) {
classes.push('login-form__submit--active');
}
if(s === 'connecting') {
classes.push('login-form__submit--invisible');
}
return classes.join(' ');
}
componentWillReceiveProps(nextProps: LoginPropTypes) {
const prev = this.props.account || {};
const next = nextProps.account || {};
if(prev.status !== next.status && next.status === 'failed') {
this.setState({ notifyOnFirstChangeAfterFailure: true });
}
}
render(): React.Element<*> {
const { accountNumber, status, error } = this.props.account;
const title = this.formTitle(status);
const subtitle = this.formSubtitle(status, error);
let isConnecting = false;
let isFailed = false;
let isLoggedIn = false;
switch(status) {
case 'connecting': isConnecting = true; break;
case 'failed': isFailed = true; break;
case 'ok': isLoggedIn = true; break;
}
const inputWrapClass = this.inputWrapClass(status);
const footerClass = this.footerClass(status);
const submitClass = this.submitClass(status, accountNumber);
const autoFocusRef = input => {
if(isFailed && input) {
input.focus();
}
};
return (
<Layout>
<Header showSettings={ true } onSettings={ this.props.onSettings } />
<Container>
<div className="login">
<div className="login-form">
{ /* show spinner when connecting */ }
<If condition={ isConnecting }>
<Then>
<div className="login-form__status-icon">
<img src="./assets/images/icon-spinner.svg" alt="" />
</div>
</Then>
</If>
{ /* show error icon when failed */ }
<If condition={ isFailed }>
<Then>
<div className="login-form__status-icon">
<img src="./assets/images/icon-fail.svg" alt="" />
</div>
</Then>
</If>
{ /* show tick when logged in */ }
<If condition={ isLoggedIn }>
<Then>
<div className="login-form__status-icon">
<img src="./assets/images/icon-success.svg" alt="" />
</div>
</Then>
</If>
<div className="login-form__title">{ title }</div>
<div className={ 'login-form__fields' + (isLoggedIn ? ' login-form__fields--invisible' : '') }>
<div className="login-form__subtitle">{ subtitle }</div>
<div className={ inputWrapClass }>
<AccountInput className="login-form__input-field"
type="text"
placeholder="e.g 0000 0000 0000"
onFocus={ this.onFocus }
onBlur={ this.onBlur }
onChange={ this.onInputChange }
onEnter={ this.onLogin }
value={ accountNumber || '' }
disabled={ isConnecting }
autoFocus={ true }
ref={ autoFocusRef } />
<button className={ submitClass } onClick={ this.onLogin }>
<LoginArrowSVG className="login-form__submit-icon" />
</button>
</div>
</div>
</div>
<div className={ footerClass }>
<div className="login-footer__prompt">{ 'Don\'t have an account number?' }</div>
<button className="button button--primary" onClick={ this.onCreateAccount }>
<span className="button-label">Create account</span>
<ExternalLinkSVG className="button-icon button-icon--16" />
</button>
</div>
</div>
</Container>
</Layout>
);
}
}
|