Implement caching of timetable data

This commit is contained in:
2023-08-27 14:12:38 +02:00
parent 21b2e68198
commit 493a6ee05b

View File

@ -30,19 +30,6 @@ export const activeProfileId = ref(
localStorage.getItem("activeProfile") || profiles.value[0].id,
);
watch(
() => activeProfile.value.classFilter,
() => {
fetchData(getNextAndPrevDay(selectedDate.value), false);
},
);
export const localTimetables = ref(
JSON.parse(localStorage.getItem("timetables")) || [],
);
export const theme = ref(localStorage.getItem("theme") || "auto");
watch(
profiles,
(newValue) => {
@ -53,6 +40,27 @@ watch(
watch(activeProfileId, (newValue) => {
localStorage.setItem("activeProfile", newValue);
});
watch(
() => activeProfile.value.classFilter,
() => {
fetchData(getNextAndPrevDay(selectedDate.value), false);
},
);
export const cachedTimetables = ref(
JSON.parse(localStorage.getItem("cachedTimetables")) || {},
);
export const localTimetables = ref(
JSON.parse(localStorage.getItem("timetables")) || [],
);
watch(
cachedTimetables,
(newValue) => {
localStorage.setItem("cachedTimetables", JSON.stringify(newValue));
},
{ deep: true },
);
watch(
localTimetables,
(newValue) => {
@ -60,6 +68,8 @@ watch(
},
{ deep: true },
);
export const theme = ref(localStorage.getItem("theme") || "auto");
watch(theme, (newValue) => {
localStorage.setItem("theme", newValue);
});
@ -92,8 +102,12 @@ export const timetable = computed(() => {
);
});
export const sessionInfo = ref({});
export const timetables = ref([]);
export const times = ref([]);
export const timetables = ref(
(cachedTimetables.value[activeProfileId.value] || {}).timetables || [],
);
export const times = ref(
(cachedTimetables.value[activeProfileId.value] || {}).times || [],
);
export const substitutions = ref({});
export const history = ref({});
export const classList = ref([]);
@ -182,6 +196,13 @@ export async function fetchTimetables() {
} else {
timetables.value = timetableData.timetables;
times.value = timetableData.times;
cachedTimetables.value[activeProfileId.value] =
structuredClone(timetableData);
for (const timetable of cachedTimetables.value[activeProfileId.value]
.timetables) {
timetable.fromCache = true;
}
}
}