From b8e27250426568bd2c41d8dd6251bc93e6732e82 Mon Sep 17 00:00:00 2001 From: Elmar Kresse Date: Tue, 10 Sep 2024 14:57:26 +0200 Subject: [PATCH] feat:#22 added static switch between semesters --- frontend/src/api/fetchRoomOccupancy.ts | 26 +++++++++++---- .../src/components/RoomOccupationOffline.vue | 32 +++++++++++++++++-- 2 files changed, 49 insertions(+), 9 deletions(-) diff --git a/frontend/src/api/fetchRoomOccupancy.ts b/frontend/src/api/fetchRoomOccupancy.ts index f067e38..7c4f4db 100644 --- a/frontend/src/api/fetchRoomOccupancy.ts +++ b/frontend/src/api/fetchRoomOccupancy.ts @@ -22,6 +22,11 @@ import { formatYearMonthDay } from "@/helpers/dates"; const END_OF_SUMMER_SEMESTER = "0930"; const END_OF_WINTER_SEMESTER = "0331"; +export function isSummerSemester(date: Date): boolean { + const formattedDate = formatYearMonthDay(date).slice(4); + return formattedDate > END_OF_WINTER_SEMESTER && formattedDate <= END_OF_SUMMER_SEMESTER; +} + /** * check if date is in winter semester before summer semester * @param date - The date to check @@ -68,15 +73,24 @@ export async function fetchRoomOccupancy( from_date?: string, to_date?: string, ): Promise { + let new_from_date: Date | string + let new_to_date = to_date + + console.debug("from_date: " + from_date); + if (from_date == undefined) { - const new_from_date = getSemesterStart(new Date()); - from_date = new_from_date.toISOString(); + new_from_date = getSemesterStart(new Date()); + new_from_date = new_from_date.toISOString(); + } else { + new_from_date = getSemesterStart(new Date(from_date)).toISOString(); + new_to_date = getSemesterStart(addMonths(new Date(from_date), 6)).toISOString(); } - if (to_date == undefined) { - const new_to_date = getSemesterStart(addMonths(new Date(), 6)); - to_date = new_to_date.toISOString(); + + if (to_date == undefined && new_to_date == undefined) { + new_to_date = getSemesterStart(addMonths(new Date(), 6)).toISOString(); } + let roomOccupancyList: RoomOccupancyList = new RoomOccupancyList( new Date(), 0, @@ -84,7 +98,7 @@ export async function fetchRoomOccupancy( [], ); - await fetch("/api/schedule/rooms?from=" + from_date + "&to=" + to_date) + await fetch("/api/schedule/rooms?from=" + new_from_date + "&to=" + new_to_date) .then((response) => { return response.arrayBuffer(); }) diff --git a/frontend/src/components/RoomOccupationOffline.vue b/frontend/src/components/RoomOccupationOffline.vue index b436e62..261651c 100644 --- a/frontend/src/components/RoomOccupationOffline.vue +++ b/frontend/src/components/RoomOccupationOffline.vue @@ -28,7 +28,7 @@ import allLocales from "@fullcalendar/core/locales-all"; import router from "@/router"; import { formatYearMonthDay } from "@/helpers/dates"; import { useQuery } from "@tanstack/vue-query"; -import { fetchRoomOccupancy } from "@/api/fetchRoomOccupancy"; +import { fetchRoomOccupancy, isSummerSemester } from "@/api/fetchRoomOccupancy"; import { isValid } from "date-fns"; import { RoomOccupancyList } from "@/model/roomOccupancyList"; @@ -89,9 +89,9 @@ function transformData(data: RoomOccupancyList) { })); } -const { data: occupancy } = useQuery({ +const { data: occupancy, refetch } = useQuery({ queryKey: ["roomOccupancy"], //, selectedRoom, currentDateFrom, currentDateTo], - queryFn: () => fetchRoomOccupancy(), + queryFn: () => fetchRoomOccupancy(date.value.toISOString()), staleTime: 12 * 3600000, // 12 hours }); @@ -102,6 +102,32 @@ const occupations = computed(() => { watch(occupations, () => fullCalendar.value?.getApi().refetchEvents()); + +const fetchedSemesterSummer: Ref = ref(isSummerSemester(date.value)); +// refetch occupancy data when date changes the month from march to april or september to october +watch(currentDateTo, () => { + //if preselected date is not in range from april to september, update occupancy data + + console.debug("currentDateTo", currentDateTo.value, isSummerSemester(new Date(currentDateTo.value))); + console.debug("currentDateFrom", currentDateFrom.value, isSummerSemester(new Date(currentDateFrom.value))); + console.debug("fetchedSemesterSummer", fetchedSemesterSummer.value); + + if (isSummerSemester(new Date(currentDateTo.value)) != fetchedSemesterSummer.value) { + fetchedSemesterSummer.value = !fetchedSemesterSummer.value; + date.value = new Date(currentDateTo.value); + refetch(); + return; + } + + if (isSummerSemester(new Date(currentDateFrom.value)) != fetchedSemesterSummer.value) { + fetchedSemesterSummer.value = !fetchedSemesterSummer.value; + date.value = new Date(currentDateFrom.value); + refetch(); + return; + } + +}); + const fullCalendar = ref>(); const calendarOptions: ComputedRef = computed(() => ({