Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cookie first help #378

Merged
merged 3 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions program/shinyApp/server.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ 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
# Define the guide
# Note do this in here to avoid setting a global upon close
guide_welcome <- Cicerone$
Expand All @@ -99,17 +98,46 @@ server <- function(input,output,session){
<div style='font-size: 18px; margin-top: 10px;'>
<p><i class='fas fa-question-circle'></i> Need help? Press the blue button</p>
<p><i class='fas fa-rocket'></i> Want to start directly? Click 'Next'.</p>
<div style='font-size: 14px; color: #777; margin-top: 15px;'>
<p><input type='checkbox' id='set_cookie_checkbox'> Do not show next time (sets a cookie)</p>
</div>
</div>
</div>
")
)$
)$
step(
el = "tabsetPanel1",
title = "Welcome to cOmicsArt!",
description = "You pressed next - redirection triggered",
)

# Check if the cookie is present and update the output
# On page load, check if the cookie exists
observe({
shinyjs::runjs("
if (!checkHasBeenBeforeCookie()) {
Shiny.setInputValue('first_visit', true);
} else {
Shiny.setInputValue('first_visit', false);
}
")
})

guide_welcome$init()$start()
# 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
67 changes: 59 additions & 8 deletions program/shinyApp/ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,48 @@ 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';
}

// Listen for changes on the checkbox and set the cookie if checked
document.addEventListener('click', function(event) {
if (event.target && event.target.id === 'set_cookie_checkbox') {
const isChecked = document.getElementById('set_cookie_checkbox').checked;
if (isChecked) {
setCookie('hasBeenBefore', 'true', 30);
} else {
deleteCookie('hasBeenBefore');
}
}
});
"))
),
##########
Expand All @@ -236,12 +278,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 +294,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
Loading