From bed5c3d1ff246eb84f2486463e71ef707a9e8669 Mon Sep 17 00:00:00 2001 From: James Andrews Date: Thu, 19 Oct 2023 10:46:42 -0400 Subject: [PATCH 1/8] converted callbacks to async await --- src/pages/Profile/EditProfile.js | 88 +++++++++++++++++--------------- 1 file changed, 48 insertions(+), 40 deletions(-) diff --git a/src/pages/Profile/EditProfile.js b/src/pages/Profile/EditProfile.js index 9fa9f32..db045db 100644 --- a/src/pages/Profile/EditProfile.js +++ b/src/pages/Profile/EditProfile.js @@ -52,51 +52,59 @@ export default function EditProfile({ token, pk, setAuth }) { "Vue.js", ]; + const client = axios.create({ + baseURL: `${ process.env.REACT_APP_BE_URL }`, + headers: { Authorization: `Token ${ token }` }, + }); + 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 client.get('/mentorinfo/'); + + setSkills(mentorInfo.data[0].skills); + setAboutMe(mentorInfo.data[0].about_me); + } catch (err) { + console.error(err); + } + } + + const getMenteeInfo = async () => { + try { + const menteeInfo = await client.get('/menteeInfo/'); + + setTeamNumber(menteeInfo.data[0].team_number); + } catch (err) { + console.error(err); + } + } + + const getProfileData = async () => { + try { + const profileData = await client.get('/myprofile/'); + 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); - }); - }, [token]); + setError(err); + } + } + + setLoading(true); + getProfileData(); + }, [client]); const handleChange = (event) => { const { value: selectedValue } = event.target; From bd60c2457fe9699ea1522394ec2200ebcc079d3a Mon Sep 17 00:00:00 2001 From: James Andrews Date: Thu, 19 Oct 2023 15:49:39 -0400 Subject: [PATCH 2/8] grouped useEffect code into functions; converted then-catch to async-await --- src/pages/Profile/EditProfile.js | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/pages/Profile/EditProfile.js b/src/pages/Profile/EditProfile.js index db045db..30b40fe 100644 --- a/src/pages/Profile/EditProfile.js +++ b/src/pages/Profile/EditProfile.js @@ -52,18 +52,16 @@ export default function EditProfile({ token, pk, setAuth }) { "Vue.js", ]; - const client = axios.create({ - baseURL: `${ process.env.REACT_APP_BE_URL }`, - headers: { Authorization: `Token ${ token }` }, - }); - useEffect(() => { const getMentorInfo = async () => { try { - const mentorInfo = await client.get('/mentorinfo/'); - + const mentorInfo = await axios.get( + `${process.env.REACT_APP_BE_URL}/myprofile/`, + { headers: { Authorization: `Token ${ token }` } }, + ); + setSkills(mentorInfo.data[0].skills); - setAboutMe(mentorInfo.data[0].about_me); + setAboutMe(mentorInfo.data[0].skills); } catch (err) { console.error(err); } @@ -71,7 +69,10 @@ export default function EditProfile({ token, pk, setAuth }) { const getMenteeInfo = async () => { try { - const menteeInfo = await client.get('/menteeInfo/'); + const menteeInfo = await axios.get( + `${process.env.REACT_APP_BE_URL}/myprofile/`, + { headers: { Authorization: `Token ${ token }` } }, + ); setTeamNumber(menteeInfo.data[0].team_number); } catch (err) { @@ -81,8 +82,11 @@ export default function EditProfile({ token, pk, setAuth }) { const getProfileData = async () => { try { - const profileData = await client.get('/myprofile/'); - + const profileData = await axios.get( + `${process.env.REACT_APP_BE_URL}/myprofile/`, + { headers: { Authorization: `Token ${ token }` } }, + ); + setLoading(false); setOriginalProfile(profileData.data); setFirstName(profileData.data.first_name); @@ -98,13 +102,13 @@ export default function EditProfile({ token, pk, setAuth }) { } } catch (err) { setLoading(false); - setError(err); + setError(err.message); } } setLoading(true); getProfileData(); - }, [client]); + }, [token]); const handleChange = (event) => { const { value: selectedValue } = event.target; From 9300041f43113665af581782edadc79a58bee706 Mon Sep 17 00:00:00 2001 From: James Andrews Date: Thu, 19 Oct 2023 15:55:00 -0400 Subject: [PATCH 3/8] added Team Number field to Edit Profile page --- src/pages/Profile/EditProfile.js | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/pages/Profile/EditProfile.js b/src/pages/Profile/EditProfile.js index 30b40fe..6e3d084 100644 --- a/src/pages/Profile/EditProfile.js +++ b/src/pages/Profile/EditProfile.js @@ -287,19 +287,18 @@ export default function EditProfile({ token, pk, setAuth }) { )} - {isMentee && ( - - setTeamNumber(e.target.value)} - > - - )} + + setTeamNumber(e.target.value)} + > + + {loading ? ( From c53e2b88283af9d001274c6e9894777e49f7366b Mon Sep 17 00:00:00 2001 From: James Andrews Date: Fri, 20 Oct 2023 09:13:36 -0400 Subject: [PATCH 4/8] Team Number field is filled when edit page loads --- src/pages/Profile/EditProfile.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/pages/Profile/EditProfile.js b/src/pages/Profile/EditProfile.js index 6e3d084..68737df 100644 --- a/src/pages/Profile/EditProfile.js +++ b/src/pages/Profile/EditProfile.js @@ -56,12 +56,13 @@ export default function EditProfile({ token, pk, setAuth }) { const getMentorInfo = async () => { try { const mentorInfo = await axios.get( - `${process.env.REACT_APP_BE_URL}/myprofile/`, + `${process.env.REACT_APP_BE_URL}/mentorinfo/`, { headers: { Authorization: `Token ${ token }` } }, ); - + setSkills(mentorInfo.data[0].skills); setAboutMe(mentorInfo.data[0].skills); + setTeamNumber(mentorInfo.data[0].team_number); } catch (err) { console.error(err); } @@ -70,7 +71,7 @@ export default function EditProfile({ token, pk, setAuth }) { const getMenteeInfo = async () => { try { const menteeInfo = await axios.get( - `${process.env.REACT_APP_BE_URL}/myprofile/`, + `${process.env.REACT_APP_BE_URL}/menteeinfo/`, { headers: { Authorization: `Token ${ token }` } }, ); From 978d566ee794a0138ca6162a30ea1c9f85672360 Mon Sep 17 00:00:00 2001 From: James Andrews Date: Fri, 20 Oct 2023 13:56:34 -0400 Subject: [PATCH 5/8] fixed about me typo --- src/pages/Profile/EditProfile.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/Profile/EditProfile.js b/src/pages/Profile/EditProfile.js index 68737df..4dc3141 100644 --- a/src/pages/Profile/EditProfile.js +++ b/src/pages/Profile/EditProfile.js @@ -61,7 +61,7 @@ export default function EditProfile({ token, pk, setAuth }) { ); setSkills(mentorInfo.data[0].skills); - setAboutMe(mentorInfo.data[0].skills); + setAboutMe(mentorInfo.data[0].about_me); setTeamNumber(mentorInfo.data[0].team_number); } catch (err) { console.error(err); From a4a8e3a89c9f9fffcd23aabad6fa742d799a9a82 Mon Sep 17 00:00:00 2001 From: James Andrews Date: Fri, 20 Oct 2023 14:02:39 -0400 Subject: [PATCH 6/8] added Team Number to Profile page --- src/pages/Profile/ProfileMentor.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/pages/Profile/ProfileMentor.js b/src/pages/Profile/ProfileMentor.js index de86787..a1febb4 100644 --- a/src/pages/Profile/ProfileMentor.js +++ b/src/pages/Profile/ProfileMentor.js @@ -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); @@ -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}/`, { @@ -228,6 +230,7 @@ export default function ProfileMentor({ token, pk, setAuth }) { + { `Team Number: ${ teamNumber }`} Date: Fri, 20 Oct 2023 15:00:11 -0400 Subject: [PATCH 7/8] team number is edited --- src/pages/Profile/EditProfile.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/pages/Profile/EditProfile.js b/src/pages/Profile/EditProfile.js index 4dc3141..c39239e 100644 --- a/src/pages/Profile/EditProfile.js +++ b/src/pages/Profile/EditProfile.js @@ -152,12 +152,13 @@ export default function EditProfile({ token, pk, setAuth }) { } ) .then((res) => { - if (skills && aboutMe) { + if (isMentor) { axios.patch( `${process.env.REACT_APP_BE_URL}/mentorinfoupdate/`, { skills: skills, about_me: aboutMe, + team_number: teamNumber, }, { headers: { @@ -166,7 +167,7 @@ export default function EditProfile({ token, pk, setAuth }) { }, } ); - } else if (teamNumber) { + } else if (isMentee) { axios.patch( `${process.env.REACT_APP_BE_URL}/menteeinfoupdate/`, { From 4ef496c08ca17785aa5cb51a4a2ffe5ce8622b90 Mon Sep 17 00:00:00 2001 From: James Andrews Date: Mon, 23 Oct 2023 09:36:22 -0400 Subject: [PATCH 8/8] editProfile function now uses async await instead of regular Promises --- src/pages/Profile/EditProfile.js | 116 +++++++++++++++++-------------- 1 file changed, 63 insertions(+), 53 deletions(-) diff --git a/src/pages/Profile/EditProfile.js b/src/pages/Profile/EditProfile.js index c39239e..37b860c 100644 --- a/src/pages/Profile/EditProfile.js +++ b/src/pages/Profile/EditProfile.js @@ -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); @@ -140,8 +155,8 @@ 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 { @@ -149,59 +164,54 @@ export default function EditProfile({ token, pk, setAuth }) { 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 (