Skip to content

Commit

Permalink
feat: using only course units that have an eligible exchange active p…
Browse files Browse the repository at this point in the history
…eriod
  • Loading branch information
tomaspalma committed Oct 19, 2024
1 parent 49a468f commit d20dea1
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const ChooseIncludedCourseUnits = ({
if (!checked) {
setSelectedCourseUnits([]);
} else {
setSelectedCourseUnits(Array.from(enrolledCourseUnits));
setSelectedCourseUnits(enrolledCourseUnits);
}
}}
checked={selectedCourseUnits.length === enrolledCourseUnits.length}
Expand All @@ -34,7 +34,7 @@ export const ChooseIncludedCourseUnits = ({
Selecionar todas as disciplinas
</label>
</div>
{Array.from(enrolledCourseUnits).map((courseInfo: CourseInfo) => (
{enrolledCourseUnits?.map((courseInfo: CourseInfo) => (
<IncludeCourseUnitCard
key={"include-course-unit-" + courseInfo.id}
courseInfo={courseInfo}
Expand Down
2 changes: 1 addition & 1 deletion src/components/exchange/requests/issue/CreateRequest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const CreateRequest = ({
const { exchangeSchedule } = useContext(ScheduleContext);
const [selectedCourseUnits, setSelectedCourseUnits] = useState<CourseInfo[]>([]);
const [selectingCourseUnits, setSelectingCourseUnits] = useState<boolean>(false);
const enrolledCourseUnits = useStudentCourseUnits(exchangeSchedule);
const { enrolledCourseUnits } = useStudentCourseUnits();

return <div className="flex flex-col">
<div className="flex flex-col gap-y-4 max-h-screen overflow-y-auto">
Expand Down
5 changes: 2 additions & 3 deletions src/components/exchange/requests/view/ViewRequestsFilters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,15 @@ export const ViewRequestsFilters = ({
filterCourseUnitsHook,
classesFilterHook
}: Props) => {
const { exchangeSchedule, loadingSchedule } = useContext(ScheduleContext);
const enrolledCourseUnits = useStudentCourseUnits(exchangeSchedule);
const { enrolledCourseUnits, exchangeSchedule, loadingSchedule } = useContext(ScheduleContext);

return <div className="flex flex-row justify-between w-full">
{/* Course unit filters */}
{loadingSchedule ? <ViewRequestsFiltersSkeletons />
: <div className="flex flex-row gap-2 w-full flex-wrap">
<div className="w-full flex flex-row flex-wrap gap-x-3 gap-y-5 items-center">
{
Array.from(enrolledCourseUnits).map((courseUnit: CourseInfo) => (
enrolledCourseUnits?.map((courseUnit: CourseInfo) => (
<ViewRequestBadgeFilter
courseUnit={courseUnit}
filterCourseUnitsHook={filterCourseUnitsHook}
Expand Down
28 changes: 28 additions & 0 deletions src/hooks/useEligibleExchange.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { useMemo } from "react";
import api from "../api/backend";
import useSWR from "swr";

export default () => {
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,
};
};

54 changes: 44 additions & 10 deletions src/hooks/useStudentCourseUnits.tsx
Original file line number Diff line number Diff line change
@@ -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<ClassDescriptor>): Array<CourseInfo> => {
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<number>();
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<ClassDescriptor>): Array<CourseInfo> => {
// if (!schedule) return [];
//
// const duplicates = new Set<number>();
// 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));
// };

48 changes: 32 additions & 16 deletions src/pages/Exchange.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<article className="flex flex-col mx-auto w-full gap-4">
<h1 className="text-center text-3xl font-bold">
Trocas de Turmas
</h1>
{children}
</article>
);
}

const ExchangePage = () => {
const [loads, setLoads] = useState<number>(-1);
const { schedule, loading: loadingSchedule } = useSchedule();
const [originalExchangeSchedule, setOriginalExchangeSchedule] = useState<Array<ClassDescriptor>>([]);
const [exchangeSchedule, setExchangeSchedule] = useState<Array<ClassDescriptor>>([]);
const { signedIn, isLoading } = useSession();
const enrolledCourseUnits = useStudentCourseUnits(schedule);
const { signedIn, isLoading, user } = useSession();
const { enrolledCourseUnits } = useStudentCourseUnits();

useEffect(() => {
setLoads(prev => prev + 1);
Expand All @@ -32,28 +44,32 @@ const ExchangePage = () => {
return <>
{!isLoading && signedIn ?
<ScheduleContext.Provider value={{ originalExchangeSchedule, exchangeSchedule, loadingSchedule, setExchangeSchedule, enrolledCourseUnits }}>
<div className="grid w-cfull grid-cols-12 gap-x-4 gap-y-4 px-4 py-4">
{/* Schedule Preview */}
<div className="lg:min-h-adjusted order-1 col-span-12 min-h-min rounded bg-lightest px-3 py-3 dark:bg-dark lg:col-span-9 2xl:px-5 2xl:py-5">
<div className="h-full w-full">
<ExchangeSchedule />
</div>
</div>
{
user.eligible_exchange ?
<div className="grid w-cfull grid-cols-12 gap-x-4 gap-y-4 px-4 py-4">
{/* Schedule Preview */}
<div className="lg:min-h-adjusted order-1 col-span-12 min-h-min rounded bg-lightest px-3 py-3 dark:bg-dark lg:col-span-9 2xl:px-5 2xl:py-5">
<div className="h-full w-full">
<ExchangeSchedule />
</div>
</div>

<ExchangeSidebar />
</div>
<ExchangeSidebar />
</div>
: <ExchangeGuard>
<p className="text-center">Não tens nenhuma inscrição numa disciplina com um período de trocas ativo.</p>
</ExchangeGuard>
}
</ScheduleContext.Provider>
: <article className="flex flex-col mx-auto w-full gap-4">
<h1 className="text-center text-3xl font-bold">
Trocas de Turmas
</h1>
: <ExchangeGuard>
<p className="text-center">
Tens de iniciar sessão para acederes a esta funcionalidade.
</p>
<div className="justify-center mx-auto">
<LoginButton expanded={true} />
</div>
</article>}

</ExchangeGuard>}
</>
}

Expand Down

0 comments on commit d20dea1

Please sign in to comment.