change occupation to useQuery

This commit is contained in:
Niclas Jost
2024-03-29 12:19:57 +01:00
parent 0ce0144ed9
commit fd0c98689d

View File

@ -3,13 +3,16 @@ import FullCalendar from "@fullcalendar/vue3";
import dayGridPlugin from "@fullcalendar/daygrid"; import dayGridPlugin from "@fullcalendar/daygrid";
import interactionPlugin from "@fullcalendar/interaction"; import interactionPlugin from "@fullcalendar/interaction";
import timeGridPlugin from "@fullcalendar/timegrid"; import timeGridPlugin from "@fullcalendar/timegrid";
import { computed, ComputedRef, inject, ref, Ref, watch } from "vue"; import { computed, ComputedRef, inject, ref, Ref } from "vue";
import { CalendarOptions, DatesSetArg, EventInput } from "@fullcalendar/core"; import { CalendarOptions, DatesSetArg, EventInput } from "@fullcalendar/core";
import { fetchEventsByRoomAndDuration } from "../api/fetchRoom.ts"; import { fetchEventsByRoomAndDuration } from "../api/fetchRoom.ts";
import { useI18n } from "vue-i18n"; import { useI18n } from "vue-i18n";
import allLocales from "@fullcalendar/core/locales-all"; import allLocales from "@fullcalendar/core/locales-all";
import router from "@/router"; import router from "@/router";
import { formatYearMonthDay } from "@/helpers/dates"; import { formatYearMonthDay } from "@/helpers/dates";
import { useQuery } from "@tanstack/vue-query";
import { watch } from "vue";
const { t } = useI18n({ useScope: "global" }); const { t } = useI18n({ useScope: "global" });
const props = defineProps({ const props = defineProps({
@ -19,13 +22,6 @@ const props = defineProps({
}, },
}); });
type CalenderEvent = {
id: number;
start: string;
end: string;
showFree: boolean;
};
const date: Ref<Date> = ref(new Date()); const date: Ref<Date> = ref(new Date());
// Set the selected date from the URL // Set the selected date from the URL
@ -48,44 +44,33 @@ setDateFromQuery();
const currentDateFrom: Ref<string> = ref(""); const currentDateFrom: Ref<string> = ref("");
const currentDateTo: Ref<string> = ref(""); const currentDateTo: Ref<string> = ref("");
const occupations: Ref<CalenderEvent[]> = ref([]);
const mobilePage = inject("mobilePage") as Ref<boolean>; const mobilePage = inject("mobilePage") as Ref<boolean>;
const selectedRoom = computed(() => props.room); const selectedRoom = computed(() => props.room);
watch(selectedRoom, () => { const { data: occupations } = useQuery({
getOccupation(); queryKey: ["roomOccupation", selectedRoom, currentDateFrom, currentDateTo],
}); queryFn: () =>
const fullCalendar = ref<InstanceType<typeof FullCalendar>>();
async function getOccupation() {
if (selectedRoom.value === "") {
return;
}
fetchEventsByRoomAndDuration( fetchEventsByRoomAndDuration(
selectedRoom.value, selectedRoom.value,
currentDateFrom.value, currentDateFrom.value,
currentDateTo.value, currentDateTo.value,
) ),
.then((events) => { select: (data) =>
occupations.value = events?.map((event, index) => { data.map((event, index) => ({
return {
id: index, id: index,
start: event.start.replace(/\s\+\d{4}\s\w+$/, "").replace(" ", "T"), start: event.start.replace(/\s\+\d{4}\s\w+$/, "").replace(" ", "T"),
end: event.end.replace(/\s\+\d{4}\s\w+$/, "").replace(" ", "T"), end: event.end.replace(/\s\+\d{4}\s\w+$/, "").replace(" ", "T"),
showFree: event.free, showFree: event.free,
}; })),
enabled: () => selectedRoom.value !== "" && currentDateFrom.value !== "",
staleTime: 5000000, // 500 seconds
}); });
})
.finally(() => { watch(occupations, () => fullCalendar.value?.getApi().refetchEvents());
fullCalendar.value?.getApi().refetchEvents();
}) const fullCalendar = ref<InstanceType<typeof FullCalendar>>();
.catch((error) => {
console.error(error);
});
}
const calendarOptions: ComputedRef<CalendarOptions> = computed(() => ({ const calendarOptions: ComputedRef<CalendarOptions> = computed(() => ({
locales: allLocales, locales: allLocales,
@ -157,12 +142,12 @@ const calendarOptions: ComputedRef<CalendarOptions> = computed(() => ({
date: formatYearMonthDay(endDate), date: formatYearMonthDay(endDate),
}, },
}); });
getOccupation();
}, },
events: function ( events: function (
_info: unknown, _info: unknown,
successCallback: (events: EventInput[]) => void, successCallback: (events: EventInput[]) => void,
) { ) {
if (!occupations.value) return;
successCallback( successCallback(
occupations.value.map((event) => { occupations.value.map((event) => {
return { return {