Skip to content

Commit

Permalink
Merge pull request #236 from jimbandrews/EditMentorTeamAsyncAwait
Browse files Browse the repository at this point in the history
Edit mentor team async await
  • Loading branch information
GitLukeW committed Oct 25, 2023
2 parents f571e2e + 4ef496c commit a7a5e83
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 104 deletions.
231 changes: 127 additions & 104 deletions src/pages/Profile/EditProfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,49 +53,62 @@ export default function EditProfile({ token, pk, setAuth }) {
];

useEffect(() => {
setLoading(true);
axios
.get(`${process.env.REACT_APP_BE_URL}/myprofile/`, {
headers: { Authorization: `Token ${token}` },
})
.then((res) => {
const getMentorInfo = async () => {
try {
const mentorInfo = await axios.get(
`${process.env.REACT_APP_BE_URL}/mentorinfo/`,
{ headers: { Authorization: `Token ${ token }` } },
);

setSkills(mentorInfo.data[0].skills);
setAboutMe(mentorInfo.data[0].about_me);
setTeamNumber(mentorInfo.data[0].team_number);
} catch (err) {
console.error(err);
}
}

const getMenteeInfo = async () => {
try {
const menteeInfo = await axios.get(
`${process.env.REACT_APP_BE_URL}/menteeinfo/`,
{ headers: { Authorization: `Token ${ token }` } },
);

setTeamNumber(menteeInfo.data[0].team_number);
} catch (err) {
console.error(err);
}
}

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

setLoading(false);
setOriginalProfile(res.data);
setFirstName(res.data.first_name);
setLastName(res.data.last_name);
setPhoneNumber(res.data.phone_number);
setIsMentor(res.data.is_mentor);
setIsMentee(res.data.is_mentee);
setOriginalProfile(profileData.data);
setFirstName(profileData.data.first_name);
setLastName(profileData.data.last_name);
setPhoneNumber(profileData.data.phone_number);
setIsMentor(profileData.data.is_mentor);
setIsMentee(profileData.data.is_mentee);

if (res.data.is_mentor) {
axios
.get(`${process.env.REACT_APP_BE_URL}/mentorinfo/`, {
headers: { Authorization: `Token ${token}` },
})
.then((res) => {
setSkills(res.data[0].skills);
setAboutMe(res.data[0].about_me);
})
.catch((e) => {
console.error(e);
});
} else if (res.data.is_mentee) {
axios
.get(`${process.env.REACT_APP_BE_URL}/menteeinfo/`, {
headers: { Authorization: `Token ${token}` },
})
.then((res) => {
setTeamNumber(res.data[0].team_number);
})
.catch((e) => {
console.error(e);
});
if (profileData.data.is_mentor) {
getMentorInfo();
} else if (profileData.data.is_mentee) {
getMenteeInfo();
}
})
.catch((e) => {
} catch (err) {
setLoading(false);
setError(e.message);
});
setError(err.message);
}
}

setLoading(true);
getProfileData();
}, [token]);

const handleChange = (event) => {
Expand All @@ -108,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 @@ -127,67 +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 (skills && aboutMe) {
axios.patch(
`${process.env.REACT_APP_BE_URL}/mentorinfoupdate/`,
{
skills: skills,
about_me: aboutMe,
},
{
headers: {
Authorization: `Token ${token}`,
"Content-Type": "application/json",
},
}
);
} else if (teamNumber) {
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 Expand Up @@ -275,19 +299,18 @@ export default function EditProfile({ token, pk, setAuth }) {
</Stack>
)}

{isMentee && (
<Stack item="true" className="field">
<TextField
label="Team Number"
placeholder={teamNumber !== "" ? teamNumber : "Team Number"}
InputLabelProps={{
shrink: true,
}}
value={teamNumber}
onChange={(e) => setTeamNumber(e.target.value)}
></TextField>
</Stack>
)}
<Stack item="true" className="field">
<TextField
label="Team Number"
placeholder={teamNumber !== "" ? teamNumber : "Team Number"}
InputLabelProps={{
shrink: true,
}}
value={teamNumber}
onChange={(e) => setTeamNumber(e.target.value)}
></TextField>
</Stack>


<Stack item="true" className="button--edit-profile">
{loading ? (
Expand Down
3 changes: 3 additions & 0 deletions src/pages/Profile/ProfileMentor.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export default function ProfileMentor({ token, pk, setAuth }) {
const [profilePhoto, setProfilePhoto] = useState("");
const [skills, setSkills] = useState([]);
const [about_me, setAboutMe] = useState("");
const [teamNumber, setTeamNumber] = useState("");
//Update the availabilities when the user adds a new availability
const [refreshAvailabilities, setRefreshAvailabilities] = useState(false);

Expand Down Expand Up @@ -67,6 +68,7 @@ export default function ProfileMentor({ token, pk, setAuth }) {
.then((res) => {
setSkills(res.data[0].skills);
setAboutMe(res.data[0].about_me);
setTeamNumber(res.data[0].team_number);
});
axios
.get(`${process.env.REACT_APP_BE_URL}/notificationsettings/${pk}/`, {
Expand Down Expand Up @@ -228,6 +230,7 @@ export default function ProfileMentor({ token, pk, setAuth }) {
<Stack spacing={1} direction="row">
<Avatar sx={{ width: 200, height: 210 }} src={profilePhoto} />
<Box sx={{ fontSize: "20px" }} textAlign={"Center"}>
<Typography variant="h5">{ `Team Number: ${ teamNumber }`}</Typography>
<ProfileBasicInfo
firstName={`First Name: ${firstName}`}
lastName={`Last Name: ${lastName}`}
Expand Down

0 comments on commit a7a5e83

Please sign in to comment.