Repository navigation
Expand file tree
/
Copy pathedit.js
More file actions
125 lines (88 loc) · 3.58 KB
/
Copy pathedit.js
File metadata and controls
125 lines (88 loc) · 3.58 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
const timetable = JSON.parse(localStorage.getItem("timetable")) || {};
const container = document.querySelector(".container");
const dateInput = document.querySelector(".date-input");
dateInput.addEventListener("change", () => {
const selectedDate = dateInput.value;
document.querySelectorAll(".subject-row").forEach(el => el.remove());
document.querySelectorAll(".msg").forEach(el => el.remove());
if (!selectedDate) return;
const today = new Date();
const selected = new Date(selectedDate);
today.setHours(0,0,0,0);
selected.setHours(0,0,0,0);
if (selected > today) {
const msg = document.createElement("p");
msg.className = "msg";
msg.innerText = "This date has not occurred yet";
container.insertBefore(msg, document.querySelector(".btn"));
return;
}
const dayName = selected.toLocaleDateString("en-US", { weekday: "long" });
const subjects = timetable[dayName];
if (!subjects || subjects.length === 0) {
const msg = document.createElement("p");
msg.className = "msg";
msg.innerText = "No lectures on this day.";
container.insertBefore(msg, document.querySelector(".btn"));
return;
}
subjects.forEach(sub => {
const row = document.createElement("div");
row.className = "subject-row";
row.innerHTML = `
<div class="subject-name">${sub}</div>
<div class="options">
<label class="option present">
<input type="radio" name="${sub}" value="P">
<span>Present</span>
</label>
<label class="option absent">
<input type="radio" name="${sub}" value="A">
<span>Absent</span>
</label>
<label class="option holiday">
<input type="radio" name="${sub}" value="H">
<span>Holiday</span>
</label>
</div>
`;
container.insertBefore(row, document.querySelector(".btn"));
});
const data = JSON.parse(localStorage.getItem("attendance")) || {};
const dayData = data[selectedDate];
if (!dayData) {
const msg = document.createElement("p");
msg.className = "msg";
msg.innerText = "No attendance recorded for this day";
container.insertBefore(msg, document.querySelector(".btn"));
return;
}
document.querySelectorAll(".subject-row").forEach(row => {
const subject = row.querySelector(".subject-name").innerText;
const value = dayData[subject];
if (value) {
const radio = row.querySelector(`input[value="${value}"]`);
if (radio) radio.checked = true;
}
});
});
const updateBtn = document.querySelector(".btn");
updateBtn.addEventListener("click", () => {
const date = dateInput.value;
if (!date) {
alert("Select a date first");
return;
}
let data = JSON.parse(localStorage.getItem("attendance")) || {};
let dayData = {};
document.querySelectorAll(".subject-row").forEach(row => {
const subject = row.querySelector(".subject-name").innerText;
const selected = row.querySelector("input:checked");
if (selected) {
dayData[subject] = selected.value;
}
});
data[date] = dayData;
localStorage.setItem("attendance", JSON.stringify(data));
alert("Updated!");
});