Skip to content

Commit

Permalink
editProfile function now uses async await instead of regular Promises
Browse files Browse the repository at this point in the history
  • Loading branch information
jimbandrews committed Oct 25, 2023
1 parent 84af870 commit 4ef496c
Showing 1 changed file with 63 additions and 53 deletions.
116 changes: 63 additions & 53 deletions src/pages/Profile/EditProfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,22 @@ export default function EditProfile({ token, pk, setAuth }) {
});
};

const editProfile = (e) => {
const navigateToProfile = async () => {
try {
await axios.get(
`${process.env.REACT_APP_BE_URL}/myprofile/`,
{ headers: { Authorization: `Token ${token}` } },
);

setLoading(false);
navigate('/profile');
} catch (err) {
setLoading(false);
setError(err.message);
}
}

const editProfile = async (e) => {
e.preventDefault();
setError("");
setLoading(true);
Expand All @@ -140,68 +155,63 @@ export default function EditProfile({ token, pk, setAuth }) {
formData.append("profile_photo", profilePhoto);
}

axios
.patch(
const patchProfile = () => {
axios.patch(
`${process.env.REACT_APP_BE_URL}/myprofile/`,
formData && formData, // Only pass form data if it exists
{
headers: {
Authorization: `Token ${token}`,
"Content-Type": "multipart/form-data",
},
},
);
}

const patchMentorInfo = () => {
axios.patch(
`${process.env.REACT_APP_BE_URL}/mentorinfoupdate/`,
{
skills: skills,
about_me: aboutMe,
team_number: teamNumber,
},
{
headers: {
Authorization: `Token ${token}`,
"Content-Type": "application/json",
},
}
)
.then((res) => {
if (isMentor) {
axios.patch(
`${process.env.REACT_APP_BE_URL}/mentorinfoupdate/`,
{
skills: skills,
about_me: aboutMe,
team_number: teamNumber,
},
{
headers: {
Authorization: `Token ${token}`,
"Content-Type": "application/json",
},
}
);
} else if (isMentee) {
axios.patch(
`${process.env.REACT_APP_BE_URL}/menteeinfoupdate/`,
{
team_number: teamNumber,
},
{
headers: {
Authorization: `Token ${token}`,
"Content-Type": "application/json",
},
}
);
);
}

const patchMenteeInfo = () => {
axios.patch(
`${process.env.REACT_APP_BE_URL}/menteeinfoupdate/`,
{
team_number: teamNumber,
},
{
headers: {
Authorization: `Token ${token}`,
"Content-Type": "application/json",
},
}
})
);
}

.then((res) => {
// const token = res.data.auth_token;
axios
.get(`${process.env.REACT_APP_BE_URL}/myprofile/`, {
headers: { Authorization: `Token ${token}` },
})
.then((res) => {
setLoading(false);
navigate("/profile");
})
.catch((e) => {
setLoading(false);
setError(e.message);
});
})
.catch((e) => {
setLoading(false);
setError(e.message);
});
try {
if (isMentor) {
await Promise.all([patchProfile(), patchMentorInfo()]);
} else if (isMentee) {
await Promise.all([patchProfile(), patchMenteeInfo()]);
}

navigateToProfile();
} catch (err) {
setLoading(false);
setError(e.message);
}
};

return (
Expand Down

0 comments on commit 4ef496c

Please sign in to comment.