Skip to content

Commit

Permalink
temporary progress for rating question
Browse files Browse the repository at this point in the history
  • Loading branch information
pingfan-hu committed Oct 4, 2024
1 parent 3ed1b4c commit cc096f1
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 15 deletions.
42 changes: 42 additions & 0 deletions R/server.R
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
26 changes: 11 additions & 15 deletions R/ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -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;",
Expand All @@ -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.');
}
});
"))
)
}

Expand Down
13 changes: 13 additions & 0 deletions inst/css/surveydown.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit cc096f1

Please sign in to comment.