-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
145 lines (123 loc) · 4.91 KB
/
Copy pathscript.js
File metadata and controls
145 lines (123 loc) · 4.91 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
document.addEventListener('DOMContentLoaded', () => {
const medForm = document.getElementById('med-form');
const medicineList = document.getElementById('medicine-list');
const totalMedsEl = document.getElementById('total-meds');
const takenTodayEl = document.getElementById('taken-today');
// State
let medicines = JSON.parse(localStorage.getItem('medicines')) || [];
// Initialize
renderMedicines();
updateStats();
// Event Listeners
medForm.addEventListener('submit', (e) => {
e.preventDefault();
const nameInput = document.getElementById('med-name');
const dosageInput = document.getElementById('med-dosage');
const timeInput = document.getElementById('med-time');
const newMedicine = {
id: Date.now().toString(),
name: nameInput.value,
dosage: dosageInput.value,
time: timeInput.value,
taken: false,
dateAdded: new Date().toLocaleDateString()
};
medicines.push(newMedicine);
saveToLocalStorage();
// Clear form
nameInput.value = '';
dosageInput.value = '';
timeInput.value = '';
renderMedicines();
updateStats();
});
// Delegated events for check and delete
medicineList.addEventListener('click', (e) => {
const checkBtn = e.target.closest('.btn-check');
const deleteBtn = e.target.closest('.btn-delete');
if (checkBtn) {
const id = checkBtn.dataset.id;
toggleTaken(id);
}
if (deleteBtn) {
const id = deleteBtn.dataset.id;
deleteMedicine(id);
}
});
// Functions
function toggleTaken(id) {
medicines = medicines.map(med => {
if (med.id === id) {
return { ...med, taken: !med.taken };
}
return med;
});
saveToLocalStorage();
renderMedicines();
updateStats();
}
function deleteMedicine(id) {
// Add a slight animation out before deleting
const item = document.querySelector(`.med-item[data-id="${id}"]`);
if (item) {
item.style.transform = 'scale(0.9)';
item.style.opacity = '0';
setTimeout(() => {
medicines = medicines.filter(med => med.id !== id);
saveToLocalStorage();
renderMedicines();
updateStats();
}, 300);
}
}
function renderMedicines() {
if (medicines.length === 0) {
medicineList.innerHTML = `
<div style="text-align: center; color: var(--text-muted); padding: 2rem;">
<i class="fa-solid fa-leaf" style="font-size: 2rem; margin-bottom: 1rem; opacity: 0.5;"></i>
<p>No medicines scheduled. Enjoy the peace of the forest.</p>
</div>
`;
return;
}
// Sort by time
const sortedMeds = [...medicines].sort((a, b) => a.time.localeCompare(b.time));
medicineList.innerHTML = sortedMeds.map(med => `
<div class="med-item ${med.taken ? 'taken' : ''}" data-id="${med.id}">
<div class="med-info">
<div class="med-time">${formatTime(med.time)}</div>
<div class="med-details">
<h4 class="med-name">${med.name}</h4>
<p>${med.dosage}</p>
</div>
</div>
<div class="med-actions">
<button class="btn-icon btn-check" data-id="${med.id}" title="${med.taken ? 'Mark as not taken' : 'Mark as taken'}">
<i class="fa-solid ${med.taken ? 'fa-rotate-left' : 'fa-check'}"></i>
</button>
<button class="btn-icon btn-delete" data-id="${med.id}" title="Delete">
<i class="fa-solid fa-trash"></i>
</button>
</div>
</div>
`).join('');
}
function updateStats() {
totalMedsEl.textContent = medicines.length;
const takenCount = medicines.filter(med => med.taken).length;
takenTodayEl.textContent = takenCount;
}
function saveToLocalStorage() {
localStorage.setItem('medicines', JSON.stringify(medicines));
}
// Helper: Convert 24h to 12h format
function formatTime(time24) {
if (!time24) return '';
const [hours24, minutes] = time24.split(':');
let hours = parseInt(hours24, 10);
const ampm = hours >= 12 ? 'PM' : 'AM';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
return `${hours}:${minutes} ${ampm}`;
}
});