feat:#5 added first offline calendar functions

This commit is contained in:
Elmar Kresse
2024-08-13 19:16:07 +02:00
parent b00528dfc1
commit b848d26704
9 changed files with 134 additions and 53 deletions

View File

@ -15,7 +15,8 @@
//along with this program. If not, see <https://www.gnu.org/licenses/>.
import { Module } from "../model/module";
import { Calendar } from "../model/calendar";
import tokenStore from "@/store/tokenStore.ts";
import { Calendar } from "@/model/calendar.ts";
export async function getCalender(token: string): Promise<Module[]> {
const request = new Request("/api/collections/feeds/records/" + token, {
@ -24,9 +25,10 @@ export async function getCalender(token: string): Promise<Module[]> {
return await fetch(request).then((response) => {
if (response.ok) {
return response
.json()
.then((calendarResponse: Calendar) => calendarResponse.modules);
return response.json().then((calendarResponse: Calendar) => {
tokenStore().feed = calendarResponse;
return calendarResponse.modules;
});
} else {
return [];
}

View File

@ -95,8 +95,13 @@
"error": "Error",
"successDetail": "calendar successfully deleted",
"errorDetail": "calendar could not be deleted",
"successDetailLoad": "calendar successfully loaded"
}
"successDetailLoad": "calendar successfully loaded",
"info": "info"
},
"calendarContainsNoModules": "calendar contains no modules",
"noCalendarInOfflineMode": "calendar isn't available in offline mode",
"calendarLoadedFromOfflineMode": "calendar loaded from offline cache",
"calendarLoadedFromServer": "calendar loaded from web"
},
"additionalModules": {
"subTitle": "select additional modules that are not listed in the regular semester for your course",

View File

@ -218,7 +218,8 @@ export class RoomOccupancyList {
// Iterate over all bits in the current byte
for (let bit_i = 0; bit_i < 8; bit_i++) {
const isOccupied = (byte & (1 << (7 - bit_i))) !== 0;
const calculateOccupancyBitIndex = (byte_i: number, bit_i: number) => byte_i * 8 + bit_i;
const calculateOccupancyBitIndex = (byte_i: number, bit_i: number) =>
byte_i * 8 + bit_i;
if (firstOccupancyBit === null) {
if (isOccupied) {
@ -226,8 +227,14 @@ export class RoomOccupancyList {
}
} else {
if (!isOccupied) {
const startTime = addMinutes(start, firstOccupancyBit * granularity);
const endTime = addMinutes(start, calculateOccupancyBitIndex(byte_i, bit_i) * granularity);
const startTime = addMinutes(
start,
firstOccupancyBit * granularity,
);
const endTime = addMinutes(
start,
calculateOccupancyBitIndex(byte_i, bit_i) * granularity,
);
// add event between start and end of a block of boolean true values
occupancyList.push(
new AnonymizedOccupancy(
@ -315,7 +322,8 @@ export class RoomOccupancyList {
json.granularity,
json.blocks,
json.rooms.map(
(room: { name: string, occupancy: Binary }) => new RoomOccupancy(room.name, room.occupancy),
(room: { name: string; occupancy: Binary }) =>
new RoomOccupancy(room.name, room.occupancy),
),
);
}

View File

@ -15,10 +15,20 @@
//along with this program. If not, see <https://www.gnu.org/licenses/>.
import { defineStore } from "pinia";
import { useLocalStorage } from "@vueuse/core";
import { Calendar } from "../model/calendar";
const tokenStore = defineStore("tokenStore", {
state: () => ({
token: "",
feed: useLocalStorage("feed", {
collectionId: "",
collectionName: "",
created: "",
id: "",
modules: [],
updated: "",
} as Calendar),
}),
persist: true,
actions: {

View File

@ -50,22 +50,67 @@ function loadCalendar(): void {
moduleStore().removeAllModules();
tokenStore().setToken(token.value);
if (navigator.onLine) {
getCalender(token.value).then((data: Module[]) => {
if (data.length > 0) {
data.forEach((module) => {
moduleStore().addModule(module);
});
modules.value = moduleStore().modules;
toast.add({
severity: "success",
summary: t("editCalendarView.toast.success"),
detail: t("editCalendarView.calendarLoadedFromServer"),
life: 3000,
});
router.push("/edit-calendar");
} else {
toast.add({
severity: "error",
summary: t("editCalendarView.error"),
summary: t("editCalendarView.toast.error"),
detail: t("editCalendarView.noCalendarFound"),
life: 3000,
});
}
});
} else {
if(tokenStore().feed.id === token.value) {
// get data from tokenStore feed if offline
const offlineModules = tokenStore().feed.modules;
if (offlineModules.length > 0) {
offlineModules.forEach((module) => {
moduleStore().addModule(module);
});
toast.add({
severity: "info",
summary: t("editCalendarView.toast.info"),
detail: t("editCalendarView.calendarLoadedFromOfflineMode"),
life: 3000,
});
} else {
toast.add({
severity: "info",
summary: t("editCalendarView.info"),
detail: t("editCalendarView.calendarContainsNoModules"),
life: 3000,
});
}
} else {
toast.add({
severity: "error",
summary: t("editCalendarView.toast.error"),
detail: t("editCalendarView.noCalendarInOfflineMode"),
life: 3000,
});
return;
}
modules.value = moduleStore().modules;
router.push("/edit-calendar");
}
}
</script>

View File

@ -12,6 +12,7 @@ const { t } = useI18n({ useScope: "global" });
const toast = useToast();
const token = ref(tokenStore().token || ("" as string));
const calendarViewerRef = ref<InstanceType<typeof CalendarViewer>>();
// parse token from query parameter
const urlParams = new URLSearchParams(window.location.search);
@ -22,8 +23,6 @@ if (tokenFromUrl) {
loadCalendar();
}
const calendarViewerRef = ref<InstanceType<typeof CalendarViewer>>();
function loadCalendar() {
try {
token.value = extractToken(token.value);

View File

@ -96,6 +96,18 @@ export default defineConfig({
},
},
},
{
// Add the runtime caching strategy for /api/modules
urlPattern: ({url}) => url.pathname.startsWith('/api/modules'),
method: 'GET',
handler: 'NetworkFirst',
options: {
cacheName: 'modules-cache',
expiration: {
maxAgeSeconds: 24 * 60 * 60, // 1 day
},
},
},
{
urlPattern: /^https?.*/,
handler: "NetworkFirst",