Skip to content

Commit

Permalink
update init_cookies to have the JS code inline and add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cjrace committed Jul 31, 2024
1 parent 973c796 commit c887b61
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 68 deletions.
88 changes: 60 additions & 28 deletions R/cookies.R
Original file line number Diff line number Diff line change
Expand Up @@ -244,15 +244,18 @@ cookie_banner_server <- function(
#' init_cookies
#'
#' @description
#' init_cookies() creates a local copy of the JavaScript file
#' required for cookies to work.
#' It checks whether there is already a www/ folder and if not, it creates one.
#' It then checks whether the cookie-consent.js file exists in the www/ folder.
#' If the file exists, it will print a message in the console to let you know.
#' If the file doesn't exist, it will pull a copy from the GitHub repo.
#' If it cannot connect to the repo then it will print "Download failed".
#' No input parameters are required
#' Call init_cookies() in the console to run the function
#' init_cookies() creates a local copy of the JavaScript file required for
#' cookies to work. It checks whether there is already a www/ folder and if
#' not, it creates one. It then checks whether the cookie-consent.js file
#' exists in the www/ folder. If the file exists, it will print a message in
#' the console to let you know it has overwritten it. If the file doesn't
#' exist, it will make one and confirm it has done so.
#'
#' No input parameters are required, run `init_cookies()` in the console to run
#' the function
#'
#' @param create_file Boolean, TRUE by default, if FALSE then will print to
#' the console rather than create a new file
#'
#' @return NULL
#' @export
Expand All @@ -261,29 +264,58 @@ cookie_banner_server <- function(
#' if (interactive()) {
#' init_cookies()
#' }
init_cookies <- function() {
sub_dir <- "www"
init_cookies <- function(create_file = TRUE) {
if (!is.logical(create_file)) {
stop("create_file must always be TRUE or FALSE")
}

output_dir <- file.path(sub_dir)
# Create the JS for the file ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cookie_js <- "function getCookies(){
var res = Cookies.get();
Shiny.setInputValue('cookies', res);
}
if (!dir.exists(output_dir)) {
dir.create(output_dir)
} else {
message("www folder already exists!")
}
Shiny.addCustomMessageHandler('cookie-set', function(msg){
Cookies.set(msg.name, msg.value);
getCookies();
})
github_area <- "https://raw.githubusercontent.com/dfe-analytical-services/"
Shiny.addCustomMessageHandler('cookie-clear', function(msg){
Cookies.remove(msg.name);
getCookies();
})
tryCatch(
utils::download.file(
url = paste0(github_area, "dfeshiny/main/inst/cookie-consent.js"),
destfile = "www/cookie-consent.js"
),
error = function(e) {
return("Download failed")
},
message("Cookie script updated")
)
$(document).on('shiny:connected', function(ev){
getCookies();
})
Shiny.addCustomMessageHandler('analytics-consent', function(msg){
gtag('consent', 'update', {
'analytics_storage': msg.value
});
})"
# End of JS for the file ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

if (create_file == TRUE) {
sub_dir <- "www"

output_dir <- file.path(sub_dir)

if (!dir.exists(output_dir)) {
dir.create(output_dir)
}

if (file.exists("www/cookie-consent.js")) {
message("www/cookie-consent.js already exists, updating file...")
cat(cookie_js, file = "www/cookie-consent.js", sep = "\n")
message("...file successfully updated")
} else {
cat(cookie_js, file = "www/cookie-consent.js", sep = "\n")
message("Created cookies script as www/cookie-consent.js")
}
} else {
cat(cookie_js, file = "", sep = "\n")
}
}

#' cookies_panel_ui
Expand Down
26 changes: 0 additions & 26 deletions inst/cookie-consent.js

This file was deleted.

7 changes: 3 additions & 4 deletions man/init_analytics.Rd

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

24 changes: 14 additions & 10 deletions man/init_cookies.Rd

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

14 changes: 14 additions & 0 deletions tests/testthat/test-init_cookies.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
test_that("Rejects non-boolean for create_file", {
expect_error(init_cookies(create_file = "Funky non-boolean"))
})

test_that("Javascript writes out", {
output <- capture.output(init_cookies(create_file = FALSE))

# Expect to find a couple of other key parts
expect_equal(output[1], "function getCookies(){")
expect_true(grepl("Shiny\\.setInputValue\\('cookies'", output[3]))
expect_true(grepl("cookie-set", output[6]))
expect_true(grepl("cookie-clear", output[11]))
expect_true(grepl("analytics-consent", output[20]))
})

0 comments on commit c887b61

Please sign in to comment.