Skip to content

Commit

Permalink
Revert "swap cookie stuff over to dfeshiny"
Browse files Browse the repository at this point in the history
This reverts commit 860a515.
  • Loading branch information
cjrace committed Jul 20, 2024
1 parent 52a464a commit 5598e96
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 27 deletions.
10 changes: 5 additions & 5 deletions renv.lock
Original file line number Diff line number Diff line change
Expand Up @@ -435,14 +435,14 @@
},
"dfeshiny": {
"Package": "dfeshiny",
"Version": "0.2.0",
"Version": "0.1.1",
"Source": "GitHub",
"RemoteType": "github",
"RemoteHost": "api.github.com",
"RemoteUsername": "dfe-analytical-services",
"RemoteRepo": "dfeshiny",
"RemoteRef": "main",
"RemoteSha": "b19f36f04df2b391a01622611c8de9ac35047784",
"RemoteRef": "add-disconnect-message",
"RemoteSha": "c36226ace2687d79a7d333f1ce3dfcb1a8b10d30",
"RemoteHost": "api.github.com",
"Requirements": [
"RCurl",
"checkmate",
Expand All @@ -454,7 +454,7 @@
"stringr",
"styler"
],
"Hash": "806a603331839222fd16db987461edab"
"Hash": "74c15899fd7c340fc84a445917598f04"
},
"diffobj": {
"Package": "diffobj",
Expand Down
107 changes: 92 additions & 15 deletions server.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,99 @@ server <- function(input, output, session) {
}
})

# Cookies -------------------------------------------------------------------
# Pop up cookies banner
output$cookie_status <- dfeshiny::cookie_banner_server(
"cookies",
input_cookies = reactive(input$cookies),
parent_session = session,
google_analytics_key = google_analytics_key,
cookie_link_panel = "cookies_panel_ui"
)
observeEvent(input$cookies, {
if (!is.null(input$cookies)) {
if (!("dfe_analytics" %in% names(input$cookies))) {
shinyjs::show(id = "cookieMain")
} else {
shinyjs::hide(id = "cookieMain")
msg <- list(
name = "dfe_analytics",
value = input$cookies$dfe_analytics
)
session$sendCustomMessage("analytics-consent", msg)
if ("cookies" %in% names(input)) {
if ("dfe_analytics" %in% names(input$cookies)) {
if (input$cookies$dfe_analytics == "denied") {
ga_msg <- list(name = paste0("_ga_", google_analytics_key))
session$sendCustomMessage("cookie-remove", ga_msg)
}
}
}
}
} else {
shinyjs::hide(id = "cookieMain")
}
})

# Cookies panel
cookies_panel_server(
id = "cookies_panel",
input_cookies = reactive(input$cookies),
google_analytics_key = google_analytics_key
)
# Need these set of observeEvent to create a path through the cookie banner
observeEvent(input$cookieAccept, {
msg <- list(
name = "dfe_analytics",
value = "granted"
)
session$sendCustomMessage("cookie-set", msg)
session$sendCustomMessage("analytics-consent", msg)
shinyjs::show(id = "cookieAcceptDiv")
shinyjs::hide(id = "cookieMain")
})

observeEvent(input$cookieReject, {
msg <- list(
name = "dfe_analytics",
value = "denied"
)
session$sendCustomMessage("cookie-set", msg)
session$sendCustomMessage("analytics-consent", msg)
shinyjs::show(id = "cookieRejectDiv")
shinyjs::hide(id = "cookieMain")
})

observeEvent(input$hideAccept, {
shinyjs::toggle(id = "cookieDiv")
})

observeEvent(input$hideReject, {
shinyjs::toggle(id = "cookieDiv")
})

observeEvent(input$remove, {
shinyjs::toggle(id = "cookieMain")
msg <- list(name = "dfe_analytics", value = "denied")
session$sendCustomMessage("cookie-remove", msg)
session$sendCustomMessage("analytics-consent", msg)
print(input$cookies)
})

cookies_data <- reactive({
input$cookies
})

output$cookie_status <- renderText({
cookie_text_stem <- "To better understand the reach of our dashboard tools,
this site uses cookies to identify numbers of unique users as part of Google
Analytics. You have chosen to"
cookie_text_tail <- "the use of cookies on this website."
if ("cookies" %in% names(input)) {
if ("dfe_analytics" %in% names(input$cookies)) {
if (input$cookies$dfe_analytics == "granted") {
paste(cookie_text_stem, "accept", cookie_text_tail)
} else {
paste(cookie_text_stem, "reject", cookie_text_tail)
}
}
} else {
"Cookies consent has not been confirmed."
}
})

observeEvent(input$cookieLink, {
# Need to link here to where further info is located. You can
# updateTabsetPanel to have a cookie page for instance
updateTabsetPanel(session, "navlistPanel",
selected = "Support and feedback"
)
})

# TODO
# CHARTS? ----------------------------------------------
Expand Down
21 changes: 15 additions & 6 deletions ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,24 @@ ui <- function(input, output, session) {
rating = "General",
referrer = "no-referrer"
),
dfeshiny::dfe_cookie_script(),
shinyjs::useShinyjs(),
cookie_banner_ui("cookies", name = "Template R-Shiny data dashboard"),
# Variables used here are dset in the global.R file
dfeshiny::custom_disconnect_message(
links = sites_list,
publication_name = parent_pub_name,
publication_link = parent_publication
),
# Setting up cookie consent based on a cookie recording the consent:
# https://book.javascript-for-r.com/shiny-cookies.html
tags$head(
tags$script(
src = paste0(
"https://cdn.jsdelivr.net/npm/js-cookie@rc/",
"dist/js.cookie.min.js"
)
),
tags$script(src = "cookie-consent.js")
),
tags$head(includeHTML(("google-analytics.html"))),
tags$head(
tags$link(
Expand All @@ -75,6 +84,7 @@ ui <- function(input, output, session) {
href = "dfe_shiny_gov_style.css"
)
),
shinyGovstyle::cookieBanner("Department for Education (DfE) R-Shiny dashboard template"),
shinyGovstyle::header(
main_text = "",
main_link = "https://www.gov.uk/government/organisations/department-for-education",
Expand Down Expand Up @@ -108,12 +118,11 @@ ui <- function(input, output, session) {
team_email = "[email protected]",
repo_name = "https://github.com/dfe-analytical-services/shiny-template",
form_url = "https://forms.office.com"
),
cookies_panel_ui(
id = "cookies_panel",
google_analytics_key = google_analytics_key
)
),
tags$script(
src = "script.js"
),
footer(full = TRUE)
)
}
2 changes: 1 addition & 1 deletion www/cookie-consent.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Shiny.addCustomMessageHandler('cookie-set', function(msg){
getCookies();
})

Shiny.addCustomMessageHandler('cookie-clear', function(msg){
Shiny.addCustomMessageHandler('cookie-remove', function(msg){
Cookies.remove(msg.name);
getCookies();
})
Expand Down

0 comments on commit 5598e96

Please sign in to comment.