Skip to content

Commit

Permalink
feat guarding on localstorage writing/reading
Browse files Browse the repository at this point in the history
Signed-off-by: WoodenMaiden <yann.pomie@laposte.net>
  • Loading branch information
WoodenMaiden committed Jun 14, 2024
1 parent 9b655b5 commit 796ac56
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions agrold-javaweb/src/main/webapp/scripts/localStorage.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// https://stackoverflow.com/questions/16427636/check-if-localstorage-is-available
const LocalStorageDataConsent = {
"sparqlEditor.history.enabled": "Store your history of sparql requests",
"advancedSearch.history.enabled": "Store your history of advanced searches"
Expand All @@ -13,8 +12,39 @@ const acceptLocalStorage = (accepted) => {
document.getElementById("localStorage-ask")?.classList.replace("slide-in", "slide-out");
console.log(accepted? "accepted": "refused");
}

const hasAcceptedLocalStorage = () => sessionStorage.getItem("acceptLocalStorage") === "true"

const consentedToTreatment = (key) => {
if (hasAcceptedLocalStorage()) {
switch (key) {
//case "advancedSearch.xxx":
case "advancedSearch.history":
return sessionStorage.getItem("advancedSearch.history.enabled") === "true"

//case "sparqlEditor.xxx":
case "sparqlEditor.history":
return sessionStorage.getItem("sparqlEditor.history.enabled") === "true"

default:
return false
}
}
return false
}

const localStorageSet = (key, val) => isLocalStorageAvailable() &&
consentedToTreatment(key) &&
localStorage.setItem(key, val)

const localStorageGet = (key) => {
if (isLocalStorageAvailable() && consentedToTreatment(key)) {
return localStorage.getItem(key)
}
return null
}


const acceptData = (arrayOfAuthorizedData) => {
if(isLocalStorageAvailable()){
acceptLocalStorage(true)
Expand Down Expand Up @@ -43,15 +73,14 @@ const acceptFromSelection = (e) => {
return false
}

/// popup setup
const popup = document.getElementById("localStorage-ask")
document.getElementById("labelstore-form")?.addEventListener("submit", acceptFromSelection)

console.log("localStorage available: " + isLocalStorageAvailable())

// v question unanswered
if(isLocalStorageAvailable() && sessionStorage.getItem("acceptLocalStorage") === null){
if (popup) popup.style.visibility = "visible";


const consentList = document.getElementById('consent-inputs')

Object.entries(LocalStorageDataConsent).forEach(([key, value]) => {
Expand Down

0 comments on commit 796ac56

Please sign in to comment.