@@ -4,7 +4,10 @@ import SignUpWrapper from './SignUpWrapper';
44import LostAllDevicesWrapper from './LostAllDevicesWrapper' ;
55import ContinueLogin from './ContinueLogin' ;
66import { closeDialog , confirmLostDevices } from './../utils/electronInterface' ;
7- import { validateUsername } from './../validators/validators' ;
7+ import {
8+ validateUsername ,
9+ checkUsernameAvailable
10+ } from './../validators/validators' ;
811
912const mode = {
1013 SIGNUP : 'SIGNUP' ,
@@ -13,6 +16,10 @@ const mode = {
1316 LOST_DEVICES : 'LOST_DEVICES'
1417} ;
1518
19+ const errorMessages = {
20+ USERNAME_NOT_EXISTS : "Username doesn't exists"
21+ } ;
22+
1623class LoginWrapper extends Component {
1724 constructor ( ) {
1825 super ( ) ;
@@ -21,13 +28,14 @@ class LoginWrapper extends Component {
2128 values : {
2229 username : ''
2330 } ,
24- disabled : true
31+ disabled : true ,
32+ errorMessage : ''
2533 } ;
2634 this . timeCountdown = 0 ;
2735 }
2836
29- componentDidMount ( ) {
30- this . checkDisable ( ) ;
37+ async componentDidMount ( ) {
38+ await this . checkDisable ( ) ;
3139 }
3240
3341 render ( ) {
@@ -55,9 +63,9 @@ class LoginWrapper extends Component {
5563 onClickSignIn = { this . handleClickSignIn }
5664 onChangeField = { this . handleChange }
5765 disabled = { this . state . disabled }
58- validator = { this . validateUsername }
5966 value = { this . state . values . username }
6067 handleLostDevices = { this . handleLostDevices }
68+ errorMessage = { this . state . errorMessage }
6169 />
6270 ) ;
6371 }
@@ -66,42 +74,47 @@ class LoginWrapper extends Component {
6674 toggleSignUp = ev => {
6775 ev . preventDefault ( ) ;
6876 ev . stopPropagation ( ) ;
69- this . setState ( {
70- mode : this . state . mode === mode . LOGIN ? mode . SIGNUP : mode . LOGIN
77+ const nextMode = this . state . mode === mode . LOGIN ? mode . SIGNUP : mode . LOGIN ;
78+ this . setState ( { mode : nextMode } , async ( ) => {
79+ await this . checkDisable ( ) ;
7180 } ) ;
72- this . checkDisable ( ) ;
7381 } ;
7482
7583 toggleContinue = ev => {
7684 ev . preventDefault ( ) ;
7785 ev . stopPropagation ( ) ;
7886 this . stopCountdown ( ) ;
79- this . setState ( {
80- mode : this . state . mode === mode . LOGIN ? mode . CONTINUE : mode . LOGIN
87+ const nextMode =
88+ this . state . mode === mode . LOGIN ? mode . CONTINUE : mode . LOGIN ;
89+ this . setState ( { mode : nextMode } , async ( ) => {
90+ await this . checkDisable ( ) ;
8191 } ) ;
82- this . checkDisable ( ) ;
8392 } ;
8493
8594 toggleLostAllDevices = ev => {
8695 ev . preventDefault ( ) ;
8796 ev . stopPropagation ( ) ;
88- this . setState ( {
89- mode : mode . LOGIN
90- } ) ;
91- this . checkDisable ( ) ;
97+ this . setState (
98+ {
99+ mode : mode . LOGIN
100+ } ,
101+ async ( ) => {
102+ await this . checkDisable ( ) ;
103+ }
104+ ) ;
92105 } ;
93106
94107 stopCountdown = ( ) => {
95108 clearTimeout ( this . timeCountdown ) ;
96109 } ;
97110
98- validateUsername = ( ) => {
111+ validateUsername = async ( ) => {
99112 const username = this . state . values [ 'username' ] ;
100- return validateUsername ( username ) ;
113+ return await validateUsername ( username ) ;
101114 } ;
102115
103- checkDisable = ( ) => {
104- const isValid = this . validateUsername ( ) ;
116+ checkDisable = async ( ) => {
117+ const isValid = await this . validateUsername ( ) ;
105118 this . setState ( {
106119 disabled : ! isValid
107120 } ) ;
@@ -110,17 +123,26 @@ class LoginWrapper extends Component {
110123 handleChange = event => {
111124 const values = { ...this . state . values } ;
112125 values [ event . target . name ] = event . target . value ;
113- this . setState ( { values } , ( ) => {
114- this . checkDisable ( ) ;
126+ this . setState ( { values, errorMessage : '' } , async ( ) => {
127+ await this . checkDisable ( ) ;
115128 } ) ;
116129 } ;
117130
118- handleClickSignIn = event => {
119- event . preventDefault ( ) ;
120- event . stopPropagation ( ) ;
121- this . setState ( {
122- mode : mode . LOST_DEVICES
123- } ) ;
131+ handleClickSignIn = async ev => {
132+ ev . preventDefault ( ) ;
133+ ev . stopPropagation ( ) ;
134+ const isAvailable = await checkUsernameAvailable (
135+ this . state . values . username
136+ ) ;
137+ if ( isAvailable ) {
138+ this . setState ( {
139+ errorMessage : errorMessages . USERNAME_NOT_EXISTS
140+ } ) ;
141+ } else {
142+ this . setState ( {
143+ mode : mode . LOST_DEVICES
144+ } ) ;
145+ }
124146 } ;
125147
126148 handleLostDevices = event => {
0 commit comments