From e9103bc956868c096fd1d3d46fbdd723227a978d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonardo=20Mur=C3=A7a?= Date: Wed, 3 Aug 2022 16:03:45 -0300 Subject: [PATCH] Add classroom announcements fetch --- src/context/user.js | 7 ++++ src/screens/Classroom/index.js | 19 +++++++++- src/services/mocks.js | 68 +++++++++++++++++++++++++++++++++- src/services/user-service.js | 26 ++++++++++++- 4 files changed, 116 insertions(+), 4 deletions(-) diff --git a/src/context/user.js b/src/context/user.js index e6249a2..67fc789 100644 --- a/src/context/user.js +++ b/src/context/user.js @@ -3,6 +3,7 @@ import { useLocation } from 'react-router-dom'; import { useAuthState } from './auth'; import { getAssignments, + getClassroomAnnouncementsById, getClassroomById, getClassrooms, getFaq, @@ -31,6 +32,9 @@ function UserProvider(props) { const fetchFAQ = () => getFaq(); + const fetchClassroomAnnouncements = classId => + getClassroomAnnouncementsById(classId); + return ( @@ -52,6 +57,7 @@ function useUser() { fetchAssignments, fetchClassroomById, fetchFAQ, + fetchClassroomAnnouncements, } = useContext(UserContext); return { @@ -60,6 +66,7 @@ function useUser() { fetchAssignments, fetchClassroomById, fetchFAQ, + fetchClassroomAnnouncements, }; } diff --git a/src/screens/Classroom/index.js b/src/screens/Classroom/index.js index 45d25c8..cd27864 100644 --- a/src/screens/Classroom/index.js +++ b/src/screens/Classroom/index.js @@ -8,12 +8,14 @@ import View from './View'; function Classroom() { const params = useParams(); const layoutType = useLayoutType(); - const { fetchClassroomById } = useUser(); + const { fetchClassroomById, fetchClassroomAnnouncements } = useUser(); const [classroom, setClassroom] = useState(null); const [selectedTabOption, setSelectedTabOption] = useState( TAB_OPTIONS.map(o => o.value).indexOf('announcements') ); + const [announcements, setAnnouncements] = useState(null); + const onSelectTabOption = (_, value) => { setSelectedTabOption(value); }; @@ -34,6 +36,21 @@ function Classroom() { } }, [classroom]); + useEffect(() => { + async function getSelectedTabData() { + if (selectedTabOption === 0) { + console.log('Fetch announcements'); + const result = await fetchClassroomAnnouncements(params.id); + setAnnouncements(result.data); + } else if (selectedTabOption === 1) { + console.log('Fetch assignments'); + } else if (selectedTabOption === 2) { + console.log('Fetch people'); + } + } + getSelectedTabData(); + }, [fetchClassroomAnnouncements, selectedTabOption, params]); + return ( sleep(3000).then(() => { @@ -17,6 +24,14 @@ const getClassroomById = classId => }; }); +const getClassroomAnnouncementsById = classId => + sleep(3000).then(() => { + console.log('classId ' + classId); + return { + data: allClassroomAnnouncements.filter(c => c.classId === classId), + }; + }); + const getAssignments = userId => sleep(4000).then(() => { console.log('userId: ' + userId); @@ -43,4 +58,11 @@ const getUser = shouldFail => } }); -export { getClassrooms, getClassroomById, getAssignments, getFaq, getUser }; +export { + getClassrooms, + getClassroomById, + getAssignments, + getClassroomAnnouncementsById, + getFaq, + getUser, +};