Skip to content

Commit 7ee68ca

Browse files
authored
Merge pull request #29 from feliciagan/edit_profile
Edit profile
2 parents d98d3a5 + fb40dfa commit 7ee68ca

File tree

4 files changed

+30
-29
lines changed

4 files changed

+30
-29
lines changed

frontend/src/components/ChangePasswordModal/index.tsx

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
import { forwardRef, useState } from 'react';
22
import { Box, Button, Stack, Typography } from '@mui/material';
33
import PasswordTextField from '../PasswordTextField';
4-
//import { userClient } from '../../utils/api';
5-
//import axios from 'axios';
6-
//import { FAILED_PW_UPDATE_MESSAGE, SUCCESS_PW_UPDATE_MESSAGE } from '../../utils/constants';
4+
import { userClient } from '../../utils/api';
5+
import axios from 'axios';
6+
import { FAILED_PW_UPDATE_MESSAGE, SUCCESS_PW_UPDATE_MESSAGE } from '../../utils/constants';
77

88
interface ChangePasswordModalProps {
99
handleClose: () => void;
1010
userId: string;
11-
onUpdate: (message: string, isSuccess: boolean) => void;
11+
onUpdate: (isProfileEdit: boolean, message: string, isSuccess: boolean) => void;
1212
}
1313

1414
const ChangePasswordModal = forwardRef<HTMLDivElement, ChangePasswordModalProps>((props, ref) => {
15-
const { handleClose } = props;
16-
//const { handleClose, userId, onUpdate } = props;
15+
const { handleClose, userId, onUpdate } = props;
1716
const [currPassword, setCurrPassword] = useState<string>('');
1817
const [newPassword, setNewPassword] = useState<string>('');
1918
const [confirmPassword, setConfirmPassword] = useState<string>('');
@@ -25,8 +24,7 @@ const ChangePasswordModal = forwardRef<HTMLDivElement, ChangePasswordModalProps>
2524
const isUpdateDisabled = !(isCurrPasswordValid && isNewPasswordValid && isConfirmPasswordValid);
2625

2726
const handleSubmit = async () => {
28-
//TODO: test with token (only tested without)
29-
/*const accessToken = localStorage.getItem("token");
27+
const accessToken = localStorage.getItem("token");
3028

3129
try {
3230
await userClient.patch(
@@ -42,16 +40,16 @@ const ChangePasswordModal = forwardRef<HTMLDivElement, ChangePasswordModalProps>
4240
},
4341
});
4442
handleClose();
45-
onUpdate(SUCCESS_PW_UPDATE_MESSAGE, true);
43+
onUpdate(false, SUCCESS_PW_UPDATE_MESSAGE, true);
4644
} catch (error) {
4745
if (axios.isAxiosError(error)) {
4846
const message =
4947
error.response?.data.message || FAILED_PW_UPDATE_MESSAGE;
50-
onUpdate(message, false);
48+
onUpdate(false, message, false);
5149
} else {
52-
onUpdate(FAILED_PW_UPDATE_MESSAGE, false);
50+
onUpdate(false, FAILED_PW_UPDATE_MESSAGE, false);
5351
}
54-
}*/
52+
}
5553
};
5654

5755
return (

frontend/src/components/EditProfileModal/index.tsx

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
import { forwardRef, useState } from 'react';
22
import { Box, Button, FormControl, FormHelperText, Stack, TextField, Typography } from '@mui/material';
3-
//import { userClient } from '../../utils/api';
4-
//import axios from 'axios';
5-
//import { FAILED_PROFILE_UPDATE_MESSAGE, SUCCESS_PROFILE_UPDATE_MESSAGE } from '../../utils/constants';
3+
import { userClient } from '../../utils/api';
4+
import axios from 'axios';
5+
import { FAILED_PROFILE_UPDATE_MESSAGE, SUCCESS_PROFILE_UPDATE_MESSAGE } from '../../utils/constants';
66

77
interface EditProfileModalProps {
88
handleClose: () => void;
99
currFirstName: string;
1010
currLastName: string;
1111
currBiography?: string;
1212
userId: string;
13-
onUpdate: (message: string, isSuccess: boolean) => void;
13+
onUpdate: (isProfileEdit: boolean, message: string, isSuccess: boolean) => void;
1414
}
1515

1616
const EditProfileModal = forwardRef<HTMLDivElement, EditProfileModalProps>((props, ref) => {
17-
const { handleClose, currFirstName, currLastName, currBiography } = props;
18-
//const { handleClose, currFirstName, currLastName, currBiography, userId, onUpdate } = props;
17+
const { handleClose, currFirstName, currLastName, currBiography, userId, onUpdate } = props;
1918
const nameCharLimit = 50;
2019
const bioCharLimit = 255;
2120
const [newFirstName, setNewFirstName] = useState<string>(currFirstName);
@@ -38,8 +37,7 @@ const EditProfileModal = forwardRef<HTMLDivElement, EditProfileModalProps>((prop
3837
const isUpdateDisabled = firstNameError || lastNameError || !checkForChanges();
3938

4039
const handleSubmit = async () => {
41-
// TODO: test with token (only tested without)
42-
/*const accessToken = localStorage.getItem("token");
40+
const accessToken = localStorage.getItem("token");
4341

4442
try {
4543
await userClient.patch(
@@ -56,17 +54,17 @@ const EditProfileModal = forwardRef<HTMLDivElement, EditProfileModalProps>((prop
5654
},
5755
});
5856
handleClose();
59-
onUpdate(SUCCESS_PROFILE_UPDATE_MESSAGE, true);
57+
onUpdate(true, SUCCESS_PROFILE_UPDATE_MESSAGE, true);
6058
} catch (error) {
6159
console.error('Error:', error);
6260
if (axios.isAxiosError(error)) {
6361
const message =
6462
error.response?.data.message || FAILED_PROFILE_UPDATE_MESSAGE;
65-
onUpdate(message, false);
63+
onUpdate(true, message, false);
6664
} else {
67-
onUpdate(FAILED_PROFILE_UPDATE_MESSAGE, false);
65+
onUpdate(true, FAILED_PROFILE_UPDATE_MESSAGE, false);
6866
}
69-
}*/
67+
}
7068
};
7169

7270
return (

frontend/src/pages/Profile/index.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const ProfilePage: React.FC = () => {
3030
const [changePasswordOpen, setChangePasswordOpen] = useState(false);
3131
const handleChangePasswordOpen = () => setChangePasswordOpen(true);
3232
const handleChangePasswordClose = () => setChangePasswordOpen(false);
33+
const [isProfileChanged, setIsProfileChanged] = useState(false);
3334

3435
const { userId } = useParams<{ userId: string }>();
3536
const [userProfile, setUserProfile] = useState<UserProfile | null>(null);
@@ -49,7 +50,7 @@ const ProfilePage: React.FC = () => {
4950
})
5051
.catch(() => setUserProfile(null));
5152
// eslint-disable-next-line react-hooks/exhaustive-deps
52-
}, []);
53+
}, [isProfileChanged]);
5354

5455
if (!userProfile) {
5556
return (
@@ -60,9 +61,12 @@ const ProfilePage: React.FC = () => {
6061
);
6162
}
6263

63-
const notify = (message: string, isSuccess: boolean) => {
64+
const notify = (isProfileEdit: boolean, message: string, isSuccess: boolean) => {
6465
if (isSuccess) {
6566
toast.success(message);
67+
if (isProfileEdit) {
68+
setIsProfileChanged(true);
69+
}
6670
} else {
6771
toast.error(message);
6872
}

frontend/src/utils/constants.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ export const FILL_ALL_FIELDS = "Please fill in all fields";
1111
// Question
1212
export const SUCCESS_QUESTION_CREATE = "Question created successfully";
1313
export const FAILED_QUESTION_CREATE = "Failed to create question";
14-
export const NO_QUESTION_CHANGES = "You have not made any changes to the question";
14+
export const NO_QUESTION_CHANGES =
15+
"You have not made any changes to the question";
1516
export const SUCCESS_QUESTION_UPDATE = "Question updated successfully";
1617
export const FAILED_QUESTION_UPDATE = "Failed to update question";
1718
export const SUCCESS_QUESTION_DELETE = "Question deleted successfully";
@@ -20,7 +21,7 @@ export const SUCCESS_FILE_UPLOAD = "File uploaded successfully";
2021
export const FAILED_FILE_UPLOAD = "Failed to upload file";
2122

2223
// Profile
23-
/*export const SUCCESS_PW_UPDATE_MESSAGE = "Password updated successfully";
24+
export const SUCCESS_PW_UPDATE_MESSAGE = "Password updated successfully";
2425
export const FAILED_PW_UPDATE_MESSAGE = "Failed to update password";
2526
export const SUCCESS_PROFILE_UPDATE_MESSAGE = "Profile updated successfully";
26-
export const FAILED_PROFILE_UPDATE_MESSAGE = "Failed to update profile";*/
27+
export const FAILED_PROFILE_UPDATE_MESSAGE = "Failed to update profile";

0 commit comments

Comments
 (0)