added reset on refresh
This commit is contained in:
parent
6748434fd7
commit
83324134b3
1 changed files with 157 additions and 93 deletions
|
@ -687,6 +687,63 @@ declare global {
|
||||||
hasFood: "all",
|
hasFood: "all",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Add cache for events
|
||||||
|
let cachedEvents: Event[] = [];
|
||||||
|
let lastCacheUpdate = 0; // Will force a refresh on page load
|
||||||
|
const CACHE_DURATION = 5 * 60 * 1000; // 5 minutes in milliseconds
|
||||||
|
|
||||||
|
// Function to refresh cache
|
||||||
|
async function refreshCache() {
|
||||||
|
try {
|
||||||
|
const now = Date.now();
|
||||||
|
|
||||||
|
// Always refresh on page load (when lastCacheUpdate is 0)
|
||||||
|
if (
|
||||||
|
lastCacheUpdate === 0 ||
|
||||||
|
now - lastCacheUpdate >= CACHE_DURATION ||
|
||||||
|
cachedEvents.length === 0
|
||||||
|
) {
|
||||||
|
auth.setUpdating(true);
|
||||||
|
const response = await get.getAll<Event>("events");
|
||||||
|
cachedEvents = response.map((event) =>
|
||||||
|
Get.convertUTCToLocal(event)
|
||||||
|
);
|
||||||
|
lastCacheUpdate = now;
|
||||||
|
|
||||||
|
// Initialize year filter options from cache
|
||||||
|
const years = new Set<number>();
|
||||||
|
cachedEvents.forEach((event) => {
|
||||||
|
const year = new Date(event.start_date).getFullYear();
|
||||||
|
years.add(year);
|
||||||
|
});
|
||||||
|
|
||||||
|
const yearFilter = document.getElementById(
|
||||||
|
"yearFilter"
|
||||||
|
) as HTMLSelectElement;
|
||||||
|
if (yearFilter) {
|
||||||
|
const sortedYears = Array.from(years).sort((a, b) => b - a);
|
||||||
|
yearFilter.innerHTML =
|
||||||
|
'<option value="all">All Years</option>' +
|
||||||
|
sortedYears
|
||||||
|
.map(
|
||||||
|
(year) =>
|
||||||
|
`<option value="${year}">${year}</option>`
|
||||||
|
)
|
||||||
|
.join("");
|
||||||
|
|
||||||
|
if (filterState.year !== "all") {
|
||||||
|
yearFilter.value = filterState.year;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Failed to refresh cache:", error);
|
||||||
|
throw error;
|
||||||
|
} finally {
|
||||||
|
auth.setUpdating(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Helper function to determine current academic term
|
// Helper function to determine current academic term
|
||||||
function getCurrentTerm(): { start: Date; end: Date } {
|
function getCurrentTerm(): { start: Date; end: Date } {
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
|
@ -759,61 +816,6 @@ declare global {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add cache for events
|
|
||||||
let cachedEvents: Event[] = [];
|
|
||||||
let lastCacheUpdate = 0;
|
|
||||||
const CACHE_DURATION = 5 * 60 * 1000; // 5 minutes in milliseconds
|
|
||||||
|
|
||||||
// Function to refresh cache
|
|
||||||
async function refreshCache() {
|
|
||||||
try {
|
|
||||||
const now = Date.now();
|
|
||||||
if (
|
|
||||||
now - lastCacheUpdate < CACHE_DURATION &&
|
|
||||||
cachedEvents.length > 0
|
|
||||||
) {
|
|
||||||
return; // Use cached data if it's fresh
|
|
||||||
}
|
|
||||||
|
|
||||||
auth.setUpdating(true);
|
|
||||||
const response = await get.getAll<Event>("events");
|
|
||||||
cachedEvents = response.map((event) =>
|
|
||||||
Get.convertUTCToLocal(event)
|
|
||||||
);
|
|
||||||
lastCacheUpdate = now;
|
|
||||||
|
|
||||||
// Initialize year filter options from cache
|
|
||||||
const years = new Set<number>();
|
|
||||||
cachedEvents.forEach((event) => {
|
|
||||||
const year = new Date(event.start_date).getFullYear();
|
|
||||||
years.add(year);
|
|
||||||
});
|
|
||||||
|
|
||||||
const yearFilter = document.getElementById(
|
|
||||||
"yearFilter"
|
|
||||||
) as HTMLSelectElement;
|
|
||||||
if (yearFilter) {
|
|
||||||
const sortedYears = Array.from(years).sort((a, b) => b - a);
|
|
||||||
yearFilter.innerHTML =
|
|
||||||
'<option value="all">All Years</option>' +
|
|
||||||
sortedYears
|
|
||||||
.map(
|
|
||||||
(year) => `<option value="${year}">${year}</option>`
|
|
||||||
)
|
|
||||||
.join("");
|
|
||||||
|
|
||||||
if (filterState.year !== "all") {
|
|
||||||
yearFilter.value = filterState.year;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Failed to refresh cache:", error);
|
|
||||||
throw error;
|
|
||||||
} finally {
|
|
||||||
auth.setUpdating(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Function to apply filters to cached data
|
// Function to apply filters to cached data
|
||||||
function filterEvents(events: Event[]): Event[] {
|
function filterEvents(events: Event[]): Event[] {
|
||||||
return events.filter((event) => {
|
return events.filter((event) => {
|
||||||
|
@ -1219,6 +1221,68 @@ declare global {
|
||||||
fetchEvents();
|
fetchEvents();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Initial setup
|
||||||
|
if (typeof window !== "undefined") {
|
||||||
|
window.addEventListener("DOMContentLoaded", () => {
|
||||||
|
// Reset all filters to defaults
|
||||||
|
const timeFilterAll = document.querySelector(
|
||||||
|
'input[name="timeFilter"][value="all"]'
|
||||||
|
) as HTMLInputElement;
|
||||||
|
if (timeFilterAll) {
|
||||||
|
// Ensure the radio button is properly checked
|
||||||
|
timeFilterAll.checked = true;
|
||||||
|
filterState.time = "all";
|
||||||
|
}
|
||||||
|
|
||||||
|
const yearFilter = document.getElementById(
|
||||||
|
"yearFilter"
|
||||||
|
) as HTMLSelectElement;
|
||||||
|
if (yearFilter) yearFilter.value = "all";
|
||||||
|
|
||||||
|
const publishedFilter = document.getElementById(
|
||||||
|
"publishedFilter"
|
||||||
|
) as HTMLSelectElement;
|
||||||
|
if (publishedFilter) publishedFilter.value = "all";
|
||||||
|
|
||||||
|
const hasFilesFilter = document.getElementById(
|
||||||
|
"hasFilesFilter"
|
||||||
|
) as HTMLSelectElement;
|
||||||
|
if (hasFilesFilter) hasFilesFilter.value = "all";
|
||||||
|
|
||||||
|
const hasFoodFilter = document.getElementById(
|
||||||
|
"hasFoodFilter"
|
||||||
|
) as HTMLSelectElement;
|
||||||
|
if (hasFoodFilter) hasFoodFilter.value = "all";
|
||||||
|
|
||||||
|
const searchInput = document.getElementById(
|
||||||
|
"searchInput"
|
||||||
|
) as HTMLInputElement;
|
||||||
|
if (searchInput) searchInput.value = "";
|
||||||
|
|
||||||
|
const perPageSelect = document.getElementById(
|
||||||
|
"perPageSelect"
|
||||||
|
) as HTMLSelectElement;
|
||||||
|
if (perPageSelect) perPageSelect.value = "5";
|
||||||
|
|
||||||
|
// Reset variables
|
||||||
|
searchQuery = "";
|
||||||
|
perPage = 5;
|
||||||
|
currentPage = 1;
|
||||||
|
|
||||||
|
// Reset filter state
|
||||||
|
filterState = {
|
||||||
|
time: "all",
|
||||||
|
year: "all",
|
||||||
|
published: "all",
|
||||||
|
hasFiles: "all",
|
||||||
|
hasFood: "all",
|
||||||
|
};
|
||||||
|
|
||||||
|
// Fetch events with reset filters
|
||||||
|
fetchEvents();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Initial fetch
|
// Initial fetch
|
||||||
fetchEvents();
|
fetchEvents();
|
||||||
</script>
|
</script>
|
||||||
|
|
Loading…
Reference in a new issue