This repository has been archived on 2023-07-11. You can view files and clone it, but cannot push or open issues or pull requests.
web/js/settings.js

115 lines
3.4 KiB
JavaScript
Raw Normal View History

2023-04-05 16:10:21 +01:00
//
2023-02-09 20:29:07 +00:00
// Init:
2023-04-05 16:10:21 +01:00
//
// Setup quick links
2023-02-09 20:29:07 +00:00
const ql = ["ql0","ql1","ql2","ql3","ql4","ql5","ql6","ql7","ql8","ql9","ql10","ql11"]
storageAvailable("localStorage");
getQl();
2023-04-05 16:10:21 +01:00
// Check if already registered
ifAlreadyRegistered();
2023-04-05 16:10:21 +01:00
// Hide loading
hideLoading();
2023-04-05 16:10:21 +01:00
async function ifAlreadyRegistered() { // If already registered, show this on the page
2023-04-07 19:18:23 +01:00
if (! await isRegistered()) {
return null
} else {
document.getElementsByName("eml")[0].placeholder = "Registered";
document.getElementById("reg_text").textContent = "You are already registered";
document.getElementById("reg_button").textContent = "Log Out";
}
}
2023-02-09 20:29:07 +00:00
2023-04-05 16:10:21 +01:00
async function getQl(){ // Fetch Quick Links from localstorage
2023-02-09 20:29:07 +00:00
var qlOpt = await getQuickLinks()
if (qlOpt){
var i = 0
while (i < 12) {
if (qlOpt[i] != 'undefined') {
document.getElementById(`ql${i}`).value = qlOpt[i]
i +=1
}
}
}
}
2023-04-05 16:10:21 +01:00
async function setQl(){ // Fetch Quick Links from text input and save to localstorage
await showLoading();// called as an onclick function
2023-02-09 20:29:07 +00:00
var qlSet = []
for (i in ql) {
var opt = document.getElementById(`ql${i}`).value
if (opt != ""){
qlSet.push(opt)
}
qlSet.sort()
}
localStorage.setItem("qlOpt", JSON.stringify(qlSet))
log(`settings.setQl: User settings saved`, "INFO")
await hideLoading();
await showDone();
vibe("ok")
await delay(800);
hideDone();
}
2023-04-05 16:10:21 +01:00
async function clearQl(){ // Clear Quick Links from localstorage
2023-02-09 20:29:07 +00:00
showLoading();
localStorage.removeItem("qlOpt")
log(`settings.setQl: User settings reset to default`, "INFO")
getQl()
await hideLoading();
await showDone();
vibe("ok");
await delay(800);
hideDone();
}
2023-04-05 16:10:21 +01:00
async function isRegistered() { // Check if a device is registered, returns BOOL
if (localStorage.getItem("uuid")) {
return true
// Also need an API Call here to check if auth is working.
2023-04-07 18:59:36 +01:00
// A Suitable function exists in auth.js - move it to lib.main.js
}
localStorage.removeItem("uuid");
return false
}
2023-04-05 16:10:21 +01:00
async function register() { // Registers a device by sending POST request to API Server
if (! await isRegistered()) {
showLoading()
2023-04-05 21:10:32 +01:00
let url = `${window.location.origin}/api/v1/register/request`;
2023-04-07 21:15:33 +01:00
let email = document.getElementById("eml").value
let res = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
redirect: "follow",
body: JSON.stringify({email: email})
})
if (res.status == 201) {
showDone();
2023-04-07 18:59:36 +01:00
hideLoading();
return;
2023-04-10 20:42:19 +01:00
} else if (res.status == 403) {
log(`settings.register: Error: Fetch returned: ${res.body['errorCode']}`, "err")
document.getElementsByName("eml")[0].placeholder = "Not Authorised";
}
} else {
logout()
2023-04-10 20:42:58 +01:00
}
}
2023-04-05 16:10:21 +01:00
async function logout() { // Simply removed the UUID from localstorage
localStorage.removeItem("uuid");
location.reload();
return
}
2023-04-05 16:10:21 +01:00
async function showDone() { // Diaplays the 'Done' dialogue.
2023-04-07 23:03:50 +01:00
document.getElementById("done").style = "opacity: 1; display: block";
2023-02-09 20:29:07 +00:00
}
2023-04-05 16:10:21 +01:00
async function hideDone() { // Hides the 'Done' dialogue.
2023-04-07 23:03:50 +01:00
document.getElementById("done").style = "opacity: 0; display: none";
2023-02-09 20:29:07 +00:00
}