From 841d6c85a39626721d3f0a1ce8a1fc489fd199e9 Mon Sep 17 00:00:00 2001 From: "Lea@Mac" Date: Tue, 12 Nov 2024 13:39:58 +0100 Subject: [PATCH] cookie tracks first visti; option to delete cookie --- program/shinyApp/server.R | 36 ++++++++++++++++++++++--- program/shinyApp/ui.R | 55 +++++++++++++++++++++++++++++++++------ 2 files changed, 80 insertions(+), 11 deletions(-) diff --git a/program/shinyApp/server.R b/program/shinyApp/server.R index 1f44c9f8..06224db8 100644 --- a/program/shinyApp/server.R +++ b/program/shinyApp/server.R @@ -81,7 +81,10 @@ server <- function(input,output,session){ shinyjs::hideElement(id = "enrichment_div") # Set Up ad Show user Landing page ---- -# TODO: SetUp cookie usage to land on second page +# SetUp cookie usage to land on second page + + + # Define the guide # Note do this in here to avoid setting a global upon close guide_welcome <- Cicerone$ @@ -108,8 +111,35 @@ server <- function(input,output,session){ title = "Welcome to cOmicsArt!", description = "You pressed next - redirection triggered", ) - - guide_welcome$init()$start() + + # Check if the cookie is present and update the output + # On page load, check if the cookie exists + observe({ + shinyjs::runjs(" + if (!checkHasBeenBeforeCookie()) { + setCookie('hasBeenBefore', 'true', 30); + Shiny.setInputValue('first_visit', true); + } else { + Shiny.setInputValue('first_visit', false); + } + ") + }) + + # If it is the user's first visit, start the guide + observeEvent(input$first_visit, { + if (input$first_visit) { + guide_welcome$init()$start() + } else { + showTab(inputId = "tabsetPanel1", target = "Data selection", select = TRUE) + } + }) + + # Delete the cookie if the toggle input is checked + observeEvent(input$set_cookie, { + shinyjs::runjs("deleteCookie('hasBeenBefore')") + showNotification("Cookie 'hasBeenBefore' deleted. Please refresh the page.") + }) + # Start the tour when the "Start Tour" button is clicked observeEvent(input$start_tour, { diff --git a/program/shinyApp/ui.R b/program/shinyApp/ui.R index e51a826d..bd806ead 100644 --- a/program/shinyApp/ui.R +++ b/program/shinyApp/ui.R @@ -228,6 +228,36 @@ ui <- shiny::fluidPage( } checkOverlay(); }); + + // Function to get a cookie value by name + function getCookie(name) { + const cname = name + '='; + const decodedCookie = decodeURIComponent(document.cookie); + const ca = decodedCookie.split(';'); + for(let i = 0; i < ca.length; i++) { + let c = ca[i].trim(); + if (c.indexOf(cname) == 0) return c.substring(cname.length, c.length); + } + return ''; + } + + // Function to set a cookie + function setCookie(name, value, days) { + const d = new Date(); + d.setTime(d.getTime() + (days*24*60*60*1000)); + const expires = 'expires=' + d.toUTCString(); + document.cookie = name + '=' + value + ';' + expires + ';path=/'; + } + + // Function to delete a cookie + function deleteCookie(name) { + document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;'; + } + + // Check if the 'hasBeenBefore' cookie is present + function checkHasBeenBeforeCookie() { + return getCookie('hasBeenBefore') === 'true'; + } ")) ), ########## @@ -236,12 +266,13 @@ ui <- shiny::fluidPage( shinyjs::useShinyjs(), ########## div( - style = "display:inline-block; float:right", - actionButton( - inputId = "Quit_App", - label = "Quit App", - class = "btn-secondary" - ) + style = "display: inline-block; float:right;", + # Quit App Button + actionButton( + inputId = "Quit_App", + label = "Quit App", + class = "btn-secondary" + ) ), div( id = "TitleID_normal", @@ -251,9 +282,17 @@ ui <- shiny::fluidPage( div( id = "UsefulLinks", splitLayout( - cellWidths = c("75%", "10%", "15%"), + cellWidths = c("75%", "5%", "20%"), DownloadReport_ui("DownloadTestModule"), - NULL + NULL, + div( + style = "display: inline-block; float:left;", + actionLink( + inputId = "set_cookie", + label = "Delete 'Skip first help' cookie", + style = "font-size: 0.9em; color: #555; text-decoration: underline;" + ) + ) ), splitLayout( cellWidths = c("75%", "10%", "15%"),