|
| 1 | +// Simple medicine reminder demo |
| 2 | +const STORAGE_KEY = 'medicine_reminder_v1'; |
| 3 | + |
| 4 | +const clientForm = document.getElementById('clientForm'); |
| 5 | +const clientId = document.getElementById('clientId'); |
| 6 | +const clientName = document.getElementById('clientName'); |
| 7 | +const medication = document.getElementById('medication'); |
| 8 | +const dosage = document.getElementById('dosage'); |
| 9 | +const doseTime = document.getElementById('doseTime'); |
| 10 | +const addTime = document.getElementById('addTime'); |
| 11 | +const timesList = document.getElementById('timesList'); |
| 12 | +const clientsEl = document.getElementById('clients'); |
| 13 | +const logEl = document.getElementById('log'); |
| 14 | + |
| 15 | +const modal = document.getElementById('reminderModal'); |
| 16 | +const reminderMsg = document.getElementById('reminderMsg'); |
| 17 | +const takenBtn = document.getElementById('takenBtn'); |
| 18 | +const missedBtn = document.getElementById('missedBtn'); |
| 19 | + |
| 20 | +let store = { clients: [], log: [], pending: [] }; |
| 21 | +let currentReminder = null; // {clientId, time, timerId, escalateId} |
| 22 | + |
| 23 | +function saveStore() { localStorage.setItem(STORAGE_KEY, JSON.stringify(store)); } |
| 24 | +function loadStore() { const raw = localStorage.getItem(STORAGE_KEY); if (raw) store = JSON.parse(raw); } |
| 25 | + |
| 26 | +function requestNotificationPermission() { |
| 27 | + if (!('Notification' in window)) return; |
| 28 | + if (Notification.permission === 'default') Notification.requestPermission(); |
| 29 | +} |
| 30 | + |
| 31 | +function formatTime(t) { return t; } |
| 32 | + |
| 33 | +function renderTimes(client) { |
| 34 | + // returns HTML for times |
| 35 | + return client.times.map(t => `<li>${t} <button data-remove='${t}' class='muted'>Remove</button></li>`).join('') |
| 36 | +} |
| 37 | + |
| 38 | +function render() { |
| 39 | + // clients |
| 40 | + clientsEl.innerHTML = ''; |
| 41 | + store.clients.forEach(c => { |
| 42 | + const li = document.createElement('li'); |
| 43 | + li.className = 'client-card'; |
| 44 | + li.innerHTML = ` |
| 45 | + <div class='client-meta'> |
| 46 | + <strong>${c.name}</strong> — ${c.medication}<br> |
| 47 | + <small>${c.dosage || ''}</small> |
| 48 | + <div>Times: ${c.times.join(', ')}</div> |
| 49 | + </div> |
| 50 | + <div class='client-actions'> |
| 51 | + <button data-edit='${c.id}'>Edit</button> |
| 52 | + <button data-delete='${c.id}' class='muted'>Delete</button> |
| 53 | + </div>`; |
| 54 | + clientsEl.appendChild(li); |
| 55 | + }); |
| 56 | + |
| 57 | + // log |
| 58 | + logEl.innerHTML = ''; |
| 59 | + store.log.slice().reverse().forEach(entry => { |
| 60 | + const li = document.createElement('li'); |
| 61 | + li.className = 'log-item'; |
| 62 | + li.textContent = `${new Date(entry.time).toLocaleString()}: ${entry.clientName} — ${entry.medication} — ${entry.action}`; |
| 63 | + logEl.appendChild(li); |
| 64 | + }); |
| 65 | +} |
| 66 | + |
| 67 | +function addClient(obj) { |
| 68 | + store.clients.push(obj); |
| 69 | + scheduleClientNotifications(obj); |
| 70 | + saveStore(); |
| 71 | + render(); |
| 72 | +} |
| 73 | + |
| 74 | +function updateClient(id, patch) { |
| 75 | + const idx = store.clients.findIndex(c => c.id === id); |
| 76 | + if (idx === -1) return; |
| 77 | + // cancel existing scheduled jobs for that client |
| 78 | + cancelScheduledForClient(id); |
| 79 | + store.clients[idx] = { ...store.clients[idx], ...patch }; |
| 80 | + scheduleClientNotifications(store.clients[idx]); |
| 81 | + saveStore(); |
| 82 | + render(); |
| 83 | +} |
| 84 | + |
| 85 | +function removeClient(id) { |
| 86 | + cancelScheduledForClient(id); |
| 87 | + store.clients = store.clients.filter(c => c.id !== id); |
| 88 | + saveStore(); |
| 89 | + render(); |
| 90 | +} |
| 91 | + |
| 92 | +function logAction({ clientId, clientName, medication, action }) { |
| 93 | + store.log.push({ clientId, clientName, medication, action, time: Date.now() }); |
| 94 | + saveStore(); |
| 95 | + render(); |
| 96 | +} |
| 97 | + |
| 98 | +function scheduleClientNotifications(client) { |
| 99 | + // for each time, compute next occurrence and set timeout |
| 100 | + client.times.forEach(time => { |
| 101 | + const next = nextOccurrenceTodayOrTomorrow(time); |
| 102 | + const ms = next - Date.now(); |
| 103 | + const timerId = setTimeout(() => triggerReminder(client, time), ms); |
| 104 | + store.pending.push({ clientId: client.id, time, timerId }); |
| 105 | + }); |
| 106 | +} |
| 107 | + |
| 108 | +function cancelScheduledForClient(clientId) { |
| 109 | + store.pending.filter(p => p.clientId === clientId).forEach(p => clearTimeout(p.timerId)); |
| 110 | + store.pending = store.pending.filter(p => p.clientId !== clientId); |
| 111 | +} |
| 112 | + |
| 113 | +function nextOccurrenceTodayOrTomorrow(timeStr) { |
| 114 | + const [hh, mm] = timeStr.split(':').map(Number); |
| 115 | + const now = new Date(); |
| 116 | + const candidate = new Date(now.getFullYear(), now.getMonth(), now.getDate(), hh, mm, 0, 0); |
| 117 | + if (candidate.getTime() <= Date.now()) candidate.setDate(candidate.getDate() + 1); |
| 118 | + return candidate.getTime(); |
| 119 | +} |
| 120 | + |
| 121 | +function triggerReminder(client, time) { |
| 122 | + const msg = `${client.name}: ${client.medication} — ${client.dosage || ''}`; |
| 123 | + // show notification |
| 124 | + if (Notification.permission === 'granted') { |
| 125 | + new Notification('Medication due', { body: msg, tag: `${client.id}-${time}` }); |
| 126 | + } |
| 127 | + // show in-app modal |
| 128 | + currentReminder = { client, time, escalateId: null }; |
| 129 | + reminderMsg.textContent = msg; |
| 130 | + modal.classList.remove('hidden'); |
| 131 | + |
| 132 | + // start escalation after 10 minutes (600k ms): repeat every minute until acknowledged |
| 133 | + const grace = 10 * 60 * 1000; |
| 134 | + const escalate = setTimeout(() => { |
| 135 | + currentReminder.escalateId = setInterval(() => { |
| 136 | + if (Notification.permission === 'granted') new Notification('Medication NOT acknowledged', { body: msg }); |
| 137 | + }, 60 * 1000); |
| 138 | + }, grace); |
| 139 | + currentReminder.escalateId = escalate; |
| 140 | +} |
| 141 | + |
| 142 | +function acknowledgeReminder(action) { |
| 143 | + if (!currentReminder) return; |
| 144 | + const { client, time } = currentReminder; |
| 145 | + logAction({ clientId: client.id, clientName: client.name, medication: client.medication, action }); |
| 146 | + // clear escalation |
| 147 | + if (currentReminder.escalateId) { |
| 148 | + clearTimeout(currentReminder.escalateId); |
| 149 | + clearInterval(currentReminder.escalateId); |
| 150 | + } |
| 151 | + currentReminder = null; |
| 152 | + modal.classList.add('hidden'); |
| 153 | +} |
| 154 | + |
| 155 | +// wire modal buttons |
| 156 | +takenBtn.addEventListener('click', () => acknowledgeReminder('Taken')); |
| 157 | +missedBtn.addEventListener('click', () => acknowledgeReminder('Missed')); |
| 158 | + |
| 159 | +// form handling |
| 160 | +addTime.addEventListener('click', () => { |
| 161 | + if (!doseTime.value) return; |
| 162 | + const li = document.createElement('li'); |
| 163 | + li.textContent = doseTime.value; |
| 164 | + const btn = document.createElement('button'); btn.textContent = 'Remove'; btn.className = 'muted'; |
| 165 | + btn.addEventListener('click', () => li.remove()); |
| 166 | + li.appendChild(btn); |
| 167 | + timesList.appendChild(li); |
| 168 | + doseTime.value = ''; |
| 169 | +}); |
| 170 | + |
| 171 | +clientForm.addEventListener('submit', (e) => { |
| 172 | + e.preventDefault(); |
| 173 | + const times = Array.from(timesList.children).map(li => li.firstChild.textContent.trim()); |
| 174 | + if (!clientName.value || !medication.value || times.length === 0) { |
| 175 | + alert('Please provide name, medication, and at least one time.'); |
| 176 | + return; |
| 177 | + } |
| 178 | + const id = clientId.value || `c_${Date.now()}`; |
| 179 | + const obj = { id, name: clientName.value.trim(), medication: medication.value.trim(), dosage: dosage.value.trim(), times }; |
| 180 | + if (clientId.value) updateClient(id, obj); else addClient(obj); |
| 181 | + // reset |
| 182 | + clientForm.reset(); timesList.innerHTML = ''; |
| 183 | +}); |
| 184 | + |
| 185 | +document.getElementById('clearForm').addEventListener('click', () => { clientForm.reset(); timesList.innerHTML = ''; clientId.value = ''; }); |
| 186 | + |
| 187 | +// clients list click handlers (edit/delete) |
| 188 | +clientsEl.addEventListener('click', (e) => { |
| 189 | + const edit = e.target.closest('[data-edit]'); |
| 190 | + const del = e.target.closest('[data-delete]'); |
| 191 | + if (edit) { |
| 192 | + const id = edit.getAttribute('data-edit'); |
| 193 | + const c = store.clients.find(x => x.id === id); |
| 194 | + if (!c) return; |
| 195 | + clientId.value = c.id; clientName.value = c.name; medication.value = c.medication; dosage.value = c.dosage; |
| 196 | + timesList.innerHTML = ''; |
| 197 | + c.times.forEach(t => { const li = document.createElement('li'); li.textContent = t; const btn = document.createElement('button'); btn.textContent='Remove'; btn.className='muted'; btn.addEventListener('click',()=>li.remove()); li.appendChild(btn); timesList.appendChild(li); }); |
| 198 | + } |
| 199 | + if (del) { |
| 200 | + const id = del.getAttribute('data-delete'); |
| 201 | + if (confirm('Delete client?')) removeClient(id); |
| 202 | + } |
| 203 | +}); |
| 204 | + |
| 205 | +// initialize |
| 206 | +loadStore(); |
| 207 | +requestNotificationPermission(); |
| 208 | +// schedule existing clients |
| 209 | +store.clients.forEach(c => scheduleClientNotifications(c)); |
| 210 | +render(); |
| 211 | + |
0 commit comments