Skip to content

Commit

Permalink
Clean timezone module (#111)
Browse files Browse the repository at this point in the history
* Clean timezone UI.

Closes #99.
Closes #105.

* Deploy with app.prod = TRUE.

* Use tzdb to try to standardize results across machines.
  • Loading branch information
jonthegeek authored Nov 12, 2023
1 parent fddd38e commit e85b12c
Show file tree
Hide file tree
Showing 16 changed files with 720 additions and 24 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@
^data-raw$
^app\.R$
^rsconnect$
_\.new\.png$
_disable_autoload\.R$
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ bookclubs4ds-service-account.json
rsconnect
.secret
.Renviron
# {shinytest2}: Ignore new debug snapshots for `$expect_values()`
*_.new.png
8 changes: 5 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Imports:
golem (>= 0.3.1),
googledrive,
googlesheets4,
htmltools,
jsonlite,
lubridate,
rhandsontable,
Expand All @@ -28,15 +29,16 @@ Imports:
shinyslack (>= 0.0.0.9008),
stringr,
tibble,
tidyr
tidyr,
tzdb
Suggests:
pkgload,
roxygen2,
shinytest2,
testthat (>= 3.0.0)
Remotes:
r4ds/bookclubdata,
r4ds/shinyslack
Config/testthat/edition: 3
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.2.3
Config/testthat/edition: 3
10 changes: 5 additions & 5 deletions R/app_server.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#' The application server-side
#'
#' @param input,output,session Internal parameters for {shiny}.
#' DO NOT REMOVE.
#' @param input,output,session Internal parameters for shiny.
#' @keywords internal
.app_server <- function(input, output, session) {
slack_user_info <- .user_server()
timezone <- .timezone_server()

approved_books <- bookclubdata::approved_books(refresh = TRUE)
signups <- reactive({
Expand All @@ -20,14 +20,14 @@
# works and other attempts didn't.
observe({
req(
input[[NS("timezone", "selected_zone")]],
timezone(),
approved_books,
signups(),
slack_user_info()[["user_id"]]
)
output[[NS("calendar", "availability")]] <- .calendar_selector(
slack_user_info()[["user_id"]],
input[[NS("timezone", "selected_zone")]],
timezone(),
signups()
)
})
Expand All @@ -38,7 +38,7 @@
slack_user_info()[["display_name"]],
slack_user_info()[["user_id"]],
input$book_name,
input[[NS("timezone", "selected_zone")]],
timezone(),
input[[NS("calendar", "availability")]]
),
priority = 1000
Expand Down
38 changes: 29 additions & 9 deletions R/timezone.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' UI to Select a Timezone
#' UI to select a timezone
#'
#' @inheritParams .shared-parameters
#'
Expand All @@ -8,42 +8,62 @@
.timezone_ui <- function(id = "timezone") {
return(
tagList(
# Add HTML to the page to store the timezone.
# Add an invisible input to the page to store the JS-detected timezone.
tags$input(
type = "text",
id = NS(id, "client_zone"),
name = "Client zone",
style = "display: none;"
),
# Add Javascript to detect the user timezone and fill the input.
.timezone_ui_javascript(),
# The user can override the detected timezone (or fill one in if JS
# fails). The available choices can be overridden in the server.
selectInput(
inputId = NS(id, "selected_zone"),
label = "Select Your Time Zone",
label = "Select your timezone",
choices = c(
"DETECTING TIMEZONE" = "",
OlsonNames()
"...detecting timezone..." = "",
tzdb::tzdb_names()
)
)
)
)
}

.timezone_ui_javascript <- function() {
htmltools::htmlDependency(
name = "timezone_detect",
version = "1.0.0",
src = "js",
package = "bookclubber",
script = "timezone_detect.js"
)
}

#' A module for setting timezones.
#'
#' @inheritParams .shared-parameters
#' @param allowed_zones A character vector of allowed timezones.
#'
#' @return A [shiny::moduleServer()] to update the timezone input using
#' javascript.
#' @return The selected timezone as a reactive.
#' @keywords internal.
.timezone_server <- function(id = "timezone") {
.timezone_server <- function(id = "timezone",
allowed_zones = tzdb::tzdb_names()) {
moduleServer(id, function(input, output, session) {
# Inspired by https://github.com/rpodcast/shinycal.
observe({
tz <- input$client_zone
if (is.null(tz) || !tz %in% OlsonNames()) tz <- NULL
if (is.null(tz) || !tz %in% allowed_zones) tz <- NULL
updateSelectInput(
inputId = "selected_zone",
choices = c(
"PLEASE SELECT A TIMEZONE" = "",
allowed_zones
),
selected = tz
)
})
reactive(input$selected_zone)
})
}
File renamed without changes.
3 changes: 1 addition & 2 deletions man/dot-app_server.Rd

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

7 changes: 4 additions & 3 deletions man/dot-timezone_server.Rd

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

4 changes: 2 additions & 2 deletions man/dot-timezone_ui.Rd

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

Loading

0 comments on commit e85b12c

Please sign in to comment.