|
| 1 | +function scoreTask(task) { |
| 2 | + |
| 3 | + const taskTypeScores = { |
| 4 | + PrescriptionRequest: 3, |
| 5 | + ReviewResults: 3, |
| 6 | + PatientCommunication: 1, |
| 7 | + SickNotes: 1, |
| 8 | + ReferralLetters: 1, |
| 9 | + MedicalReports: 1, |
| 10 | + }; |
| 11 | + |
| 12 | + const baseScore = taskTypeScores[task.task_type] || 0; |
| 13 | + const timeDays = Number(task.time_in_pipeline) || 0; |
| 14 | + const timingPenalty = 0.1 * (timeDays * timeDays); |
| 15 | + |
| 16 | + return baseScore + timingPenalty; |
| 17 | + } |
| 18 | + |
| 19 | + const taskForm = document.querySelector("#taskForm"); |
| 20 | + const taskContainer = document.querySelector("#tasks"); |
| 21 | + const modalBackdrop = document.querySelector(".modal-backdrop"); |
| 22 | + const editForm = document.querySelector("#editForm"); |
| 23 | + |
| 24 | + let tasks = JSON.parse(localStorage.getItem("tasks")) || []; |
| 25 | + |
| 26 | + // 1) RENDER TASKS |
| 27 | + function renderTasks() { |
| 28 | + // Clear the container |
| 29 | + taskContainer.innerHTML = ""; |
| 30 | + |
| 31 | + // Sort tasks by severity descending |
| 32 | + // We recalculate severity each time because |
| 33 | + // it might have changed if user edits |
| 34 | + tasks.forEach((task) => { |
| 35 | + task.severity = scoreTask(task); |
| 36 | + }); |
| 37 | + tasks.sort((a, b) => b.severity - a.severity); |
| 38 | + |
| 39 | + tasks.forEach((task, i) => { |
| 40 | + const card = document.createElement("div"); |
| 41 | + card.classList.add("card"); |
| 42 | + const heading = document.createElement("h4"); |
| 43 | + heading.textContent = `${task.task_type} (score: ${task.severity.toFixed(2)})`; |
| 44 | + |
| 45 | + // Example: show time in pipeline |
| 46 | + const pTime = document.createElement("p"); |
| 47 | + pTime.textContent = `Time in pipeline: ${task.time_in_pipeline} days`; |
| 48 | + |
| 49 | + // “Delete” button |
| 50 | + const delBut = document.createElement("button"); |
| 51 | + delBut.innerText = "Delete"; |
| 52 | + delBut.addEventListener("click", function () { |
| 53 | + tasks.splice(i, 1); |
| 54 | + saveTasks(); |
| 55 | + renderTasks(); |
| 56 | + }); |
| 57 | + |
| 58 | + // “Edit” button |
| 59 | + const editBut = document.createElement("button"); |
| 60 | + editBut.innerText = "Edit"; |
| 61 | + editBut.addEventListener("click", function () { |
| 62 | + openEditForm(task, i); |
| 63 | + }); |
| 64 | + |
| 65 | + card.appendChild(heading); |
| 66 | + card.appendChild(pTime); |
| 67 | + card.appendChild(delBut); |
| 68 | + card.appendChild(editBut); |
| 69 | + taskContainer.appendChild(card); |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + // 2) ADD A NEW TASK |
| 74 | + taskForm.addEventListener("submit", function (event) { |
| 75 | + event.preventDefault(); |
| 76 | + const newTask = { |
| 77 | + task_type: taskForm.task_type.value, |
| 78 | + time_in_pipeline: taskForm.time_in_pipeline.value, |
| 79 | + // If you had more fields, you'd gather them here |
| 80 | + }; |
| 81 | + tasks.push(newTask); |
| 82 | + saveTasks(); |
| 83 | + renderTasks(); |
| 84 | + }); |
| 85 | + |
| 86 | + // 3) EDIT TASK |
| 87 | + function openEditForm(task, index) { |
| 88 | + modalBackdrop.classList.remove("hidden"); |
| 89 | + |
| 90 | + // Fill in form with existing values |
| 91 | + editForm.task_type.value = task.task_type; |
| 92 | + editForm.time_in_pipeline.value = task.time_in_pipeline; |
| 93 | + |
| 94 | + // Swap out the old “saveButton” event so it doesn’t stack |
| 95 | + const saveButton = document.querySelector("#saveButton"); |
| 96 | + // In case a prior open edit used the same button |
| 97 | + saveButton.onclick = function (e) { |
| 98 | + e.preventDefault(); |
| 99 | + // Update the existing task |
| 100 | + tasks[index].task_type = editForm.task_type.value; |
| 101 | + tasks[index].time_in_pipeline = editForm.time_in_pipeline.value; |
| 102 | + |
| 103 | + saveTasks(); |
| 104 | + renderTasks(); |
| 105 | + closeModal(); |
| 106 | + }; |
| 107 | + } |
| 108 | + |
| 109 | + // 4) CANCEL EDIT |
| 110 | + document.querySelector("#cancelButton").addEventListener("click", closeModal); |
| 111 | + function closeModal() { |
| 112 | + modalBackdrop.classList.add("hidden"); |
| 113 | + } |
| 114 | + |
| 115 | + // 5) SAVE to localStorage |
| 116 | + function saveTasks() { |
| 117 | + localStorage.setItem("tasks", JSON.stringify(tasks)); |
| 118 | + } |
| 119 | + |
| 120 | + // 6) INITIAL RENDER |
| 121 | + renderTasks(); |
0 commit comments