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

Commit f2d0c8a

Browse files
committed
Added EditFunctionality in Profile.tsx
Yet unfixed elements: - change of username only changes displayed name, not credentials - no pw change
1 parent 187fcb6 commit f2d0c8a

File tree

4 files changed

+34
-13
lines changed

4 files changed

+34
-13
lines changed

src/background/api/userInformations.ts renamed to src/background/api/userInformation.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@ import store from "../redux/store";
77
import {updateUser} from "../redux/actions/user";
88

99
export const changeUserInformation = (userWithNewInformation: any): Promise<UserState> => {
10-
console.log("[userInformations] changeUserInformations: user: ", userWithNewInformation);
11-
return new Promise<any>((resolve, reject) => {
12-
return Axios.put<any>(`${hostname}${userPath}/${userWithNewInformation.userId}/edit`, userWithNewInformation)
10+
return new Promise((resolve, reject) => {
11+
return Axios.put(`${hostname}${userPath}/${userWithNewInformation.userId}/edit`, userWithNewInformation)
1312
.then((response: AxiosResponse<UserState>) => {
14-
console.log("answer: ", response.data)
15-
store.dispatch(updateUser(response.data));
13+
store.dispatch(updateUser(JSON.parse(response.config.data)));
14+
resolve(response.data)
1615
})
1716
.catch((error) => {
1817
console.log(error);

src/background/redux/actions/userTypes.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +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"?
56
id: number | null;
67
username: string | null;
78
groups: number[];

src/components/pages/User/Profile.tsx

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import {Alert, Button, Container, Row} from "react-bootstrap";
33
import UserInformationInput, {UserInformationInterface} 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";
68

79

810
export default function Profile(): ReactElement {
@@ -30,8 +32,25 @@ export default function Profile(): ReactElement {
3032

3133
const handleSubmit = async (newUser: UserInformationInterface) => {
3234
console.log("[PROFILE] handleSubmit")
33-
//console.log(changeUserInformation(newUser))
34-
changeEditMode()
35+
36+
if (!newUser.username) {
37+
handleAlertVisibility(DEFAULT_ALERT_DURATION, "danger", "Error: Please choose an username.")
38+
// } else if (newUser.password !== newUser.passwordConfirmation) {
39+
// 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)) {
41+
// 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+
})
53+
}
3554
}
3655

3756
/*function EditProfile(): ReactElement {
@@ -82,7 +101,7 @@ export default function Profile(): ReactElement {
82101
<dt>Username</dt>
83102
<dd>{user.username}</dd>
84103
<dt>Groups</dt>
85-
<dd>{user.groups.map((value: number) => {
104+
<dd>{user.groups?.map((value: number) => {
86105
return value + " "
87106
})}
88107
</dd>
@@ -98,12 +117,14 @@ export default function Profile(): ReactElement {
98117
<h1 className="mr-1 h4">
99118
My Profile
100119
</h1>
101-
{isEditing ? <></> : <Button onClick={changeEditMode}
102-
disabled={isEditing}>{isEditing ? "Editing" : "Edit Information"}</Button>}
120+
<Button
121+
onClick={changeEditMode}
122+
>
123+
{isEditing ? "Cancel" : "Edit"}
124+
</Button>
103125
</Row>
104126
</Container>
105127
{isEditing ? <EditProfile/> : <DisplayProfile/>}
106-
107128
</Container>
108129
);
109130
}

src/components/pages/User/UserInformationInput.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ export interface UserInformationInterface {
1313
passwordConfirmation?: string
1414
}
1515

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

22-
export default function UserInformationInput(props: Props): ReactElement {
22+
export default function UserInformationInput(props: UserInformationInputProps): ReactElement {
2323
let {triggerAlert, submitFunction, presets} = props;
2424
const [username, setUsername] = useState<string>(presets?.username??"");
2525
const [password, setPassword] = useState<string>(presets?.password??"");

0 commit comments

Comments
 (0)