1
- import React , { ReactElement , useState } from "react" ;
2
- import { Alert , Button , Container } from "react-bootstrap" ;
3
- import UserInformationInput , { UserInformationInputInterface } from "./UserInformationInput" ;
4
- import { useSelector } from "react-redux" ;
5
- import { RootState } from "../../../background/redux/store" ;
6
- import { DEFAULT_ALERT_DURATION , MIN_PASSWORD_LENGTH } from "../../../background/constants" ;
7
- import { changeUserInformation , UserInformation } from "../../../background/api/userInformation" ;
8
- import { notMinStrLength } from "../../../background/methods/checkInput" ;
1
+ import React , { ReactElement , useState } from "react" ;
2
+ import { Alert , Button , Container } from "react-bootstrap" ;
3
+ import UserInformationInput , {
4
+ UserInformationInputInterface
5
+ } from "./UserInformationInput" ;
6
+ import { useSelector } from "react-redux" ;
7
+ import { RootState } from "../../../background/redux/store" ;
8
+ import {
9
+ DEFAULT_ALERT_DURATION ,
10
+ MIN_PASSWORD_LENGTH
11
+ } from "../../../background/constants" ;
12
+ import {
13
+ changeUserInformation ,
14
+ UserInformation
15
+ } from "../../../background/api/userInformation" ;
16
+ import { notMinStrLength } from "../../../background/methods/checkInput" ;
9
17
import edit_svg from "../../../assets/images/icons/material.io/edit_white_24dp.svg" ;
10
- import { hashPassword } from "../../../background/methods/passwords" ;
11
-
18
+ import { hashPassword } from "../../../background/methods/passwords" ;
12
19
13
20
export default function Profile ( ) : ReactElement {
14
21
const [ isEditing , setIsEditing ] = useState < boolean > ( false ) ;
15
22
const user = useSelector ( ( state : RootState ) => state . user ) ;
16
- const [ alertMessage , setAlertMessage ] = useState < string > ( "Error 404: No Message found." ) ;
17
- const [ alertVariant , setAlertColor ] = useState < "primary" | "secondary" | "success" | "danger" | "warning" | "info" | "light" | "dark" > ( "success" ) ;
23
+ const [ alertMessage , setAlertMessage ] = useState < string > (
24
+ "Error 404: No Message found."
25
+ ) ;
26
+ const [ alertVariant , setAlertColor ] = useState <
27
+ | "primary"
28
+ | "secondary"
29
+ | "success"
30
+ | "danger"
31
+ | "warning"
32
+ | "info"
33
+ | "light"
34
+ | "dark"
35
+ > ( "success" ) ;
18
36
const [ alertVisibility , setAlertVisibility ] = useState < boolean > ( false ) ;
19
37
20
- const handleAlertVisibility = ( duration : number , color : "primary" | "secondary" | "success" | "danger" | "warning" | "info" | "light" | "dark" , message : string ) => {
38
+ const handleAlertVisibility = (
39
+ duration : number ,
40
+ color :
41
+ | "primary"
42
+ | "secondary"
43
+ | "success"
44
+ | "danger"
45
+ | "warning"
46
+ | "info"
47
+ | "light"
48
+ | "dark" ,
49
+ message : string
50
+ ) => {
21
51
if ( ! alertVisibility ) {
22
52
setAlertMessage ( message ) ;
23
53
setAlertColor ( color ) ;
@@ -26,99 +56,129 @@ export default function Profile(): ReactElement {
26
56
setAlertVisibility ( false ) ;
27
57
} , duration ) ;
28
58
}
29
- }
59
+ } ;
30
60
31
61
function changeEditMode ( ) : void {
32
- console . log ( "[PROFILE] changedEditMode" )
33
- setIsEditing ( ! isEditing )
62
+ console . log ( "[PROFILE] changedEditMode" ) ;
63
+ setIsEditing ( ! isEditing ) ;
34
64
}
35
65
36
66
const handleSubmit = async ( inputUser : UserInformationInputInterface ) => {
37
- console . log ( "[PROFILE] handleSubmit" )
67
+ console . log ( "[PROFILE] handleSubmit" ) ;
38
68
let newUser : UserInformation = {
39
69
groups : user . groups ,
40
70
userId : user . userId
41
- }
71
+ } ;
42
72
if ( ! inputUser . username ) {
43
- handleAlertVisibility ( DEFAULT_ALERT_DURATION , "danger" , "Error: Please choose an username." )
73
+ handleAlertVisibility (
74
+ DEFAULT_ALERT_DURATION ,
75
+ "danger" ,
76
+ "Error: Please choose an username."
77
+ ) ;
44
78
return ;
45
79
}
46
80
newUser [ "username" ] = inputUser . username ;
47
81
if ( inputUser . password || inputUser . passwordConfirmation ) {
48
82
if ( inputUser . password !== inputUser . passwordConfirmation ) {
49
- handleAlertVisibility ( DEFAULT_ALERT_DURATION , "danger" , "Error: Password and password confirmation must match." )
83
+ handleAlertVisibility (
84
+ DEFAULT_ALERT_DURATION ,
85
+ "danger" ,
86
+ "Error: Password and password confirmation must match."
87
+ ) ;
50
88
return ;
51
89
}
52
- if ( inputUser . password . match ( / \d / ) == null || inputUser . password . match ( / [ a - z ] / ) == null || inputUser . password . match ( / [ A - Z ] / ) == null || notMinStrLength ( inputUser . password , MIN_PASSWORD_LENGTH ) ) {
53
- handleAlertVisibility ( DEFAULT_ALERT_DURATION , "danger" , "Error: Please pay attention to the notes below the input fields." )
90
+ if (
91
+ inputUser . password . match ( / \d / ) == null ||
92
+ inputUser . password . match ( / [ a - z ] / ) == null ||
93
+ inputUser . password . match ( / [ A - Z ] / ) == null ||
94
+ notMinStrLength ( inputUser . password , MIN_PASSWORD_LENGTH )
95
+ ) {
96
+ handleAlertVisibility (
97
+ DEFAULT_ALERT_DURATION ,
98
+ "danger" ,
99
+ "Error: Please pay attention to the notes below the input fields."
100
+ ) ;
54
101
return ;
55
102
}
56
103
newUser [ "password" ] = await hashPassword ( inputUser . password ) ;
57
104
newUser [ "confirmationPassword" ] = newUser [ "password" ] ;
58
105
}
59
106
60
107
await changeUserInformation ( newUser )
61
- . then ( res => {
62
- changeEditMode ( )
63
- handleAlertVisibility ( DEFAULT_ALERT_DURATION , "success" , "Worked: " + ( res ) ) ;
64
- } )
65
- . catch ( err => {
66
- console . log ( "Error: " + err )
67
- handleAlertVisibility ( DEFAULT_ALERT_DURATION , "danger" , "Error: " + err )
108
+ . then ( ( res ) => {
109
+ changeEditMode ( ) ;
110
+ handleAlertVisibility (
111
+ DEFAULT_ALERT_DURATION ,
112
+ "success" ,
113
+ "Worked: " + res
114
+ ) ;
68
115
} )
69
- }
116
+ . catch ( ( err ) => {
117
+ console . log ( "Error:" + err ) ;
118
+ handleAlertVisibility (
119
+ DEFAULT_ALERT_DURATION ,
120
+ "danger" ,
121
+ "Error: " + err
122
+ ) ;
123
+ } ) ;
124
+ } ;
70
125
71
126
function EditProfile ( ) : ReactElement {
72
127
return (
73
128
< >
74
129
< UserInformationInput
75
130
triggerAlert = { handleAlertVisibility }
76
131
submitFunction = { handleSubmit }
77
- presets = { { username : user . username ?? "" , password : "" } }
132
+ presets = { { username : user . username ?? "" , password : "" } }
78
133
/>
79
- < Alert variant = { alertVariant } onClose = { ( ) => setAlertVisibility ( false ) } show = { alertVisibility }
80
- dismissible >
134
+ < Alert
135
+ variant = { alertVariant }
136
+ onClose = { ( ) => setAlertVisibility ( false ) }
137
+ show = { alertVisibility }
138
+ dismissible
139
+ >
81
140
< p > { alertMessage } </ p >
82
141
</ Alert >
83
142
</ >
84
- )
143
+ ) ;
85
144
}
86
145
87
146
function DisplayProfile ( ) : ReactElement {
88
147
return (
89
148
< div className = "profile-information-display p-0 w-100" >
90
- < h2 className = "h3 pb-3" >
91
- { user . username }
92
- </ h2 >
149
+ < h2 className = "h3 pb-3" > { user . username } </ h2 >
93
150
< dl >
94
151
< dt > Username</ dt >
95
152
< dd > { user . username } </ dd >
96
153
< dt > Groups</ dt >
97
- < dd > { user . groups ?. map ( ( value : number ) => {
98
- return value + " "
99
- } ) }
154
+ < dd >
155
+ { user . groups ?. map ( ( value : string ) => {
156
+ return value + " " ;
157
+ } ) }
100
158
</ dd >
101
159
</ dl >
102
160
</ div >
103
- )
161
+ ) ;
104
162
}
105
163
106
164
return (
107
165
< Container className = "page-content" >
108
166
< div className = "px-1 w-100 mt-1 mb-3 " >
109
167
< div className = "w-100 title-action" >
110
- < h1 className = "mr-1 h4" >
111
- My Profile
112
- </ h1 >
113
- < Button
114
- onClick = { changeEditMode }
115
- >
116
- { ! isEditing && < img src = { edit_svg } alt = { "edit icon" } className = "pr-2" /> }
168
+ < h1 className = "mr-1 h4" > My Profile</ h1 >
169
+ < Button onClick = { changeEditMode } >
170
+ { ! isEditing && (
171
+ < img
172
+ src = { edit_svg }
173
+ alt = { "edit icon" }
174
+ className = "pr-2"
175
+ />
176
+ ) }
117
177
{ isEditing ? "Cancel" : "Edit" }
118
178
</ Button >
119
179
</ div >
120
180
</ div >
121
- { isEditing ? < EditProfile /> : < DisplayProfile /> }
181
+ { isEditing ? < EditProfile /> : < DisplayProfile /> }
122
182
</ Container >
123
183
) ;
124
184
}
0 commit comments