@@ -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
138205function 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+ }
0 commit comments