1
1
import React , { ChangeEvent , FormEvent , ReactElement , useState } from "react" ;
2
- import { Container , Row , Col , Form , FormGroup , Button } from "react-bootstrap" ;
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" ;
5
5
import info_svg from "../../assets/images/icons/material.io/info-24px.svg" ;
6
6
import check_svg from "../../assets/images/icons/material.io/check_circle-24px.svg" ;
7
7
import error_svg from "../../assets/images/icons/material.io/error-24px.svg" ;
8
+ import { registerNewUser } from "../../background/api/registration" ;
8
9
9
10
export default function Registration ( ) : ReactElement {
10
11
const MIN_PASSWORD_LENGTH = 8 ;
@@ -17,12 +18,39 @@ export default function Registration(): ReactElement {
17
18
const [ passwordInformationUppercase , setPasswordInformationUppercase ] = useState < boolean > ( false ) ;
18
19
const [ passwordInformationNumber , setPasswordInformationNumber ] = useState < boolean > ( false ) ;
19
20
const [ passwordsMatch , setPasswordsMatch ] = useState < boolean > ( false ) ;
20
- const [ errorMessage , setErrorMessage ] = useState < string > ( "" ) ;
21
+ const [ alertMessage , setAlertMessage ] = useState < string > ( "Error 404: No Message found." ) ;
22
+ const [ alertVariant , setAlertColor ] = useState < "primary" | "secondary" | "success" | "danger" | "warning" | "info" | "light" | "dark" > ( "success" ) ;
23
+ const [ alertVisibility , setAlertVisibility ] = useState < boolean > ( false ) ;
21
24
22
- const handleSubmit = ( event : FormEvent ) => {
25
+ const handleSubmit = async ( event : FormEvent ) => {
26
+ console . log ( "[REGISTRATION] handleSubmit" )
23
27
event . preventDefault ( ) ;
28
+ if ( password !== passwordConfirmation ) {
29
+ setAlertColor ( "danger" ) ;
30
+ setAlertMessage ( "Error: Password and password confirmation must match." )
31
+ handleAlertVisibility ( 3500 )
32
+ } else {
33
+ await registerNewUser ( username , password , passwordConfirmation )
34
+ . then ( res => {
35
+ setAlertMessage ( "Worked: " + res . user . username ) ;
36
+ setAlertColor ( "success" ) ;
37
+ console . table ( res ) ;
38
+ console . log ( res . user )
39
+ } )
40
+ . catch ( err => {
41
+ setAlertColor ( "danger" ) ;
42
+ setAlertMessage ( "Some error occurred: " + err )
43
+ console . log ( err )
44
+ } )
45
+ . finally ( ( ) => handleAlertVisibility ( 3500 ) )
46
+ }
47
+ }
24
48
25
- setErrorMessage ( "test" ) ;
49
+ const handleAlertVisibility = ( duration : number ) => {
50
+ setAlertVisibility ( true ) ;
51
+ setTimeout ( ( ) => {
52
+ setAlertVisibility ( false )
53
+ } , duration )
26
54
}
27
55
28
56
const handlePasswordChange = ( event : ChangeEvent < HTMLInputElement > ) => {
@@ -49,7 +77,7 @@ export default function Registration(): ReactElement {
49
77
< Row >
50
78
< Col md = { { span : 6 , offset : 3 } } >
51
79
< h1 > Create new account</ h1 >
52
- < Form onSubmit = { handleSubmit } >
80
+ < Form onSubmit = { ( ) => handleSubmit } >
53
81
< FormGroup controlId = "formBasicUsername" >
54
82
< Form . Label > Username</ Form . Label >
55
83
< Form . Control type = { "name" } value = { username }
@@ -62,22 +90,28 @@ export default function Registration(): ReactElement {
62
90
value = { password }
63
91
onChange = { ( event : ChangeEvent < HTMLInputElement > ) => handlePasswordChange ( event ) } />
64
92
< div >
65
- < img alt = { "status icon password length" } src = { passwordInformationLength ? check_svg : info_svg } />
93
+ < img alt = { "status icon password length" }
94
+ src = { passwordInformationLength ? check_svg : info_svg } />
66
95
< span className = { "sr-only" } > { passwordInformationLength ? "Done: " : "Missing: " } </ span >
67
96
< span className = { passwordInformationLength ? "text-success" : "text-muted" } > Passwords must be at least 8 characters.</ span >
68
97
</ div >
69
98
< div >
70
- < img alt = { "status icon password contains uppercase character" } src = { passwordInformationUppercase ? check_svg : info_svg } />
71
- < span className = { "sr-only" } > { passwordInformationUppercase ? "Done: " : "Missing: " } </ span >
99
+ < img alt = { "status icon password contains uppercase character" }
100
+ src = { passwordInformationUppercase ? check_svg : info_svg } />
101
+ < span
102
+ className = { "sr-only" } > { passwordInformationUppercase ? "Done: " : "Missing: " } </ span >
72
103
< span className = { passwordInformationUppercase ? "text-success" : "text-muted" } > Passwords must be at least contain 1 uppercase character.</ span >
73
104
</ div >
74
105
< div >
75
- < img alt = { "status icon password contains lowercase character" } src = { passwordInformationLowercase ? check_svg : info_svg } />
76
- < span className = { "sr-only" } > { passwordInformationLowercase ? "Done: " : "Missing: " } </ span >
106
+ < img alt = { "status icon password contains lowercase character" }
107
+ src = { passwordInformationLowercase ? check_svg : info_svg } />
108
+ < span
109
+ className = { "sr-only" } > { passwordInformationLowercase ? "Done: " : "Missing: " } </ span >
77
110
< span className = { passwordInformationLowercase ? "text-success" : "text-muted" } > Passwords must be at least contain 1 lowercase character.</ span >
78
111
</ div >
79
112
< div >
80
- < img alt = { "status icon password contains number" } src = { passwordInformationNumber ? check_svg : info_svg } />
113
+ < img alt = { "status icon password contains number" }
114
+ src = { passwordInformationNumber ? check_svg : info_svg } />
81
115
< span className = { "sr-only" } > { passwordInformationNumber ? "Done: " : "Missing: " } </ span >
82
116
< span className = { passwordInformationNumber ? "text-success" : "text-muted" } > Passwords must be at least contain 1 number.</ span >
83
117
</ div >
@@ -88,15 +122,20 @@ export default function Registration(): ReactElement {
88
122
value = { passwordConfirmation }
89
123
onChange = { ( event : ChangeEvent < HTMLInputElement > ) => handlePasswordConfirmationChange ( event ) } />
90
124
< div >
91
- < img alt = { "status icon passwords match" } src = { ! passwordConfirmation ? info_svg : passwordsMatch ? check_svg : error_svg } />
125
+ < img alt = { "status icon passwords match" }
126
+ src = { ! passwordConfirmation ? info_svg : passwordsMatch ? check_svg : error_svg } />
92
127
< span className = { "sr-only" } > { passwordsMatch ? "Done: " : "Missing: " } </ span >
93
- < span className = { ! passwordConfirmation ? "text-muted" : passwordsMatch ? "text-success" : "text-danger" } > Passwords must match.</ span >
128
+ < span
129
+ className = { ! passwordConfirmation ? "text-muted" : passwordsMatch ? "text-success" : "text-danger" } > Passwords must match.</ span >
94
130
</ div >
95
131
</ Form . Group >
96
132
< Button variant = "primary" type = "submit" >
97
133
Submit
98
134
</ Button >
99
- < p className = "text-danger" > { errorMessage } </ p >
135
+ < Alert variant = { alertVariant } onClose = { ( ) => setAlertVisibility ( false ) } show = { alertVisibility }
136
+ dismissible >
137
+ < p > { alertMessage } </ p >
138
+ </ Alert >
100
139
</ Form >
101
140
</ Col >
102
141
</ Row >
0 commit comments