Store preferences in local storage

Fixes #2.
This commit is contained in:
Florine W. Dekker 2020-08-12 19:20:46 +02:00
parent fc1fdb899d
commit e9aee88387
Signed by: FWDekker
GPG Key ID: B1B567AF58D6EE0F
4 changed files with 13 additions and 20 deletions

BIN
package-lock.json generated

Binary file not shown.

View File

@ -16,8 +16,7 @@
"deploy": "grunt deploy"
},
"dependencies": {
"@fwdekker/template": "^0.0.20",
"js-cookie": "^2.2.1"
"@fwdekker/template": "^0.0.20"
},
"devDependencies": {
"grunt": "^1.2.1",

View File

@ -47,6 +47,6 @@
<div id="footer"></div>
</main>
<script src="bundle.js"></script>
<script src="bundle.js?v=%%VERSION_NUMBER%%"></script>
</body>
</html>

View File

@ -1,7 +1,7 @@
import {$, doAfterLoad, footer, header, nav} from "@fwdekker/template";
import Cookies from "js-cookie";
const storageKey = "/tools/random-fo76//selected-signatures";
const signatureColCount = 8;
@ -43,35 +43,29 @@ const setAllSignatures = checked => {
const checkboxes = document.querySelectorAll("#signatures input");
for (let i = 0; i < checkboxes.length; i++)
checkboxes[i].checked = checked;
saveSelectedSignaturesToCookie();
saveSelectedSignaturesToStorage();
updateSignatureToggle();
};
/**
* (De)selects signatures based on the selection stored in a cookie.
* (De)selects signatures based on the selection stored in local storage.
*/
const loadSelectedSignaturesFromCookie = () => {
const cookie = Cookies.get("selectedSignatures");
const loadSelectedSignaturesFromStorage = () => {
const item = localStorage.getItem(storageKey);
let signatures;
if (cookie === undefined)
if (item === null)
signatures = [];
else
signatures = cookie.split(",");
signatures = item.split(",");
setSelectedSignatures(signatures);
};
/**
* Saves the currently-selected signatures to a cookie.
* Saves the currently-selected signatures to local storage.
*/
const saveSelectedSignaturesToCookie =
() => Cookies.set("selectedSignatures", getSelectedSignatures().join(","), {
expires: 5 * 365,
secure: true,
sameSite: "lax",
path: ""
});
const saveSelectedSignaturesToStorage = () => localStorage.setItem(storageKey, getSelectedSignatures().join(","));
/**
* Updates the button used to toggle all signatures on or off.
@ -145,7 +139,7 @@ const createSignatureButtons = signatures => {
checkbox.value = signature;
checkbox.onclick = () => {
updateSignatureToggle();
saveSelectedSignaturesToCookie();
saveSelectedSignaturesToStorage();
};
col.appendChild(checkbox);
@ -212,7 +206,7 @@ doAfterLoad(() => {
downloadSignatures(
signatures => {
createSignatureButtons(signatures);
loadSelectedSignaturesFromCookie();
loadSelectedSignaturesFromStorage();
},
errorResponse => {
const form = $("#signatureForm");