diff --git a/src/components/exchange/requests/issue/ChooseIncludedCourseUnits.tsx b/src/components/exchange/requests/issue/ChooseIncludedCourseUnits.tsx index 3039648f..7e904663 100644 --- a/src/components/exchange/requests/issue/ChooseIncludedCourseUnits.tsx +++ b/src/components/exchange/requests/issue/ChooseIncludedCourseUnits.tsx @@ -25,7 +25,7 @@ export const ChooseIncludedCourseUnits = ({ if (!checked) { setSelectedCourseUnits([]); } else { - setSelectedCourseUnits(Array.from(enrolledCourseUnits)); + setSelectedCourseUnits(enrolledCourseUnits); } }} checked={selectedCourseUnits.length === enrolledCourseUnits.length} @@ -34,7 +34,7 @@ export const ChooseIncludedCourseUnits = ({ Selecionar todas as disciplinas - {Array.from(enrolledCourseUnits).map((courseInfo: CourseInfo) => ( + {enrolledCourseUnits?.map((courseInfo: CourseInfo) => ( ([]); const [selectingCourseUnits, setSelectingCourseUnits] = useState(false); - const enrolledCourseUnits = useStudentCourseUnits(exchangeSchedule); + const { enrolledCourseUnits } = useStudentCourseUnits(); return
diff --git a/src/components/exchange/requests/view/ViewRequestsFilters.tsx b/src/components/exchange/requests/view/ViewRequestsFilters.tsx index 6d03887c..17ca4298 100644 --- a/src/components/exchange/requests/view/ViewRequestsFilters.tsx +++ b/src/components/exchange/requests/view/ViewRequestsFilters.tsx @@ -24,8 +24,7 @@ export const ViewRequestsFilters = ({ filterCourseUnitsHook, classesFilterHook }: Props) => { - const { exchangeSchedule, loadingSchedule } = useContext(ScheduleContext); - const enrolledCourseUnits = useStudentCourseUnits(exchangeSchedule); + const { enrolledCourseUnits, exchangeSchedule, loadingSchedule } = useContext(ScheduleContext); return
{/* Course unit filters */} @@ -33,7 +32,7 @@ export const ViewRequestsFilters = ({ :
{ - Array.from(enrolledCourseUnits).map((courseUnit: CourseInfo) => ( + enrolledCourseUnits?.map((courseUnit: CourseInfo) => ( { + const isEligible = async () => { + try { + const res = await fetch(`${api.BACKEND_URL}/student/exchange/eligible`, { + credentials: "include", + }); + + const json = await res.json(); + } catch (error) { + console.error(error); + } + }; + + const { data, error, mutate, isLoading } = useSWR("", isEligible); + const courseUnits = useMemo(() => data ? data : null, [data]); + + return { + courseUnits, + error, + loading: isLoading, + mutate, + }; +}; + diff --git a/src/hooks/useStudentCourseUnits.tsx b/src/hooks/useStudentCourseUnits.tsx index 6c036483..bd547e22 100644 --- a/src/hooks/useStudentCourseUnits.tsx +++ b/src/hooks/useStudentCourseUnits.tsx @@ -1,18 +1,52 @@ import { ClassDescriptor, CourseInfo } from "../@types"; +import { useMemo } from "react"; +import api from "../api/backend"; +import useSWR from "swr"; -export default (schedule: Array): Array => { - if (!schedule) return []; +export default () => { + const getEligibileCourseUnits = async () => { + try { + const res = await fetch(`${api.BACKEND_URL}/student/course_units/eligible`, { + credentials: "include" + }); - const duplicates = new Set(); - const result = []; + if (res.ok) { + return await res.json(); + } - schedule?.forEach((scheduleItem: ClassDescriptor) => { - if (!duplicates.has(scheduleItem.courseInfo.id)) { - result.push(scheduleItem.courseInfo); - duplicates.add(scheduleItem.courseInfo.id); + return []; + } catch (e) { + console.error(e); + return []; } - }); - return result.sort((a: CourseInfo, b: CourseInfo) => Number(a.acronym < b.acronym)); + } + + const { data, error, mutate, isValidating } = useSWR("eligibleCourseUnits", getEligibileCourseUnits, {}); + const enrolledCourseUnits = useMemo(() => data ? data : null, [data]); + + return { + enrolledCourseUnits, + error, + loading: !data, + isValidating, + mutate, + }; }; + +// export default (schedule: Array): Array => { +// if (!schedule) return []; +// +// const duplicates = new Set(); +// const result = []; +// +// schedule?.forEach((scheduleItem: ClassDescriptor) => { +// if (!duplicates.has(scheduleItem.courseInfo.id)) { +// result.push(scheduleItem.courseInfo); +// duplicates.add(scheduleItem.courseInfo.id); +// } +// }); +// return result.sort((a: CourseInfo, b: CourseInfo) => Number(a.acronym < b.acronym)); +// }; + diff --git a/src/pages/Exchange.tsx b/src/pages/Exchange.tsx index f64e8f79..9953d02d 100644 --- a/src/pages/Exchange.tsx +++ b/src/pages/Exchange.tsx @@ -6,16 +6,28 @@ import ExchangeSchedule from "../components/exchange/schedule/ExchangeSchedule"; import { Schedule } from "../components/planner"; import ScheduleContext from "../contexts/ScheduleContext"; import { useSession } from "../hooks"; +import useEligibleExchange from "../hooks/useEligibleExchange"; import useSchedule from "../hooks/useSchedule"; import useStudentCourseUnits from "../hooks/useStudentCourseUnits"; +const ExchangeGuard = ({ children }) => { + return ( +
+

+ Trocas de Turmas +

+ {children} +
+ ); +} + const ExchangePage = () => { const [loads, setLoads] = useState(-1); const { schedule, loading: loadingSchedule } = useSchedule(); const [originalExchangeSchedule, setOriginalExchangeSchedule] = useState>([]); const [exchangeSchedule, setExchangeSchedule] = useState>([]); - const { signedIn, isLoading } = useSession(); - const enrolledCourseUnits = useStudentCourseUnits(schedule); + const { signedIn, isLoading, user } = useSession(); + const { enrolledCourseUnits } = useStudentCourseUnits(); useEffect(() => { setLoads(prev => prev + 1); @@ -32,28 +44,32 @@ const ExchangePage = () => { return <> {!isLoading && signedIn ? -
- {/* Schedule Preview */} -
-
- -
-
+ { + user.eligible_exchange ? +
+ {/* Schedule Preview */} +
+
+ +
+
- -
+ +
+ : +

Não tens nenhuma inscrição numa disciplina com um período de trocas ativo.

+
+ }
- :
-

- Trocas de Turmas -

+ :

Tens de iniciar sessão para acederes a esta funcionalidade.

-
} + + } }