1
1
import React , { ReactElement , useState } from "react" ;
2
2
import { Alert , Button , Container , Row } from "react-bootstrap" ;
3
- import UserInformationInput , { UserInformationInterface } from "./UserInformationInput" ;
3
+ import UserInformationInput , { UserInformationInputInterface } from "./UserInformationInput" ;
4
4
import { useSelector } from "react-redux" ;
5
5
import { RootState } from "../../../background/redux/store" ;
6
- import { DEFAULT_ALERT_DURATION } from "../../../background/constants" ;
7
- import { changeUserInformation } from "../../../background/api/userInformation" ;
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" ;
8
9
9
10
10
11
export default function Profile ( ) : ReactElement {
@@ -30,27 +31,42 @@ export default function Profile(): ReactElement {
30
31
setIsEditing ( ! isEditing )
31
32
}
32
33
33
- const handleSubmit = async ( newUser : UserInformationInterface ) => {
34
+ const handleSubmit = async ( inputUser : UserInformationInputInterface ) => {
34
35
console . log ( "[PROFILE] handleSubmit" )
35
-
36
- if ( ! newUser . username ) {
36
+ let newUser :UserInformation = {
37
+ groups : user . groups ,
38
+ userId : user . userId
39
+ }
40
+ if ( ! inputUser . username ) {
37
41
handleAlertVisibility ( DEFAULT_ALERT_DURATION , "danger" , "Error: Please choose an username." )
38
- // } else if (newUser .password !== newUser .passwordConfirmation) {
42
+ // } else if (inputUser .password !== inputUser .passwordConfirmation) {
39
43
// handleAlertVisibility(DEFAULT_ALERT_DURATION, "danger", "Error: Password and password confirmation must match.")
40
- // } else if (newUser .password.match(/\d/) == null || newUser .password.match(/[a-z]/) == null || newUser .password.match(/[A-Z]/) == null || notMinStrLength(newUser .password, MIN_PASSWORD_LENGTH)) {
44
+ // } else if (inputUser .password.match(/\d/) == null || inputUser .password.match(/[a-z]/) == null || inputUser .password.match(/[A-Z]/) == null || notMinStrLength(inputUser .password, MIN_PASSWORD_LENGTH)) {
41
45
// handleAlertVisibility(DEFAULT_ALERT_DURATION, "danger", "Error: Please pay attention to the notes below the input fields.")
42
- } else {
43
- // console.log("Hello there ", {...user, username: newUser.username})
44
- // console.log("General Kenobi ", newUser)
45
- await changeUserInformation ( { ...user , username : newUser . username } )
46
- . then ( res => {
47
- changeEditMode ( )
48
- handleAlertVisibility ( DEFAULT_ALERT_DURATION , "success" , "Worked: " + ( res ) ) ;
49
- } )
50
- . catch ( err => {
51
- handleAlertVisibility ( DEFAULT_ALERT_DURATION , "danger" , "Error: " + ( err . outputMessage ? err . outputMessage : ( err . httpStatus + " " + err . httpMessage ) ) )
52
- } )
46
+ return ;
53
47
}
48
+ newUser [ "username" ] = inputUser . username ;
49
+ if ( inputUser . password || inputUser . passwordConfirmation ) {
50
+ if ( inputUser . password !== inputUser . passwordConfirmation ) {
51
+ handleAlertVisibility ( DEFAULT_ALERT_DURATION , "danger" , "Error: Password and password confirmation must match." )
52
+ return ;
53
+ }
54
+ if ( inputUser . password . match ( / \d / ) == null || inputUser . password . match ( / [ a - z ] / ) == null || inputUser . password . match ( / [ A - Z ] / ) == null || notMinStrLength ( inputUser . password , MIN_PASSWORD_LENGTH ) ) {
55
+ handleAlertVisibility ( DEFAULT_ALERT_DURATION , "danger" , "Error: Please pay attention to the notes below the input fields." )
56
+ return ;
57
+ }
58
+ newUser [ "password" ] = inputUser . password
59
+ newUser [ "confirmationPassword" ] = inputUser . passwordConfirmation
60
+ }
61
+
62
+ await changeUserInformation ( newUser )
63
+ . then ( res => {
64
+ changeEditMode ( )
65
+ handleAlertVisibility ( DEFAULT_ALERT_DURATION , "success" , "Worked: " + ( res ) ) ;
66
+ } )
67
+ . catch ( err => {
68
+ handleAlertVisibility ( DEFAULT_ALERT_DURATION , "danger" , "Error: " + ( err . outputMessage ? err . outputMessage : ( err . httpStatus + " " + err . httpMessage ) ) )
69
+ } )
54
70
}
55
71
56
72
/*function EditProfile(): ReactElement {
0 commit comments