Skip to content
This repository was archived by the owner on Apr 5, 2024. It is now read-only.

Commit 80f0e80

Browse files
committed
WIP Adding edit password
1 parent f2d0c8a commit 80f0e80

File tree

6 files changed

+55
-31
lines changed

6 files changed

+55
-31
lines changed

src/background/api/userInformation.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,30 @@ import Axios, { AxiosResponse } from "axios";
22

33
import { hostname, userPath } from "./api";
44

5-
import { UserState } from "../redux/actions/userTypes";
65
import store from "../redux/store";
76
import {updateUser} from "../redux/actions/user";
7+
import {UserState} from "../redux/actions/userTypes";
88

9-
export const changeUserInformation = (userWithNewInformation: any): Promise<UserState> => {
9+
export interface UserInformation {
10+
userId: number | null;
11+
username?: string | null;
12+
groups?: number[]|null;
13+
password?: string;
14+
confirmationPassword?: string;
15+
}
16+
17+
export const changeUserInformation = (userWithNewInformation: UserInformation): Promise<UserState> => {
18+
console.log("User given tu update user api:")
19+
console.log(userWithNewInformation)
1020
return new Promise((resolve, reject) => {
1121
return Axios.put(`${hostname}${userPath}/${userWithNewInformation.userId}/edit`, userWithNewInformation)
1222
.then((response: AxiosResponse<UserState>) => {
1323
store.dispatch(updateUser(JSON.parse(response.config.data)));
1424
resolve(response.data)
1525
})
1626
.catch((error) => {
17-
console.log(error);
27+
console.log("[userInformation.ts] " + error);
1828
reject(error);
1929
});
2030
})
21-
2231
};

src/background/redux/actions/userTypes.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ export const ADD_USER = "ADD_USER";
22
export const UPDATE_USER = "UPDATE_USER";
33

44
export interface UserState {
5-
//TODO: how is it called id here but the object in the store has "userId"?
6-
id: number | null;
5+
userId: number | null;
76
username: string | null;
87
groups: number[];
98
}

src/background/redux/reducers/user.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {ADD_USER, UPDATE_USER, UserActionTypes, UserState} from "../actions/user
22

33
const initialState: UserState = {
44
groups: [],
5-
id: null,
5+
userId: null,
66
username: null
77
};
88

src/components/pages/User/Profile.tsx

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import React, {ReactElement, useState} from "react";
22
import {Alert, Button, Container, Row} from "react-bootstrap";
3-
import UserInformationInput, {UserInformationInterface} from "./UserInformationInput";
3+
import UserInformationInput, {UserInformationInputInterface} from "./UserInformationInput";
44
import {useSelector} from "react-redux";
55
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";
89

910

1011
export default function Profile(): ReactElement {
@@ -30,27 +31,42 @@ export default function Profile(): ReactElement {
3031
setIsEditing(!isEditing)
3132
}
3233

33-
const handleSubmit = async (newUser: UserInformationInterface) => {
34+
const handleSubmit = async (inputUser: UserInformationInputInterface) => {
3435
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) {
3741
handleAlertVisibility(DEFAULT_ALERT_DURATION, "danger", "Error: Please choose an username.")
38-
// } else if (newUser.password !== newUser.passwordConfirmation) {
42+
// } else if (inputUser.password !== inputUser.passwordConfirmation) {
3943
// 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)) {
4145
// 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;
5347
}
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+
})
5470
}
5571

5672
/*function EditProfile(): ReactElement {

src/components/pages/User/Registration.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {registerNewUser} from "../../../background/api/registration";
66
import {getWindowSize, getWindowSize_Interface} from "../../../background/methods/windowSize";
77
import {getStyleValue} from "../../../background/methods/style";
88
import {DEFAULT_ALERT_DURATION, MIN_PASSWORD_LENGTH} from "../../../background/constants";
9-
import UserInformationInput, {UserInformationInterface} from "./UserInformationInput";
9+
import UserInformationInput, {UserInformationInputInterface} from "./UserInformationInput";
1010

1111
export default function Registration(): ReactElement {
1212
const [alertMessage, setAlertMessage] = useState<string>("Error 404: No Message found.");
@@ -40,7 +40,7 @@ export default function Registration(): ReactElement {
4040
repositionSubmitLogo()
4141
}, [registrationContainer, logoSubmit])
4242

43-
const handleSubmit = async (newUser:UserInformationInterface) => {
43+
const handleSubmit = async (newUser:UserInformationInputInterface) => {
4444
console.log("[REGISTRATION] handleSubmit")
4545
if (!newUser.username) {
4646
handleAlertVisibility(DEFAULT_ALERT_DURATION, "danger", "Error: Please choose an username.")

src/components/pages/User/UserInformationInput.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ import {biggerMaxStrLength, notMinStrLength} from "../../../background/methods/c
77
import {deleteSpaces} from "../../../background/methods/strings";
88
import {DEFAULT_ALERT_DURATION, MAX_PASSWORD_LENGTH, MIN_PASSWORD_LENGTH} from "../../../background/constants";
99

10-
export interface UserInformationInterface {
10+
export interface UserInformationInputInterface {
1111
username: string,
1212
password: string,
1313
passwordConfirmation?: string
1414
}
1515

1616
type UserInformationInputProps = {
1717
triggerAlert(duration: number, color: "primary" | "secondary" | "success" | "danger" | "warning" | "info" | "light" | "dark", message: string): void,
18-
submitFunction(newUser: UserInformationInterface): void,
19-
presets?: UserInformationInterface
18+
submitFunction(newUser: UserInformationInputInterface): void,
19+
presets?: UserInformationInputInterface
2020
}
2121

2222
export default function UserInformationInput(props: UserInformationInputProps): ReactElement {

0 commit comments

Comments
 (0)