-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from dfe-analytical-services/add-disconnect-mes…
…sage Add custom disconnect message function
- Loading branch information
Showing
8 changed files
with
348 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
#' Custom disconnect message | ||
#' | ||
#' | ||
#' @description | ||
#' Create the html overlay panel to appear when RSConnect disconnects | ||
#' | ||
#' @param refresh the text to appear that will refresh the page when clicked | ||
#' @param links A list of mirrors or alternative links to the dashboard | ||
#' @param publication_name The parent publication name | ||
#' @param publication_link The link to the publication on Explore Education | ||
#' Statistics | ||
#' | ||
#' @importFrom htmltools tags tagList | ||
#' | ||
#' | ||
#' @return A html overlay panel that appears when RSConnect disconnects for a | ||
#' public R Shiny dashboard in DfE | ||
#' @export | ||
#' | ||
#' @examples | ||
#' custom_disconnect_message( | ||
#' refresh = "Refresh page", | ||
#' links = c( | ||
#' "https://department-for-education.shinyapps.io/dfe-shiny-template/", | ||
#' "https://department-for-education.shinyapps.io/dfe-shiny-template-overflow/" # nolint: [line_length_linter] | ||
#' ), | ||
#' publication_name = "Explore Education Statistics Publication", | ||
#' publication_link = | ||
#' "https://explore-education-statistics.service.gov.uk/find-statistics/pupil-attendance-in-schools" # nolint: [line_length_linter] | ||
#' ) | ||
#' | ||
custom_disconnect_message <- function(refresh = "Refresh page", | ||
links = NULL, | ||
publication_name = NULL, | ||
publication_link = NULL) { | ||
# Check links are valid | ||
|
||
is_valid_sites_list <- function(sites) { | ||
lapply( | ||
stringr::str_trim(sites), startsWith, | ||
"https://department-for-education.shinyapps.io/" | ||
) | ||
} | ||
|
||
if (FALSE %in% is_valid_sites_list(links) || | ||
"https://department-for-education.shinyapps.io/" %in% links) { # nolint: [indentation_linter] | ||
stop("You have entered an invalid site link in the links argument.") | ||
} | ||
|
||
pub_prefix <- c( | ||
"https://explore-education-statistics.service.gov.uk/find-statistics/", | ||
"https://www.explore-education-statistics.service.gov.uk/find-statistics/", | ||
"https://www.gov.uk/", | ||
"https://gov.uk/" | ||
) | ||
|
||
is_valid_publication_link <- function(link) { | ||
startsWith(stringr::str_trim(link), pub_prefix) | ||
} | ||
|
||
if (RCurl::url.exists(publication_link) == FALSE || | ||
(TRUE %in% is_valid_publication_link(publication_link)) == FALSE || # nolint: [indentation_linter] | ||
publication_link %in% pub_prefix) { | ||
stop("You have entered an invalid publication link in the publication_link | ||
argument.") | ||
} | ||
|
||
checkmate::assert_string(refresh) | ||
tagList( | ||
tags$script( | ||
paste0( | ||
"$(function() {", | ||
" $(document).on('shiny:disconnected', function(event) {", | ||
" $('#custom-disconnect-dialog').show();", | ||
" $('#ss-overlay').show();", | ||
" })", | ||
"});" | ||
) | ||
), | ||
tags$div( | ||
id = "custom-disconnect-dialog", | ||
style = "display: none !important;", | ||
tags$div( | ||
id = "ss-connect-refresh", | ||
tags$p("You've lost connection to the dashboard server - please try | ||
refreshing the page:"), | ||
tags$p(tags$a( | ||
id = "ss-reload-link", | ||
href = "#", "Refresh page", | ||
onclick = "window.location.reload(true);" | ||
)), | ||
if (length(links) > 1) { | ||
tags$p( | ||
"If this persists, you can also view the dashboard at one of our | ||
mirror sites:", | ||
tags$p( | ||
tags$a(href = links[1], "Site 1"), | ||
" - ", | ||
tags$a(href = links[2], "Site 2"), | ||
if (length(links) == 3) { | ||
"-" | ||
}, | ||
if (length(links) == 3) { | ||
tags$a(href = links[3], "Site 3") | ||
} | ||
) | ||
) | ||
}, | ||
if (!is.null(publication_name)) { | ||
tags$p( | ||
"All the data used in this dashboard can also be viewed or | ||
downloaded via the ", | ||
tags$a( | ||
href = publication_link, | ||
publication_name | ||
), | ||
"on Explore Education Statistics." | ||
) | ||
}, | ||
tags$p( | ||
"Please contact", | ||
tags$a( | ||
href = "mailto:statistics.development@education.gov.uk", | ||
"statistics.development@education.gov.uk" | ||
), | ||
"with details of any problems with this resource." | ||
) | ||
) | ||
), | ||
tags$div(id = "ss-overlay", style = "display: none;"), | ||
tags$head(htmltools::tags$style( | ||
glue::glue( | ||
.open = "{{", .close = "}}", | ||
"#custom-disconnect-dialog a { | ||
display: {{ if (refresh == '') 'none' else 'inline' }} !important; | ||
color: #1d70b8 !important; | ||
font-size: 16px !important; | ||
font-weight: normal !important; | ||
}" | ||
) | ||
)) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
server <- function(input, output, session) { | ||
output$cookie_status <- cookie_banner_server( | ||
"cookies", | ||
input_cookies = reactive(input$cookies), | ||
input_clear = reactive(input$cookie_consent_clear), | ||
input_cookies = shiny::reactive(input$cookies), | ||
input_clear = shiny::reactive(input$cookie_consent_clear), | ||
parent_session = session, | ||
google_analytics_key = google_analytics_key | ||
google_analytics_key = google_analytics_key # # nolint: [object_usage_linter] | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
test_that("publication link is valid", { | ||
# Test that an EES publication link passes | ||
expect_no_error( | ||
custom_disconnect_message( | ||
refresh = "Refresh page", | ||
links = "https://department-for-education.shinyapps.io/dfe-shiny-template/", # nolint: [line_length_linter] | ||
publication_name = "Pupil attendance in schools", | ||
publication_link = "https://explore-education-statistics.service.gov.uk/find-statistics/pupil-attendance-in-schools" # nolint: [line_length_linter] | ||
) | ||
) | ||
|
||
# Test that a gov.uk publication link passes | ||
expect_no_error( | ||
custom_disconnect_message( | ||
refresh = "Refresh page", | ||
links = "https://department-for-education.shinyapps.io/dfe-shiny-template/", # nolint: [line_length_linter] | ||
publication_name = "Pupil attendance in schools", | ||
publication_link = "https://www.gov.uk/government/collections/job-and-skills-data" # nolint: [line_length_linter] | ||
) | ||
) | ||
|
||
# Test that it fails for non-existent links/typos/random links | ||
expect_error( | ||
custom_disconnect_message( | ||
refresh = "Refresh page", | ||
links = "https://department-for-education.shinyapps.io/dfe-shiny-template/", # nolint: [line_length_linter] | ||
publication_name = "Pupil attendance in schools", | ||
publication_link = "https://explore-education-statistics.service.gov.uk/find-statistics/hello" # nolint: [line_length_linter] | ||
) | ||
) | ||
|
||
expect_error( | ||
custom_disconnect_message( | ||
refresh = "Refresh page", | ||
links = "https://department-for-education.shinyapps.io/dfe-shiny-template/", # nolint: [line_length_linter] | ||
publication_name = "Pupil attendance in schools", | ||
publication_link = "https://www.bbc.com/news" | ||
) | ||
) | ||
|
||
# Test that just linking to EES homepage fails | ||
|
||
expect_error( | ||
custom_disconnect_message( | ||
refresh = "Refresh page", | ||
links = "https://department-for-education.shinyapps.io/dfe-shiny-template/", # nolint: [line_length_linter] | ||
publication_name = "Pupil attendance in schools", | ||
publication_link = "https://explore-education-statistics.service.gov.uk/" # nolint: [line_length_linter] | ||
) | ||
) | ||
}) | ||
|
||
test_that("site links are valid", { | ||
# Test that valid long shinyapps.io link pass | ||
expect_no_error( | ||
custom_disconnect_message( | ||
refresh = "Refresh page", | ||
links = "https://department-for-education.shinyapps.io/dfe-shiny-template/", # nolint: [line_length_linter] | ||
publication_name = "Pupil attendance in schools", | ||
publication_link = "https://explore-education-statistics.service.gov.uk/find-statistics/pupil-attendance-in-schools" # nolint: [line_length_linter] | ||
) | ||
) | ||
|
||
# Test that valid short shinyapps.io link pass | ||
expect_no_error( | ||
custom_disconnect_message( | ||
refresh = "Refresh page", | ||
links = "https://department-for-education.shinyapps.io/dfe-shiny-template/", # nolint: [line_length_linter] | ||
publication_name = "Pupil attendance in schools", | ||
publication_link = "https://explore-education-statistics.service.gov.uk/find-statistics/pupil-attendance-in-schools" # nolint: [line_length_linter] | ||
) | ||
) | ||
|
||
# Test that a list of valid shinyapps.io links pass | ||
expect_no_error( | ||
custom_disconnect_message( | ||
refresh = "Refresh page", | ||
links = c( | ||
"https://department-for-education.shinyapps.io/dfe-shiny-template/", # nolint: [line_length_linter] | ||
"https://department-for-education.shinyapps.io/dfe-shiny-template-overflow/", # nolint: [line_length_linter] | ||
"https://department-for-education.shinyapps.io/dfe-shiny-template/" # nolint: [line_length_linter] | ||
), | ||
publication_name = "Pupil attendance in schools", | ||
publication_link = "https://explore-education-statistics.service.gov.uk/find-statistics/pupil-attendance-in-schools" # nolint: [line_length_linter] | ||
) | ||
) | ||
|
||
# Test that it fails for non-shinyapps.io links | ||
expect_error( | ||
custom_disconnect_message( | ||
refresh = "Refresh page", | ||
links = "https://explore-education-statistics.service.gov.uk/find-statistics/pupil-attendance-in-schools", # nolint: [line_length_linter] | ||
publication_name = "Pupil attendance in schools", | ||
publication_link = "https://explore-education-statistics.service.gov.uk/find-statistics/pupil-attendance-in-schools" # nolint: [line_length_linter] | ||
) | ||
) | ||
|
||
expect_error( | ||
custom_disconnect_message( | ||
refresh = "Refresh page", | ||
links = c( | ||
"https://explore-education-statistics.service.gov.uk/find-statistics/pupil-attendance-in-schools", # nolint: [line_length_linter] | ||
"https://department-for-education.shinyapps.io/dfe-shiny-template/" # nolint: [line_length_linter] | ||
), | ||
publication_name = "Pupil attendance in schools", | ||
publication_link = "https://explore-education-statistics.service.gov.uk/find-statistics/pupil-attendance-in-schools" # nolint: [line_length_linter] | ||
) | ||
) | ||
|
||
# Test that it fails for a link to shinyapps.io with no suffix | ||
expect_error( | ||
custom_disconnect_message( | ||
refresh = "Refresh page", | ||
links = c( | ||
"https://department-for-education.shinyapps.io/dfe-shiny-template/", | ||
"https://department-for-education.shinyapps.io/" | ||
), | ||
publication_name = "Pupil attendance in schools", | ||
publication_link = "https://explore-education-statistics.service.gov.uk/find-statistics/pupil-attendance-in-schools" # nolint: [line_length_linter] | ||
) | ||
) | ||
}) |