Skip to content

Commit

Permalink
changed
Browse files Browse the repository at this point in the history
  • Loading branch information
jhelvy committed Oct 4, 2024
1 parent ee6177c commit 7c270a3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
4 changes: 2 additions & 2 deletions R/config.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ get_html_content <- function(survey_file) {
}

extract_html_pages <- function(
html_content, required_questions, all_questions_required, show_if
html_content, required_questions, all_questions_required, show_if
) {
pages <- html_content |>
rvest::html_elements(".sd-page") |>
Expand Down Expand Up @@ -108,7 +108,7 @@ extract_html_pages <- function(
if (!is.null(show_if)) {
if (question_id %in% show_if$targets) {
current_style <- xml2::xml_attr(container, "style")
new_style <- gsub("display:\\s*block;", "display: none;", current_style, ignore.case = TRUE)
new_style <- paste(current_style, "display: none;", sep = " ")
xml2::xml_attr(container, "style") <- new_style
}
}
Expand Down
28 changes: 19 additions & 9 deletions R/server.R
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ sd_server <- function(

# Set up show_if conditions ----

# Track if show_if conditions should be re-evaluated
show_if_trigger <- shiny::reactiveVal(0)

# Reactive values storing status of show_if conditions
show_if_results <- set_show_if_conditions(show_if)

Expand All @@ -149,6 +152,9 @@ sd_server <- function(

# Observer to hide/show based on show_if condition results
shiny::observe({

# Run if the trigger or condition results change
show_if_trigger()
results <- show_if_results()

# Update question visibility based on show_if results
Expand All @@ -161,9 +167,9 @@ sd_server <- function(
# Show or hide question
for (target in names(results)) {
if (results[[target]]) {
shinyjs::show(target)
shinyjs::show(paste0('container-', target))
} else {
shinyjs::hide(target)
shinyjs::hide(paste0('container-', target))
}
}
})
Expand Down Expand Up @@ -272,26 +278,30 @@ sd_server <- function(
local_ts_id <- question_ts_ids[index]

observeEvent(input[[local_id]], {
# Tag event time
timestamp <- get_utc_timestamp()

# Update question value
formatted_value <- format_question_value(input[[local_id]])
all_data[[local_id]] <- formatted_value

# Update tracker of which fields changed
changed_fields(c(changed_fields(), local_id))
# Trigger show_if evaluation
show_if_trigger(show_if_trigger() + 1)

# Update timestamp and progress if interacted
changed <- local_id
if (!is.null(input[[paste0(local_id, "_interacted")]])) {
all_data[[local_ts_id]] <- get_utc_timestamp()
changed_fields(c(changed_fields(), local_ts_id))
all_data[[local_ts_id]] <- timestamp
changed <- c(changed, local_ts_id)
update_progress_bar(index)
}

# Update tracker of which fields changed
changed_fields(c(changed_fields(), changed))

# Make value accessible in the UI
output[[paste0(local_id, "_value")]] <- renderText({ formatted_value })

# Trigger show_if evaluation
show_if_results()

# Update data after a short delay
shiny::invalidateLater(100)
}, ignoreNULL = FALSE, ignoreInit = TRUE)
Expand Down

0 comments on commit 7c270a3

Please sign in to comment.