Skip to content

Commit

Permalink
Merge pull request #146 from surveydown-dev/cookie-revert
Browse files Browse the repository at this point in the history
Cookie update
  • Loading branch information
pingfan-hu authored Nov 28, 2024
2 parents a4046c2 + ce6a4fc commit 12c24f5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

# surveydown 0.5.2

- Cookies feature fixed.
- Cookies feature fixed and back online.
- Now upon window closure, the data will be immediately uploaded to db. A backup uploading will trigger upon session ends, which is usually after 5-10 secs upon closing the window.

# surveydown 0.5.1

Expand Down
15 changes: 11 additions & 4 deletions R/server.R
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ sd_server <- function(
# Reactive value to track which fields have changed
changed_fields <- shiny::reactiveVal(names(initial_data))

# Initial data update when session starts
# Update checkpoint 1 - when session starts
shiny::isolate({
update_data()
})
Expand Down Expand Up @@ -497,7 +497,7 @@ sd_server <- function(
# Update tracker of which fields changed
changed_fields(c(changed_fields(), next_ts_id, "current_page"))

# Update data
# Update checkpoint 2 - upon going to the next page
update_data()
} else if (!is.null(next_page_id)) {
shinyWidgets::sendSweetAlert(
Expand Down Expand Up @@ -561,7 +561,7 @@ sd_server <- function(
rating <- input$survey_rating
all_data[['exit_survey_rating']] <- rating
changed_fields(c(changed_fields(), 'exit_survey_rating'))
# Update data immediately
# Update checkpoint 3 - when submitting rating
shiny::isolate({
update_data(time_last = TRUE)
})
Expand All @@ -576,7 +576,14 @@ sd_server <- function(
session$sendCustomMessage("closeWindow", list())
})

# Ensure final update on session end
# Update checkpoint 4 - when window is closed
shiny::observeEvent(input$window_closing, {
shiny::isolate({
update_data(time_last = TRUE)
})
}, ignoreInit = TRUE)

# Update checkpoint 5 - when session is ended (backup)
shiny::onSessionEnded(function() {
shiny::isolate({
update_data(time_last = TRUE)
Expand Down
1 change: 1 addition & 0 deletions R/ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ sd_ui <- function() {
"countdown.js",
"enter_key.js",
"keep_alive.js",
"window_close.js",
"surveydown.css"
),
if (any(theme == "default")) {
Expand Down
10 changes: 10 additions & 0 deletions inst/js/window_close.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
$(document).ready(function() {
window.addEventListener('beforeunload', function(e) {
// Send message to Shiny
Shiny.setInputValue('window_closing', true, {priority: 'event'});

// Small delay to allow data to save
var start = Date.now();
while(Date.now() - start < 200) {}
});
});

0 comments on commit 12c24f5

Please sign in to comment.