1
- import React , { ChangeEvent , FormEvent , ReactElement , useState } from "react" ;
1
+ import React , { ChangeEvent , FormEvent , ReactElement , useEffect , useState } from "react" ;
2
2
import { Container , Row , Col , Form , FormGroup , Button , Alert } from "react-bootstrap" ;
3
3
import { deleteSpaces } from "../../background/methods/strings" ;
4
4
import { notMinStrLength } from "../../background/methods/checkInput" ;
@@ -17,30 +17,42 @@ export default function Registration(): ReactElement {
17
17
const [ passwordInformationLowercase , setPasswordInformationLowercase ] = useState < boolean > ( false ) ;
18
18
const [ passwordInformationUppercase , setPasswordInformationUppercase ] = useState < boolean > ( false ) ;
19
19
const [ passwordInformationNumber , setPasswordInformationNumber ] = useState < boolean > ( false ) ;
20
- const [ passwordsMatch , setPasswordsMatch ] = useState < boolean > ( false ) ;
20
+ const [ passwordsMatch , setPasswordsMatch ] = useState < boolean > ( true ) ;
21
21
const [ alertMessage , setAlertMessage ] = useState < string > ( "Error 404: No Message found." ) ;
22
22
const [ alertVariant , setAlertColor ] = useState < "primary" | "secondary" | "success" | "danger" | "warning" | "info" | "light" | "dark" > ( "success" ) ;
23
23
const [ alertVisibility , setAlertVisibility ] = useState < boolean > ( false ) ;
24
24
25
+ useEffect ( ( ) => {
26
+ reviewPasswordMatch ( )
27
+ } , [ passwordConfirmation , password ] )
28
+
25
29
const handleSubmit = async ( event : FormEvent ) => {
26
30
console . log ( "[REGISTRATION] handleSubmit" )
27
31
event . preventDefault ( ) ;
28
- if ( password !== passwordConfirmation ) {
32
+ reviewPasswordMatch ( ) ;
33
+ if ( ! username ) {
34
+ setAlertColor ( "danger" ) ;
35
+ setAlertMessage ( "Error: Please choose an username." )
36
+ handleAlertVisibility ( 3500 )
37
+ } else if ( ! passwordsMatch ) {
29
38
setAlertColor ( "danger" ) ;
30
39
setAlertMessage ( "Error: Password and password confirmation must match." )
31
40
handleAlertVisibility ( 3500 )
41
+ } else if ( ! passwordInformationNumber || ! passwordInformationLowercase || ! passwordInformationUppercase || ! passwordInformationLength ) {
42
+ setAlertColor ( "danger" ) ;
43
+ setAlertMessage ( "Error: Please pay attention to the notes below the input field." ) ;
44
+ handleAlertVisibility ( 3500 )
32
45
} else {
33
46
await registerNewUser ( username , password , passwordConfirmation )
34
47
. then ( res => {
35
- setAlertMessage ( "Worked: " + res . user . username ) ;
48
+ setAlertMessage ( "Worked: " + ( res . outputMessage ? res . outputMessage : ( res . httpStatus + " " + res . httpMessage ) ) ) ;
36
49
setAlertColor ( "success" ) ;
37
50
console . table ( res ) ;
38
- console . log ( res . user )
39
51
} )
40
52
. catch ( err => {
41
53
setAlertColor ( "danger" ) ;
42
- setAlertMessage ( "Some error occurred: " + err )
43
- console . log ( err )
54
+ setAlertMessage ( "Error: " + ( err . outputMessage ? err . outputMessage : ( err . httpStatus + " " + err . httpMessage ) ) )
55
+ console . table ( err )
44
56
} )
45
57
. finally ( ( ) => handleAlertVisibility ( 3500 ) )
46
58
}
@@ -66,14 +78,17 @@ export default function Registration(): ReactElement {
66
78
setPassword ( value )
67
79
}
68
80
69
- const handlePasswordConfirmationChange = ( event : ChangeEvent < HTMLInputElement > ) => {
81
+ const handlePasswordConfirmationChange = async ( event : ChangeEvent < HTMLInputElement > ) => {
70
82
event . preventDefault ( ) ;
71
83
let value = event . target . value ;
72
84
value = deleteSpaces ( value ) ;
73
- setPasswordsMatch ( password === value ) ;
74
85
setPasswordConfirmation ( value ) ;
75
86
}
76
87
88
+ const reviewPasswordMatch = ( ) :void => {
89
+ setPasswordsMatch ( password === passwordConfirmation ) ;
90
+ }
91
+
77
92
return (
78
93
< Container >
79
94
< Row >
0 commit comments