Spaces:
Running
Running
Download script.js from MegaTacos/school-bucks-tracker: direct link, hf CLI and curl.
- Browser
- Download file 9.77 kB
-
https://huggingface.co/spaces/MegaTacos/school-bucks-tracker/resolve/main/script.js
- Command line
-
hf download hf://spaces/MegaTacos/school-bucks-tracker/script.js
-
curl -L -o script.js https://huggingface.co/spaces/MegaTacos/school-bucks-tracker/resolve/main/script.js
9.77 kB
| document.addEventListener('DOMContentLoaded', function() { | |
| // DOM Elements | |
| const fullDayBtn = document.getElementById('fullDayBtn'); | |
| const halfDayBtn = document.getElementById('halfDayBtn'); | |
| const totalDaysDisplay = document.getElementById('totalDays'); | |
| const totalEarningsDisplay = document.getElementById('totalEarnings'); | |
| const historyList = document.getElementById('historyList'); | |
| const goal1 = document.getElementById('goal1'); | |
| const goal2 = document.getElementById('goal2'); | |
| const goal3 = document.getElementById('goal3'); | |
| // Initialize data from localStorage or set defaults | |
| let attendanceData = JSON.parse(localStorage.getItem('attendanceData')) || { | |
| totalMoney: 0, | |
| history: [] | |
| }; | |
| // Constants | |
| const FULL_DAY_RATE = 2.00; | |
| const HALF_DAY_RATE = 1.00; | |
| // Update the UI with current data | |
| updateUI(); | |
| // Event Listeners | |
| // Add empty dishwasher button functionality | |
| const emptyDishwasherBtn = document.createElement('button'); | |
| emptyDishwasherBtn.id = 'emptyDishwasherBtn'; | |
| emptyDishwasherBtn.className = 'w-full py-3 px-4 bg-secondary-500 hover:bg-secondary-600 text-black transition flex items-center justify-center mb-4'; | |
| emptyDishwasherBtn.innerHTML = '<i data-feather="check-circle" class="mr-2"></i> Empty Dishwasher (+$1.00)'; | |
| document.querySelector('.bg-white.p-8').insertBefore(emptyDishwasherBtn, document.querySelector('.bg-white.p-8').firstChild); | |
| fullDayBtn.addEventListener('click', (e) => { | |
| e.preventDefault(); | |
| addAttendance(true); | |
| }); | |
| emptyDishwasherBtn.addEventListener('click', (e) => { | |
| e.preventDefault(); | |
| const questsElement = document.querySelector('custom-quests'); | |
| if (questsElement) { | |
| questsElement.updateQuestProgress('emptyDishwasher'); | |
| } | |
| }); | |
| // SMS Notification Functions | |
| async function sendSMS(phoneNumber, message) { | |
| // In a real implementation, you would call your backend API here | |
| // which would then use Twilio to send the SMS | |
| console.log(`Would send SMS to ${phoneNumber}: ${message}`); | |
| // This is just a simulation - in production you would: | |
| // 1. Create a backend endpoint (e.g., /api/send-sms) | |
| // 2. Call it with fetch(): | |
| /* | |
| try { | |
| const response = await fetch('/api/send-sms', { | |
| method: 'POST', | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| }, | |
| body: JSON.stringify({ phoneNumber, message }) | |
| }); | |
| const data = await response.json(); | |
| console.log('SMS sent:', data); | |
| } catch (error) { | |
| console.error('Error sending SMS:', error); | |
| } | |
| */ | |
| } | |
| function checkDailyReminder() { | |
| const settings = JSON.parse(localStorage.getItem('notificationSettings')) || {}; | |
| if (!settings.dailyReminder || !settings.phoneNumber) return; | |
| // Check current time | |
| const now = new Date(); | |
| const currentHour = now.getHours(); | |
| const currentMinute = now.getMinutes(); | |
| // If it's 14:00 (2PM) and user hasn't logged attendance today | |
| if (currentHour === 14 && currentMinute === 0) { | |
| const today = now.toLocaleDateString(); | |
| const attendanceData = JSON.parse(localStorage.getItem('attendanceData')) || { history: [] }; | |
| const hasLoggedToday = attendanceData.history.some(entry => { | |
| return entry.date === today; | |
| }); | |
| if (!hasLoggedToday) { | |
| const message = `Prestige School Rewards: Don't forget to log your attendance today!`; | |
| sendSMS(settings.phoneNumber, message); | |
| } | |
| } | |
| } | |
| // Check time every minute | |
| setInterval(checkDailyReminder, 60000); | |
| halfDayBtn.addEventListener('click', (e) => { | |
| e.preventDefault(); | |
| addAttendance(false); | |
| }); | |
| // Functions | |
| // Listen for UI update events from quests component | |
| document.addEventListener('updateUI', updateUI); | |
| function addAttendance(isFullDay) { | |
| // Update quest progress | |
| const questsElement = document.querySelector('custom-quests'); | |
| if (questsElement) { | |
| questsElement.updateQuestProgress(isFullDay ? 'attendFullDay' : 'attendHalfDay'); | |
| // Check for perfect week progress | |
| if (isFullDay) { | |
| questsElement.updateQuestProgress('perfectWeek'); | |
| } | |
| } | |
| const now = new Date(); | |
| const dateString = now.toLocaleDateString('en-US', { | |
| weekday: 'short', | |
| month: 'short', | |
| day: 'numeric' | |
| }); | |
| const timeString = now.toLocaleTimeString('en-US', { | |
| hour: '2-digit', | |
| minute: '2-digit' | |
| }); | |
| // Add to history | |
| const amount = isFullDay ? FULL_DAY_RATE : HALF_DAY_RATE; | |
| const entry = { | |
| date: dateString, | |
| time: timeString, | |
| amount: amount, | |
| isFullDay: isFullDay, | |
| timestamp: now.getTime() | |
| }; | |
| attendanceData.history.unshift(entry); | |
| attendanceData.totalMoney += amount; | |
| // Save to localStorage | |
| localStorage.setItem('attendanceData', JSON.stringify(attendanceData)); | |
| // Update UI | |
| updateUI(); | |
| } | |
| function updateUI() { | |
| // Update quests display if it exists | |
| const questsElement = document.querySelector('custom-quests'); | |
| if (questsElement) { | |
| questsElement.renderQuests(); | |
| } | |
| // Update total days and earnings | |
| totalEarningsDisplay.textContent = `${attendanceData.totalMoney.toFixed(2)}`; | |
| // Update history list | |
| historyList.innerHTML = ''; | |
| if (attendanceData.history.length === 0) { | |
| historyList.innerHTML = '<li class="py-2 text-gray-600 italic">No payments recorded yet</li>'; | |
| } else { | |
| attendanceData.history.forEach(entry => { | |
| const li = document.createElement('li'); | |
| li.className = 'history-item group'; | |
| li.innerHTML = ` | |
| <div class="flex justify-between items-center"> | |
| <div class="flex items-center"> | |
| <div class="w-8 h-8 rounded-full flex items-center justify-center mr-3 ${entry.isFullDay ? 'bg-primary-500/10' : 'bg-yellow-500/10'}"> | |
| <i data-feather="${entry.isFullDay ? 'check' : 'clock'}" class="${entry.isFullDay ? 'text-primary-500' : 'text-yellow-500'} w-4 h-4"></i> | |
| </div> | |
| <div> | |
| <span class="font-medium">${entry.date}</span> | |
| <span class="text-gray-500 text-sm ml-2">${entry.time}</span> | |
| </div> | |
| </div> | |
| <div class="flex items-center"> | |
| <span class="font-medium ${entry.isFullDay ? 'text-primary-500' : 'text-yellow-500'} mr-3"> | |
| +${entry.amount.toFixed(2)} | |
| </span> | |
| <button class="delete-btn opacity-0 group-hover:opacity-100 transition-opacity duration-200 text-red-500 hover:text-red-700" data-timestamp="${entry.timestamp}"> | |
| <i data-feather="trash-2" class="w-4 h-4"></i> | |
| </button> | |
| </div> | |
| </div> | |
| `; | |
| historyList.appendChild(li); | |
| // Add event listener to delete button | |
| const deleteBtn = li.querySelector('.delete-btn'); | |
| deleteBtn.addEventListener('click', (e) => { | |
| e.preventDefault(); | |
| e.stopPropagation(); | |
| deleteHistoryEntry(entry.timestamp); | |
| }); | |
| }); | |
| } | |
| // Update goal progress | |
| updateGoalProgress(goal1, 10); | |
| updateGoalProgress(goal2, 20); | |
| updateGoalProgress(goal3, 40); | |
| } | |
| function updateGoalProgress(progressBar, goalDays) { | |
| const progress = Math.min((attendanceData.totalDays / goalDays) * 100, 100); | |
| progressBar.style.width = `${progress}%`; | |
| } | |
| function deleteHistoryEntry(timestamp) { | |
| if (!confirm('Remove this attendance record? This will adjust your total days and earnings.')) return; | |
| // Find and remove the entry | |
| const entryIndex = attendanceData.history.findIndex(entry => entry.timestamp === timestamp); | |
| if (entryIndex === -1) return; | |
| // Subtract days from total | |
| const removedEntry = attendanceData.history[entryIndex]; | |
| attendanceData.totalMoney -= removedEntry.amount; | |
| // Remove from history | |
| attendanceData.history.splice(entryIndex, 1); | |
| // Save and update | |
| localStorage.setItem('attendanceData', JSON.stringify(attendanceData)); | |
| updateUI(); | |
| } | |
| // Add a reset button functionality (hidden by default, can be enabled if needed) | |
| function addResetButton() { | |
| const resetBtn = document.createElement('button'); | |
| resetBtn.textContent = 'Reset All Data'; | |
| resetBtn.className = 'mt-4 text-sm text-red-500 hover:text-red-700'; | |
| resetBtn.addEventListener('click', () => { | |
| if (confirm('Are you sure you want to reset all data? This cannot be undone.')) { | |
| localStorage.removeItem('attendanceData'); | |
| attendanceData = { totalMoney: 0, history: [] }; | |
| updateUI(); | |
| } | |
| }); | |
| const resetBtnContainer = document.createElement('div'); | |
| resetBtnContainer.className = 'text-center mt-8'; | |
| resetBtnContainer.appendChild(resetBtn); | |
| document.querySelector('main').appendChild(resetBtnContainer); | |
| } | |
| // Check for daily reminder and send SMS if needed | |
| checkDailyReminder(); | |
| // Uncomment to enable reset button (for debugging) | |
| // addResetButton(); | |
| }); |