diff --git a/public/js/main.js b/public/js/main.js index beb280d..bc0df10 100644 --- a/public/js/main.js +++ b/public/js/main.js @@ -14,7 +14,8 @@ import { handleViewNotesClick, renderArchiveView, renderTimeOffHistoryView, - updatePendingRequestsList + updatePendingRequestsList, + renderEditTimeOffModal } from './ui.js'; // --- STATE MANAGEMENT --- @@ -214,17 +215,78 @@ function handleAdminDashboardClick(e) { if (target.classList.contains('delete-note-btn')) { if (confirm('Delete this note?')) { apiCall(`/admin/notes/${noteId}`, 'DELETE').then(res => { if (res.success) { showMessage('Note deleted.', 'success'); handleViewNotesClick(); }});}} } +// --- NEW: Handler for editing a time-off request --- +async function handleEditTimeOffSubmit(e) { + e.preventDefault(); + const id = e.target.elements['edit-request-id'].value; + const startDate = e.target.elements['edit-start-date'].value; + const endDate = e.target.elements['edit-end-date'].value; + const reason = e.target.elements['edit-reason'].value; + + if (new Date(endDate) < new Date(startDate)) { + return showMessage('End date cannot be before start date.', 'error'); + } + + const res = await apiCall(`/user/time-off-requests/${id}`, 'PUT', { startDate, endDate, reason }); + if (res.success) { + showMessage(res.data.message, 'success'); + document.getElementById('modal-container').innerHTML = ''; + renderEmployeeDashboard(); // Refresh the dashboard to show changes + } +} + // --- LISTENER ATTACHMENT FUNCTIONS --- export function attachAuthFormListener() { const form = document.getElementById('auth-form'); if (form) form.addEventListener('submit', handleAuthSubmit); } +// --- UPDATED: Employee dashboard listeners now use event delegation --- export function attachEmployeeDashboardListeners() { - document.getElementById('punch-btn')?.addEventListener('click', handlePunch); - document.getElementById('change-password-btn')?.addEventListener('click', () => renderChangePasswordModal(handleChangePassword)); - document.getElementById('time-off-form')?.addEventListener('submit', handleTimeOffRequest); - document.getElementById('view-request-history-btn')?.addEventListener('click', handleViewRequestHistoryClick); + const dashboard = document.getElementById('employee-dashboard'); + if (!dashboard) return; + + // Use one listener for the entire dashboard + dashboard.addEventListener('click', async (e) => { + const target = e.target; + + if (target.id === 'punch-btn') { + handlePunch(); + } + if (target.id === 'change-password-btn') { + renderChangePasswordModal(handleChangePassword); + } + if (target.id === 'view-request-history-btn') { + handleViewRequestHistoryClick(); + } + if (target.classList.contains('delete-request-btn')) { + const id = target.dataset.id; + if (confirm('Are you sure you want to delete this time off request?')) { + const res = await apiCall(`/user/time-off-requests/${id}`, 'DELETE'); + if (res.success) { + showMessage(res.data.message, 'success'); + renderEmployeeDashboard(); + } + } + } + if (target.classList.contains('edit-request-btn')) { + const id = target.dataset.id; + // We need to get the full request data to pre-fill the form + const res = await apiCall('/user/time-off-requests/history'); + if (res.success) { + const requestToEdit = res.data.find(r => r.id == id); + if (requestToEdit) { + renderEditTimeOffModal(requestToEdit, handleEditTimeOffSubmit); + } + } + } + }); + + // Handle form submissions separately + const timeOffForm = document.getElementById('time-off-form'); + if (timeOffForm) { + timeOffForm.addEventListener('submit', handleTimeOffRequest); + } } export function attachAdminDashboardListeners() { diff --git a/public/js/ui.js b/public/js/ui.js index be49ae4..85a2014 100644 --- a/public/js/ui.js +++ b/public/js/ui.js @@ -38,7 +38,6 @@ export function showView(viewName) { // --- MASTER UI UPDATE FUNCTION --- export function updateUI() { - // This function was empty, it has been corrected. try { const storedUser = localStorage.getItem('user'); const authToken = localStorage.getItem('authToken'); @@ -126,7 +125,35 @@ export async function renderEmployeeDashboard() {
-| Dates | Reason | Status |
|---|---|---|
| ${utils.formatDate(r.start_date)} - ${utils.formatDate(r.end_date)} | ${r.reason || ''} | ${r.status} |
| No upcoming or pending requests. | ||
| Dates | +Reason | +Status | +Actions | +
|---|---|---|---|
| ${utils.formatDate(r.start_date)} - ${utils.formatDate(r.end_date)} | +${r.reason || ''} | +${r.status} | +
+ ${r.status === 'pending' ? `
+
+
+
+
+ ` : ''}
+ |
+
| No upcoming or pending requests. | |||
Could not load notes.
'; } } -// This function ONLY updates the pending requests table. + export function updatePendingRequestsList(requests) { const tableBody = document.querySelector('#tab-content-overview table tbody'); - if (!tableBody) return; // Exit if the table isn't on the page + if (!tableBody) return; if (requests.length === 0) { tableBody.innerHTML = '