Skip to content

Commit

Permalink
cookie tracks first visti; option to delete cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
LeaSeep committed Nov 12, 2024
1 parent 325bbec commit 841d6c8
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 11 deletions.
36 changes: 33 additions & 3 deletions program/shinyApp/server.R
Original file line number Diff line number Diff line change
Expand Up @@ -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$
Expand All @@ -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, {
Expand Down
55 changes: 47 additions & 8 deletions program/shinyApp/ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
"))
),
##########
Expand All @@ -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",
Expand All @@ -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%"),
Expand Down

0 comments on commit 841d6c8

Please sign in to comment.