@@ -3,6 +3,10 @@ import * as antd from "antd"
33
44import AuthModel from "@models/auth"
55
6+ const isValidEmailFormat = ( email ) => {
7+ return / ^ [ ^ \s @ ] + @ [ ^ \s @ ] + \. [ ^ \s @ ] + $ / . test ( email )
8+ }
9+
610const EmailStepComponent = ( props ) => {
711 const [ email , setEmail ] = React . useState ( props . currentValue ?? "" )
812
@@ -14,10 +18,6 @@ const EmailStepComponent = (props) => {
1418 return email . length > 0 && validFormat && emailAvailable
1519 }
1620
17- const checkIfIsEmail = ( email ) => {
18- return / ^ [ ^ \s @ ] + @ [ ^ \s @ ] + \. [ ^ \s @ ] + $ / . test ( email )
19- }
20-
2121 const submit = ( ) => {
2222 if ( ! isValid ( ) ) {
2323 return false
@@ -31,36 +31,35 @@ const EmailStepComponent = (props) => {
3131 }
3232
3333 React . useEffect ( ( ) => {
34- if ( email . length === 0 ) {
35- setEmailAvailable ( null )
36- setValidFormat ( null )
34+ props . updateValue ( null )
35+ setEmailAvailable ( null )
36+ setValidFormat ( null )
37+
38+ const validFormat = isValidEmailFormat ( email )
39+ setValidFormat ( validFormat )
3740
41+ if ( email . length === 0 || ! validFormat ) {
3842 return
3943 }
4044
41- props . updateValue ( null )
42-
4345 setLoading ( true )
4446
45- const isEmailValid = checkIfIsEmail ( email )
46- setValidFormat ( isEmailValid )
47-
4847 // check if email is available
4948 const timer = setTimeout ( async ( ) => {
50- if ( ! isEmailValid ) {
51- return false
52- }
53-
54- const request = await AuthModel . availability ( { email } ) . catch ( ( error ) => {
55- antd . message . error ( `Cannot check email availability: ${ error . message } ` )
49+ const request = await AuthModel . availability ( { email } ) . catch (
50+ ( error ) => {
51+ antd . message . error (
52+ `Cannot check email availability: ${ error . message } ` ,
53+ )
5654
57- return false
58- } )
55+ return false
56+ } ,
57+ )
5958
6059 if ( request ) {
61- setEmailAvailable ( ! request . exist )
60+ setEmailAvailable ( ! request . exists )
6261
63- if ( request . exist ) {
62+ if ( request . exists ) {
6463 antd . message . error ( "Email is already in use" )
6564 props . updateValue ( null )
6665 } else {
0 commit comments