Skip to content

Commit

Permalink
code reallocation
Browse files Browse the repository at this point in the history
  • Loading branch information
pingfan-hu committed Dec 2, 2024
1 parent 2fd6111 commit e8ffb3a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
43 changes: 24 additions & 19 deletions R/server.R
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ sd_server <- function(
use_cookies = TRUE
) {

# Initialize local variables ----
# 1. Initialize local variables ----

# Get input, output, and session from the parent environment
parent_env <- parent.frame()
Expand Down Expand Up @@ -173,7 +173,6 @@ sd_server <- function(
current_page_id <- shiny::reactiveVal(start_page)

# Progress bar
load_js_file("update_progress.js")
max_progress <- shiny::reactiveVal(0)
last_answered_question <- shiny::reactiveVal(0)
update_progress_bar <- function(index) {
Expand All @@ -199,7 +198,15 @@ sd_server <- function(
# Initialize translations list (from '_survey/translations.yml' file)
translations <- get_translations()$translations

# show_if conditions ----
# Keep-alive observer - this will be triggered every 60 seconds
shiny::observeEvent(input$keepAlive, {
cat("Session keep-alive at", format(Sys.time(), "%m/%d/%Y %H:%M:%S"), "\n")
})

# Create admin page if admin_page is TRUE
if (isTRUE(config$admin_page)) admin_enable(input, output, session, db)

# 2. show_if conditions ----

# Reactive to store visibility status of all questions
question_visibility <- shiny::reactiveVal(
Expand All @@ -222,7 +229,7 @@ sd_server <- function(
question_visibility(current_visibility)
})

# Update data ----
# 3. Update data ----

update_data <- function(time_last = FALSE) {
data_list <- latest_data()
Expand Down Expand Up @@ -320,17 +327,7 @@ sd_server <- function(
changed_fields(setdiff(changed_fields(), fields))
}

# Initial settings ----

# Keep-alive observer - this will be triggered every 60 seconds
shiny::observeEvent(input$keepAlive, {
cat("Session keep-alive at", format(Sys.time(), "%m/%d/%Y %H:%M:%S"), "\n")
})

# Create admin page if admin_page is TRUE
if (isTRUE(config$admin_page)) admin_enable(input, output, session, db)

# Data tracking ----
# 4. Data tracking ----

# First check and initialize table if needed
if (!ignore_mode) {
Expand Down Expand Up @@ -376,7 +373,7 @@ sd_server <- function(
update_data()
})

# Main question observers ----
# 5. Main question observers ----

lapply(seq_along(question_ids), function(index) {
local({
Expand Down Expand Up @@ -435,7 +432,7 @@ sd_server <- function(
})
})

# Page rendering ----
# 6. Page rendering ----

# Create reactive values for the start page ID
get_current_page <- shiny::reactive({
Expand All @@ -461,7 +458,7 @@ sd_server <- function(
)
})

# Page navigation ----
# 7. Page navigation ----

check_required <- function(page) {
required_questions <- page$required_questions
Expand Down Expand Up @@ -520,7 +517,8 @@ sd_server <- function(
}
})

# Survey rating ----
# 8. Survey rating and exit ----

# Observer for the exit survey modal
shiny::observeEvent(input$show_exit_modal, {
if (rate_survey) {
Expand Down Expand Up @@ -579,6 +577,13 @@ sd_server <- function(
shiny::removeModal()
session$sendCustomMessage("closeWindow", list())
})

# Update checkpoint 5 - when session ends
shiny::onSessionEnded(function() {
shiny::isolate({
update_data(time_last = TRUE)
})
})
}

#' Define skip conditions for survey pages
Expand Down
1 change: 1 addition & 0 deletions R/ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ sd_ui <- function() {
"countdown.js",
"enter_key.js",
"keep_alive.js",
"update_progress.js",
"surveydown.css"
),
if (any(theme == "default")) {
Expand Down

0 comments on commit e8ffb3a

Please sign in to comment.