Skip to content

Commit ffff084

Browse files
Persist section status during appointment (#169)
* Persist section status during appointment * Update button text once sections are reviewed
1 parent 736a441 commit ffff084

File tree

3 files changed

+210
-6
lines changed

3 files changed

+210
-6
lines changed

app/assets/javascript/expandable-sections.js

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ document.addEventListener('DOMContentLoaded', function () {
2222
const button = document.createElement('button')
2323
button.className =
2424
'nhsuk-button nhsuk-button--secondary nhsuk-u-margin-bottom-0 nhsuk-u-margin-top-3 js-save-and-next'
25-
button.textContent = 'Mark as reviewed'
2625
button.type = 'button'
2726
button.setAttribute('data-section-index', index)
2827

28+
// Set initial button text based on current status
29+
updateButtonText(section, button)
30+
2931
buttonContainer.appendChild(button)
3032

3133
// Append HR first, then button container
@@ -54,6 +56,15 @@ document.addEventListener('DOMContentLoaded', function () {
5456
// Update the status for this section
5557
updateSectionStatus(section, newStatus)
5658

59+
// Save status to sessionStorage
60+
const sectionId = section.getAttribute('id')
61+
if (sectionId && window.saveSectionStatus) {
62+
window.saveSectionStatus(sectionId, newStatus)
63+
}
64+
65+
// Update button text based on new status
66+
updateButtonText(section, button)
67+
5768
// Close current section
5869
section.removeAttribute('open')
5970

@@ -68,6 +79,51 @@ document.addEventListener('DOMContentLoaded', function () {
6879

6980
// Initialize progress
7081
updateProgress(sections, completedSections)
82+
83+
// Handle "Complete all and continue" button
84+
const completeAllButtons = document.querySelectorAll(
85+
'.js-complete-all-sections'
86+
)
87+
completeAllButtons.forEach(function (button) {
88+
button.addEventListener('click', function (event) {
89+
// Mark all sections as completed
90+
sections.forEach(function (section, index) {
91+
// Add to completed set
92+
completedSections.add(index)
93+
94+
// Determine current status and set new status
95+
const currentStatus = getCurrentSectionStatus(section)
96+
let newStatus
97+
98+
if (currentStatus === 'Incomplete') {
99+
newStatus = 'Complete'
100+
} else if (currentStatus === 'To review') {
101+
newStatus = 'Reviewed'
102+
} else if (currentStatus === 'Reviewed') {
103+
newStatus = 'Reviewed' // Keep as reviewed
104+
} else {
105+
newStatus = 'Complete' // Default fallback
106+
}
107+
108+
// Update the status for this section
109+
updateSectionStatus(section, newStatus)
110+
111+
// Save status to sessionStorage
112+
const sectionId = section.getAttribute('id')
113+
if (sectionId && window.saveSectionStatus) {
114+
window.saveSectionStatus(sectionId, newStatus)
115+
}
116+
117+
// Close the section
118+
section.removeAttribute('open')
119+
})
120+
121+
// Update progress counter
122+
updateProgress(sections, completedSections)
123+
124+
console.log('All sections marked as complete')
125+
})
126+
})
71127
})
72128

73129
// Function to get current section status
@@ -134,6 +190,17 @@ function getCurrentSectionStatus(section) {
134190
return statusElement ? statusElement.textContent.trim() : 'Incomplete'
135191
}
136192

193+
// Function to update button text based on section status
194+
function updateButtonText(section, button) {
195+
const currentStatus = getCurrentSectionStatus(section)
196+
197+
if (currentStatus === 'Reviewed' || currentStatus === 'Complete') {
198+
button.textContent = 'Next section'
199+
} else {
200+
button.textContent = 'Mark as reviewed'
201+
}
202+
}
203+
137204
// Function to find and open the next incomplete section after the current one
138205
function openNextIncompleteSection(currentIndex, sections, completedSections) {
139206
// First, search from the section after the current one to the end
@@ -288,7 +355,7 @@ function updateSectionStatus(section, statusText) {
288355
console.log('Found status element:', statusElement)
289356
statusElement.textContent = statusText
290357

291-
// Update the tag color class based on status
358+
// Update the tag colour class based on status
292359
// Reset all status classes first
293360
statusElement.classList.remove(
294361
'nhsuk-tag--blue',
@@ -333,3 +400,14 @@ function updateProgress(sections, completedSections) {
333400
`Progress: ${completed}/${totalSections} completed, ${inProgress} in progress`
334401
)
335402
}
403+
404+
// Expose function globally for use by expanded-state-tracker
405+
window.updateSectionButtonText = function (sectionId) {
406+
const section = document.getElementById(sectionId)
407+
if (section) {
408+
const button = section.querySelector('.js-save-and-next')
409+
if (button) {
410+
updateButtonText(section, button)
411+
}
412+
}
413+
}

app/assets/javascript/expanded-state-tracker.js

Lines changed: 127 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
// Generic expanded state tracker
44
// Tracks open/closed state of elements with .js-track-expanded class
5+
// Also tracks section statuses for elements with .js-expandable-section class
56

67
;(function () {
78
'use strict'
89

910
const STORAGE_KEY_PREFIX = 'expanded-sections-'
1011
const PENDING_EXPAND_KEY_PREFIX = 'pending-expand-'
12+
const STATUS_KEY_PREFIX = 'section-statuses-'
1113

1214
function getStorageKey() {
1315
return STORAGE_KEY_PREFIX + window.location.pathname
@@ -17,6 +19,10 @@
1719
return PENDING_EXPAND_KEY_PREFIX + window.location.pathname
1820
}
1921

22+
function getStatusStorageKey() {
23+
return STATUS_KEY_PREFIX + window.location.pathname
24+
}
25+
2026
function saveExpandedState() {
2127
const trackableElements = document.querySelectorAll('.js-track-expanded')
2228
const expandedSections = []
@@ -30,6 +36,120 @@
3036
sessionStorage.setItem(getStorageKey(), JSON.stringify(expandedSections))
3137
}
3238

39+
function saveStatusState(sectionId, statusText) {
40+
const storedStatuses = sessionStorage.getItem(getStatusStorageKey())
41+
let statuses = {}
42+
43+
if (storedStatuses) {
44+
try {
45+
statuses = JSON.parse(storedStatuses)
46+
} catch (e) {
47+
console.warn('Failed to parse stored statuses:', e)
48+
}
49+
}
50+
51+
statuses[sectionId] = statusText
52+
sessionStorage.setItem(getStatusStorageKey(), JSON.stringify(statuses))
53+
}
54+
55+
function restoreStatusState() {
56+
const storedStatuses = sessionStorage.getItem(getStatusStorageKey())
57+
if (storedStatuses) {
58+
try {
59+
const statuses = JSON.parse(storedStatuses)
60+
Object.keys(statuses).forEach((sectionId) => {
61+
const section = document.getElementById(sectionId)
62+
if (section && section.classList.contains('js-expandable-section')) {
63+
const statusText = statuses[sectionId]
64+
updateSectionStatusDisplay(section, statusText)
65+
}
66+
})
67+
} catch (e) {
68+
console.warn('Failed to restore status state:', e)
69+
}
70+
}
71+
}
72+
73+
function updateSectionStatusDisplay(section, statusText) {
74+
const sectionId = section.getAttribute('id')
75+
let statusElement = null
76+
77+
// Use the same strategies to find the status element
78+
const parent = section.parentElement
79+
if (parent) {
80+
statusElement = parent.querySelector('.app-details__status .nhsuk-tag')
81+
}
82+
83+
if (!statusElement && sectionId) {
84+
statusElement =
85+
document.querySelector(`[data-section="${sectionId}"] .nhsuk-tag`) ||
86+
document.querySelector(`#${sectionId}-status .nhsuk-tag`)
87+
}
88+
89+
if (!statusElement) {
90+
let currentElement = section.previousElementSibling
91+
while (currentElement && !statusElement) {
92+
statusElement = currentElement.querySelector(
93+
'.app-details__status .nhsuk-tag'
94+
)
95+
96+
if (!statusElement) {
97+
currentElement = currentElement.previousElementSibling
98+
}
99+
}
100+
}
101+
102+
if (!statusElement) {
103+
let currentElement = section.nextElementSibling
104+
while (currentElement && !statusElement) {
105+
statusElement = currentElement.querySelector(
106+
'.app-details__status .nhsuk-tag'
107+
)
108+
109+
if (!statusElement) {
110+
currentElement = currentElement.nextElementSibling
111+
}
112+
}
113+
}
114+
115+
if (!statusElement) {
116+
const container =
117+
section.closest('.nhsuk-grid-column-two-thirds') ||
118+
section.closest('.nhsuk-grid-row') ||
119+
document.body
120+
121+
const allStatusElements = container.querySelectorAll(
122+
'.app-details__status .nhsuk-tag'
123+
)
124+
125+
const allSections = container.querySelectorAll('.js-expandable-section')
126+
const sectionIndex = Array.from(allSections).indexOf(section)
127+
128+
if (allStatusElements[sectionIndex]) {
129+
statusElement = allStatusElements[sectionIndex]
130+
}
131+
}
132+
133+
if (statusElement) {
134+
statusElement.textContent = statusText
135+
136+
// Update the tag colour class based on status
137+
statusElement.classList.remove(
138+
'nhsuk-tag--blue',
139+
'nhsuk-tag--green',
140+
'nhsuk-tag--yellow'
141+
)
142+
143+
if (statusText === 'Complete' || statusText === 'Reviewed') {
144+
statusElement.classList.add('nhsuk-tag--green')
145+
} else if (statusText === 'Incomplete') {
146+
statusElement.classList.add('nhsuk-tag--blue')
147+
} else if (statusText === 'To review') {
148+
statusElement.classList.add('nhsuk-tag--yellow')
149+
}
150+
}
151+
}
152+
33153
function restoreExpandedState() {
34154
const storedState = sessionStorage.getItem(getStorageKey())
35155
if (storedState) {
@@ -63,6 +183,9 @@
63183
console.warn('Failed to restore pending expand state:', e)
64184
}
65185
}
186+
187+
// Restore section statuses
188+
restoreStatusState()
66189
}
67190

68191
function clearExpandedStateIfNeeded() {
@@ -78,7 +201,8 @@
78201
Object.keys(sessionStorage).forEach((key) => {
79202
if (
80203
key.startsWith(STORAGE_KEY_PREFIX) ||
81-
key.startsWith(PENDING_EXPAND_KEY_PREFIX)
204+
key.startsWith(PENDING_EXPAND_KEY_PREFIX) ||
205+
key.startsWith(STATUS_KEY_PREFIX)
82206
) {
83207
sessionStorage.removeItem(key)
84208
}
@@ -129,6 +253,7 @@
129253
setupEventListeners()
130254
})
131255

132-
// Expose clear function globally for clear data link
256+
// Expose functions globally
133257
window.clearAllExpandedStates = clearAllExpandedStates
258+
window.saveSectionStatus = saveStatusState
134259
})()

app/views/events/record-medical-information.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
<div class="nhsuk-grid-column-two-thirds">
3636
{{ button({
3737
text: "Complete all and continue" if data.event.workflowStatus['record-medical-information'] != 'completed' else "Next section",
38-
classes: "nhsuk-u-margin-bottom-0 nhsuk-button"
38+
classes: "nhsuk-u-margin-bottom-0 nhsuk-button js-complete-all-sections"
3939
}) }}
4040

4141
</div>
@@ -47,7 +47,8 @@
4747

4848
<div class="nhsuk-form-group">
4949
{{ button({
50-
text: "Complete all and continue"
50+
text: "Complete all and continue",
51+
classes: "js-complete-all-sections"
5152
}) }}
5253

5354
{% include "screening-cannot-proceed-link.njk" %}

0 commit comments

Comments
 (0)