Skip to content

Commit

Permalink
sweetalert
Browse files Browse the repository at this point in the history
  • Loading branch information
pingfan-hu committed Oct 4, 2024
1 parent 64e6140 commit b3a9c79
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 28 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export(sd_update)
export(sd_version)
import(rstudioapi)
import(shiny)
import(shinyWidgets)
importFrom(digest,digest)
importFrom(rsconnect,deployApp)
importFrom(shiny,HTML)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- `sd_server()` now has a new parameter called `auto_scroll`. It's default to `TRUE`, which enables auto scrolling that tracks the user's input, can be turned off by changing to `FALSE`. Thanks to the contribution from [Zain Hoda](https://github.com/zainhoda1).
- `sd_question()` now has the `"matrix"` type. The [documentation page](https://surveydown.org/question-types#matrix) is updated.
- Asterisk, as an indication of required questions, is now moved to the top right corner of question containers.
- Replaced the default shiny alert with `sweetalert`.

# surveydown 0.3.2

Expand Down
66 changes: 38 additions & 28 deletions R/server.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#' @param auto_scroll Logical. Whether to enable auto-scrolling to the next question after answering. Defaults to TRUE.
#'
#' @import shiny
#' @import shinyWidgets
#' @importFrom stats setNames
#' @importFrom shiny reactiveValuesToList observeEvent renderText
#'
Expand All @@ -33,6 +34,7 @@
#' \item Performs database operations or saves to a local CSV file in preview mode.
#' \item Sets up admin functionality if enabled in the configuration.
#' \item Controls auto-scrolling behavior based on the `auto_scroll` parameter.
#' \item Uses sweetalert for warning messages when required questions are not answered.
#' }
#'
#' @section Progress Bar:
Expand All @@ -50,12 +52,18 @@
#' after the current question is answered. This behavior can be disabled by setting
#' `auto_scroll` to FALSE.
#'
#' @section Warning Messages:
#' The function uses sweetalert to display warning messages when users attempt to proceed
#' without answering all required questions. This provides a more user-friendly experience
#' compared to standard Shiny notifications.
#'
#' @return
#' This function does not return a value; it sets up the server-side logic for the Shiny application.
#'
#' @examples
#' \dontrun{
#' library(surveydown)
#' library(shinyWidgets)
#' db <- sd_database()
#'
#' shinyApp(
Expand Down Expand Up @@ -308,10 +316,10 @@ sd_server <- function(
# Page navigation ----

check_required <- function(page) {
all(vapply(page$required_questions, function(q) {
is_visible <- is_question_visible(q)
!is_visible || check_answer(q, input)
}, logical(1)))
all(vapply(page$required_questions, function(q) {
is_visible <- is_question_visible(q)
!is_visible || check_answer(q, input)
}, logical(1)))
}

is_question_visible <- function(q) {
Expand All @@ -321,31 +329,33 @@ sd_server <- function(

# Determine which page is next, then update current_page_id() to it
observe({
lapply(pages, function(page) {
observeEvent(input[[page$next_button_id]], {
shiny::isolate({
current_page_id <- page$id
next_page_id <- get_default_next_page(page, page_ids, page_id_to_index)
next_page_id <- handle_skip_logic(input, skip_if, current_page_id, next_page_id)
if (!is.null(next_page_id) && check_required(page)) {
# Set the current page as the next page
current_page_id(next_page_id)

# Update the page time stamp
next_ts_id <- page_ts_ids[which(page_ids == next_page_id)]
all_data[[next_ts_id]] <- get_utc_timestamp()

# Update tracker of which fields changed
changed_fields(c(changed_fields(), next_ts_id))
} else if (!is.null(next_page_id)) {
shiny::showNotification(
"Please answer all required questions before proceeding.",
type = "error"
)
}
})
})
lapply(pages, function(page) {
observeEvent(input[[page$next_button_id]], {
shiny::isolate({
current_page_id <- page$id
next_page_id <- get_default_next_page(page, page_ids, page_id_to_index)
next_page_id <- handle_skip_logic(input, skip_if, current_page_id, next_page_id)
if (!is.null(next_page_id) && check_required(page)) {
# Set the current page as the next page
current_page_id(next_page_id)

# Update the page time stamp
next_ts_id <- page_ts_ids[which(page_ids == next_page_id)]
all_data[[next_ts_id]] <- get_utc_timestamp()

# Update tracker of which fields changed
changed_fields(c(changed_fields(), next_ts_id))
} else if (!is.null(next_page_id)) {
shinyWidgets::sendSweetAlert(
session = session,
title = "Warning",
text = "Please answer all required questions before proceeding.",
type = "warning"
)
}
})
})
})
})

# Observer to max out the progress bar when we reach the last page
Expand Down
5 changes: 5 additions & 0 deletions inst/css/surveydown.css
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,8 @@ p:last-child,
font-weight: normal;
padding-left: 15px; /* Add some left padding */
}

/* SweetAlert custom styling */
.swal2-popup {
font-size: 1rem !important;
}
9 changes: 9 additions & 0 deletions man/sd_server.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b3a9c79

Please sign in to comment.