feat: use static data instead of fetching from the API

This commit is contained in:
2025-07-12 02:12:54 +02:00
parent df241f9a30
commit 0a7e92be06
2 changed files with 358 additions and 47 deletions

334
src/demoData.js Normal file
View File

@ -0,0 +1,334 @@
export const DEMO_SESSION_INFO = {
authenticated: true,
appliedKeys: [],
permissions: [],
};
export const DEMO_CLASS_LIST = ["Demo", "Empty"];
export const DEMO_TIMETABLE = {
timetables: [
{
id: 1,
title: "Demo",
createdAt: "2023-08-28T08:00:53.233Z",
updatedAt: "2025-01-07T11:37:42.590Z",
class: "Demo",
validFrom: "2023-08-28T08:00:53.233Z",
validUntil: null,
data: [
[
[{}],
[{ room: "R 206", length: 1, subject: "MA1", teacher: "Wen" }],
[{ room: "R 307", length: 2, subject: "en1", teacher: "Fre" }],
[{ room: "R 005", length: 2, subject: "de2", teacher: "Str" }],
],
[
[{ room: "R 107", length: 2, subject: "IN2", teacher: "Kom" }],
[{ room: "R 206", length: 2, subject: "MA1", teacher: "Wen" }],
[{ room: "R 403", subject: "geo2", teacher: "Spl" }],
[{ room: "", length: 1, subject: "", teacher: "" }],
[{ room: "R 007", subject: "Sp-Th", teacher: "Bun" }],
[{ room: "R 313", subject: "pw1", teacher: "Göl" }],
],
[
[{ room: "R 313", length: 2, subject: "pw1", teacher: "Göl" }],
[{ room: "R 314", length: 2, subject: "ge1", teacher: "Mog" }],
[{ room: "R 307", subject: "en1", teacher: "Fre" }],
[{ room: "R 403", length: 2, subject: "geo2", teacher: "Spl" }],
[{}],
[{ room: "R 002", length: 2, subject: "Sp-Fit", teacher: "Gan" }],
],
[
[{ room: "R 206", length: 2, subject: "MA1", teacher: "Wen" }],
[{ room: "R 107", length: 2, subject: "IN2", teacher: "Kom" }],
[{ room: "R 209", subject: "ph1", teacher: "And" }],
[{ room: "R 005", length: 2, subject: "Sp-Th", teacher: "Bun" }],
],
[
[{ room: "R 107", length: 2, subject: "IN2", teacher: "Kom" }],
[{ room: "R 314", length: 1, subject: "ge1", teacher: "Mog" }],
[{ room: "R 005", subject: "de2", teacher: "Str" }],
[{ room: "R 209", length: 2, subject: "ph1", teacher: "And" }],
],
],
source: "Demo Provider",
trusted: true,
},
],
times: [
{
lesson: 1,
start: "1970-01-01T07:00:00.000Z",
end: "1970-01-01T07:45:00.000Z",
},
{
lesson: 3,
start: "1970-01-01T08:50:00.000Z",
end: "1970-01-01T09:35:00.000Z",
},
{
lesson: 5,
start: "1970-01-01T11:10:00.000Z",
end: "1970-01-01T11:55:00.000Z",
},
{
lesson: 6,
start: "1970-01-01T12:00:00.000Z",
end: "1970-01-01T12:45:00.000Z",
},
{
lesson: 7,
start: "1970-01-01T12:50:00.000Z",
end: "1970-01-01T13:35:00.000Z",
},
{
lesson: 8,
start: "1970-01-01T13:40:00.000Z",
end: "1970-01-01T14:25:00.000Z",
},
{
lesson: 9,
start: "1970-01-01T14:30:00.000Z",
end: "1970-01-01T15:15:00.000Z",
},
{
lesson: 10,
start: "1970-01-01T15:20:00.000Z",
end: "1970-01-01T16:05:00.000Z",
},
{
lesson: 11,
start: "1970-01-01T16:10:00.000Z",
end: "1970-01-01T16:55:00.000Z",
},
{
lesson: 2,
start: "1970-01-01T07:45:00.000Z",
end: "1970-01-01T08:30:00.000Z",
},
{
lesson: 4,
start: "1970-01-01T09:40:00.000Z",
end: "1970-01-01T10:25:00.000Z",
},
],
};
export function getDemoSubstitutions(date) {
let weekday = new Date(date).getDay();
switch (weekday) {
case 1: {
return [
{
id: 0,
class: ["Demo"],
type: "cancellation",
rawType: "Entfall",
lesson: 5,
date: date,
notes: null,
teacher: "Str",
change: {},
},
{
id: 1,
class: ["Demo"],
type: "cancellation",
rawType: "Entfall",
lesson: 6,
date: date,
notes: null,
teacher: "Str",
change: {},
},
{
id: 2,
class: ["Demo"],
type: "change",
rawType: "Raum-Vtr.",
lesson: 3,
date: date,
notes: null,
teacher: "Fre",
change: {
room: "308",
teacher: "Fre",
subject: "en1",
},
},
];
}
case 2: {
return [
{
id: 3,
class: ["Demo"],
type: "cancellation",
rawType: "Entfall",
lesson: 7,
date: date,
notes: null,
teacher: "Bun",
change: {},
},
{
id: 4,
class: ["Demo"],
type: "change",
rawType: "Vertretung",
lesson: 8,
date: date,
notes: null,
teacher: "Bun",
change: {
room: "007",
teacher: "Aci",
subject: "Sp-Th",
},
},
];
}
case 3: {
return [
{
id: 5,
class: ["Demo"],
type: "cancellation",
rawType: "Entfall",
lesson: 9,
date: date,
notes: "Aufgaben im Sekretariat",
teacher: "Gan",
change: {},
},
{
id: 6,
class: ["Demo"],
type: "cancellation",
rawType: "Entfall",
lesson: 10,
date: date,
notes: null,
teacher: "Gan",
change: {},
},
];
}
}
return [];
}
export function getDemoHistory(date) {
let weekday = new Date(date).getDay();
switch (weekday) {
case 1: {
return [
...getDemoSubstitutions(date).map((e) => substitutionToChange(e)),
];
}
case 2: {
return [
...getDemoSubstitutions(date).map((e) => substitutionToChange(e)),
{
id: 101,
type: "deletion",
class: ["Demo"],
substitutionId: 100,
lesson: 5,
updatedAt: date - 47800000,
date: date,
teacher: null,
change: {
date: date,
type: "cancellation",
class: ["Demo"],
notes: null,
change: {},
lesson: 5,
rawType: "Entfall",
teacher: "Spl",
},
parseEventId: 0,
},
{
id: 100,
type: "addition",
class: ["Demo"],
substitutionId: 100,
lesson: 5,
updatedAt: date - 57800000,
date: date,
teacher: null,
change: {
date: date,
type: "cancellation",
class: ["Demo"],
notes: null,
change: {},
lesson: 5,
rawType: "Entfall",
teacher: "Spl",
},
parseEventId: 0,
},
];
}
case 3: {
return [
{
id: 102,
type: "change",
class: ["Demo"],
substitutionId: 5,
lesson: 9,
updatedAt: date - 57800000,
date: date,
teacher: null,
change: {
notes: {
before: null,
after: "Aufgaben im Sekretariat",
},
},
parseEventId: 0,
},
...getDemoSubstitutions(date)
.map((e) => substitutionToChange(e))
.map((e) => {
e.change.notes = null;
return e;
}),
];
}
}
return [];
}
function substitutionToChange(substitution) {
return {
id: substitution.id,
type: "addition",
class: substitution.class,
substitutionId: substitution.id,
lesson: substitution.lesson,
updatedAt: substitution.date - 46800000,
date: substitution.date,
teacher: null,
change: {
date: substitution.date,
type: substitution.type,
class: substitution.class,
notes: substitution.notes,
change: substitution.change,
lesson: substitution.lesson,
rawType: substitution.rawType,
teacher: substitution.teacher,
},
parseEventId: 0,
};
}

View File

@ -1,6 +1,13 @@
import { ref, watch, computed } from "vue";
import { getNextAndPrevDay, setUTCMidnight } from "@/util";
import i18n from "@/i18n";
import {
DEMO_CLASS_LIST,
DEMO_SESSION_INFO,
DEMO_TIMETABLE,
getDemoHistory,
getDemoSubstitutions,
} from "./demoData";
/* Router */
export const shouldLogin = ref(false);
@ -15,7 +22,7 @@ export const profiles = ref(
id: 0,
name: "Default Profile",
classFilter: "none",
timetableId: "none",
timetableId: 1,
timetableGroups: [],
},
],
@ -162,16 +169,7 @@ watch(selectedDate, () =>
export async function fetchSessionInfo() {
try {
const checkResponse = await fetch(`${baseUrl}/info`);
if (checkResponse.status == 401) {
shouldLogin.value = true;
return false;
} else if (checkResponse.status != 200) {
console.log("Other error while fetching data: " + checkResponse.status);
return false;
} else {
sessionInfo.value = await checkResponse.json();
}
sessionInfo.value = DEMO_SESSION_INFO;
} catch {
console.log("Error while fetching data: No internet connection!");
return false;
@ -180,53 +178,32 @@ export async function fetchSessionInfo() {
}
export async function fetchClassList() {
const classListResponse = await fetch(`${baseUrl}/classes`);
const classListData = await classListResponse.json();
classList.value = classListData;
classList.value = DEMO_CLASS_LIST;
}
export async function fetchTimetables() {
const timetableResponse = await fetch(
`${baseUrl}/timetable?class=${activeProfile.value.classFilter}`,
);
const timetableData = await timetableResponse.json();
if (timetableData.error) {
console.warn("API Error: " + timetableData.error);
timetables.value = [];
if (activeProfile.value.classFilter == "Demo") {
timetables.value = DEMO_TIMETABLE.timetables;
} 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;
}
timetables.value = [];
}
times.value = DEMO_TIMETABLE.times;
}
export async function fetchSubstitutions(day) {
const requestDate = `?date=${day}`;
const substitutionResponse = await fetch(
activeProfile.value.classFilter == "none"
? `${baseUrl}/substitutions${requestDate}`
: `${baseUrl}/substitutions${requestDate}&class=${activeProfile.value.classFilter}`,
);
const substitutionData = await substitutionResponse.json();
substitutions.value[day] = substitutionData;
if (activeProfile.value.classFilter == "Demo") {
substitutions.value[day] = getDemoSubstitutions(day);
} else {
substitutions.value[day] = [];
}
}
export async function fetchHistory(day) {
const requestDate = `?date=${day}`;
const historyResponse = await fetch(
activeProfile.value.classFilter == "none"
? `${baseUrl}/history${requestDate}`
: `${baseUrl}/history${requestDate}&class=${activeProfile.value.classFilter}`,
);
const historyData = await historyResponse.json();
if (historyData.error) console.warn("API Error: " + historyData.error);
else history.value[day] = historyData;
if (activeProfile.value.classFilter == "Demo") {
history.value[day] = getDemoHistory(day);
} else {
history.value[day] = [];
}
}
/* Preprocess the timetable data */