Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove caching as data is now fetched instantly anyway #36

Merged
merged 2 commits into from
Jan 9, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 17 additions & 39 deletions src/lib/components/features/calendar-table.svelte
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
<script context="module" lang="ts">
type CacheData = Array<string>;
const dataCache: Record<string, CacheData> = {};
</script>

<script lang="ts">
import type { CalendarEntry } from "$lib/types/calendar-entry";
import type { MonthValue } from "$lib/types/calendar-month";
Expand All @@ -13,45 +8,32 @@
export let selectedType: TypeValue;

let entries: CalendarEntry[] = [];
let isLoading = false;
let isLoading = true;

// split <number> and <string> ("WT"/"LWT")
function formatWorkday(workday: string): string {
return workday.replace(/(\d+)([a-zA-Z]+)/g, "$1 $2");
}

async function fetchData(): Promise<CacheData> {
const cacheKey = `${selectedYear}-${selectedType}`;

if (dataCache[cacheKey]) {
return dataCache[cacheKey];
}

async function fetchData(): Promise<string[]> {
const requestUrl =
selectedType === "ALL"
? `https://fristenkalender.azurewebsites.net/api/GenerateAllFristen/${selectedYear}`
: `https://fristenkalender.azurewebsites.net/api/GenerateFristenForType/${selectedYear}/${selectedType}`;

try {
const response = await fetch(requestUrl, {
method: "GET",
mode: "cors",
});
const response = await fetch(requestUrl, {
method: "GET",
mode: "cors",
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was macht dieser mode: "cors"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point; ist actually obsolet, weil das der default fetching mode ist, wenn die azure functions angesprochen werden. habs entfernt.


if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

const data = await response.json();
dataCache[cacheKey] = data;
return data;
} catch (error) {
console.error("Error fetching data:", error);
throw error;
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

return response.json();
}

function processData(data: CacheData): CalendarEntry[] {
function processData(data: string[]): CalendarEntry[] {
let yearSelected = selectedYear;
let monthSelected = parseInt(selectedMonth);

Expand Down Expand Up @@ -114,14 +96,12 @@

async function loadData(): Promise<void> {
if (!selectedYear || !selectedMonth || !selectedType) return;

isLoading = true;

try {
const data = await fetchData();
entries = processData(data);
} catch (error) {
console.error("Error in loadData:", error);
console.error("Error while attempting to load data", error);
entries = [];
} finally {
isLoading = false;
Expand All @@ -134,13 +114,11 @@
</script>

<div class="h-full w-full relative flex flex-col overflow-hidden">
{#if isLoading}
<div class="flex justify-center items-center p-4">
<span>Lade Fristenkalender ...</span>
</div>
{:else if entries.length === 0}
<span>Keine Fristen für den ausgewählten Zeitraum gefunden.</span>
{:else}
{#if !isLoading && entries.length === 0}
<span class="text-lg text-transform: uppercase"
>Keine Fristen für den ausgewählten Zeitraum gefunden.</span
>
{:else if !isLoading}
<div class="overflow-auto flex-1 min-h-0">
<table class="w-full text-left">
<thead class="text-sm bg-tint uppercase sticky top-0 z-10">
Expand Down
Loading