forked from smlab-niser/messmenu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
126 lines (103 loc) · 3.78 KB
/
Copy pathscript.js
File metadata and controls
126 lines (103 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const csvUrl = "https://docs.google.com/spreadsheets/d/e/2PACX-1vTteP2NIiGzPK3qMd5j9JIW9ao9K-WqSG17mtg6yYerMx6D0jAhps9HzylBk8thkYfZKpusRLayRzOd/pub?gid=814693963&single=true&output=csv";
const daysOrder = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
let menuData = {};
async function fetchMenu() {
try {
const response = await fetch(csvUrl);
const csvText = await response.text();
const parsed = Papa.parse(csvText, { header: true, skipEmptyLines: true });
const dataRows = parsed.data;
if (dataRows.length === 0) return;
// Take the first row's timestamp from "Column 1"
const firstTimestamp = parseTimestamp(dataRows[0]["Timestamp"]);
const weekRange = getWeekRange(firstTimestamp);
document.getElementById("week-title").textContent = `${weekRange.start} - ${weekRange.end}`;
// Build menuData sorted by daysOrder
dataRows.forEach(row => {
const day = row["Day"]?.trim();
if (!day || !daysOrder.includes(day)) return;
menuData[day] = {
breakfast: row["Breakfast"]?.trim(),
lunch: row["Lunch"]?.trim(),
// snacks: row["Snacks"]?.trim(),
dinner: row["Dinner"]?.trim()
};
});
renderTabs();
} catch (err) {
console.error("Error fetching menu:", err);
}
}
function parseTimestamp(ts) {
// Example: "9/17/2025 9:43:24"
const [datePart, timePart] = ts.split(" ");
const [month, day, year] = datePart.split("/").map(Number);
const [hour, minute, second] = timePart.split(":").map(Number);
return new Date(year, month - 1, day, hour, minute, second || 0);
}
function getWeekRange(date) {
const day = date.getDay(); // 0 = Sunday, 1 = Monday, ..., 6 = Saturday
// Find Monday of this week
const monday = new Date(date);
const diffToMonday = (day + 6) % 7; // turns Sunday(0) → 6, Monday(1) → 0, etc.
monday.setDate(date.getDate() - diffToMonday);
const sunday = new Date(monday);
sunday.setDate(monday.getDate() + 6);
return {
start: formatDate(monday),
end: formatDate(sunday)
};
}
function formatDate(date) {
const options = { month: "short", day: "numeric" };
return date.toLocaleDateString(undefined, options);
}
function getTodayName() {
const jsDay = new Date().getDay(); // 0 = Sunday, 1 = Monday, ..., 6 = Saturday
// convert to Monday=0 ... Sunday=6
const mondayFirstIndex = (jsDay + 6) % 7;
return daysOrder[mondayFirstIndex];
}
function renderTabs() {
const tabsContainer = document.querySelector(".tabs");
tabsContainer.innerHTML = "";
let defaultDay = getTodayName();
daysOrder.forEach(day => {
if (menuData[day]) {
const btn = document.createElement("button");
btn.textContent = day;
btn.onclick = () => {
document.querySelectorAll(".tabs button").forEach(b => b.classList.remove("active"));
btn.classList.add("active");
renderMenu(day);
};
tabsContainer.appendChild(btn);
// If this is today’s tab, make it active
if (day === defaultDay) {
btn.classList.add("active");
}
}
});
// Render today’s menu if available, otherwise fall back to Sunday
if (menuData[defaultDay]) {
renderMenu(defaultDay);
} else {
const firstAvailableDay = Object.keys(menuData)[0];
if (firstAvailableDay) {
tabsContainer.querySelector("button").classList.add("active");
renderMenu(firstAvailableDay);
}
}
}
function renderMenu(day) {
const menuContainer = document.getElementById("menu");
menuContainer.innerHTML = "";
const meals = menuData[day];
for (let [meal, value] of Object.entries(meals)) {
const div = document.createElement("div");
div.className = "meal";
div.innerHTML = `<span>${meal.toUpperCase()}:</span> ${value || "Not available"}`;
menuContainer.appendChild(div);
}
}
fetchMenu();