diff --git a/R/server.R b/R/server.R index dbb1c8d..9286cbe 100644 --- a/R/server.R +++ b/R/server.R @@ -385,6 +385,48 @@ sd_server <- function( } }) + # Add this new observer for the exit survey modal + observeEvent(input$show_exit_modal, { + showModal(modalDialog( + title = "Before you go...", + sd_question( + type = 'mc_buttons', + id = 'survey_rating', + label = "Rate your survey experience from 1-poor to 5-excellent.", + option = c( + "1" = "1", + "2" = "2", + "3" = "3", + "4" = "4", + "5" = "5" + ) + ), + footer = tagList( + modalButton("Cancel"), + actionButton("submit_rating", "Submit and Exit") + ) + )) + }) + + # Add this observer to handle the rating submission + observeEvent(input$submit_rating, { + # Save the rating + rating <- input$survey_rating + all_data[['exit_survey_rating']] <- rating + changed_fields(c(changed_fields(), 'exit_survey_rating')) + + # Update data immediately + isolate({ + update_data(latest_data(), time_last = TRUE) + }) + + # Close the modal + removeModal() + + # Send a message to close the window + session$sendCustomMessage("closeWindow", list()) + }) + # Ensure final update on session end shiny::onSessionEnded(function() { shiny::isolate({ diff --git a/R/ui.R b/R/ui.R index 635f784..6d6fdc6 100644 --- a/R/ui.R +++ b/R/ui.R @@ -497,21 +497,9 @@ make_next_button_id <- function(page_id) { #' sd_close("Exit Survey", "Are you sure you want to exit? Your progress will not be saved.") #' #' @export -sd_close <- function(label = "Exit Survey", confirm_message = "Are you sure you want to exit the survey?") { +sd_close <- function(label = "Exit Survey") { button_id <- "close-survey-button" - close_script <- sprintf( - "function closeAndConfirm() { - if (confirm('%s')) { - window.close(); - if (!window.closed) { - alert('Please close this tab manually to exit the survey.'); - } - } - }", - confirm_message - ) - shiny::tagList( shiny::div( style = "margin-top: 0.5rem; margin-bottom: 0.5rem;", @@ -520,10 +508,18 @@ sd_close <- function(label = "Exit Survey", confirm_message = "Are you sure you label = label, class = "sd-enter-button", style = "display: block; margin: auto;", - onclick = "closeAndConfirm();" + onclick = "console.log('Exit button clicked'); Shiny.setInputValue('show_exit_modal', true, {priority: 'event'});" ) ), - shiny::tags$script(shiny::HTML(close_script)) + shiny::tags$script(shiny::HTML(" + Shiny.addCustomMessageHandler('closeWindow', function(message) { + console.log('Closing window'); + window.close(); + if (!window.closed) { + alert('Please close this tab manually to exit the survey.'); + } + }); + ")) ) } diff --git a/inst/css/surveydown.css b/inst/css/surveydown.css index 22991ac..8a00a4b 100644 --- a/inst/css/surveydown.css +++ b/inst/css/surveydown.css @@ -291,3 +291,16 @@ p:last-child, div:where(.swal2-container) .swal2-html-container { padding: 0 2em; } + +.modal { + display: block !important; + opacity: 1 !important; +} + +.modal-backdrop { + opacity: 0.5 !important; +} + +.modal-dialog { + z-index: 1050 !important; +}