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.1 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-05-01 21:04:19 +01: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
2023-05-01 21:04:19 +01:00
ifAlreadyRegistered()
2023-04-05 16:10:21 +01:00
// Hide loading
2023-05-01 21:04:19 +01:00
hideLoading()
2023-04-05 16:10:21 +01:00
async function ifAlreadyRegistered() { // If already registered, show this on the page
2023-05-01 21:04:19 +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-05-01 21:04:19 +01: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-02-09 20:29:07 +00:00
}
2023-05-01 21:04:19 +01:00
}
2023-02-09 20:29:07 +00:00
}
2023-04-05 16:10:21 +01:00
async function setQl(){ // Fetch Quick Links from text input and save to localstorage
2023-05-01 21:04:19 +01:00
await showLoading()// called as an onclick function
var qlSet = []
for (i in ql) {
var opt = document.getElementById(`ql${i}`).value
if (opt != ''){
qlSet.push(opt)
2023-02-09 20:29:07 +00:00
}
2023-05-01 21:04:19 +01:00
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-02-09 20:29:07 +00:00
}
2023-04-05 16:10:21 +01:00
async function clearQl(){ // Clear Quick Links from localstorage
2023-05-01 21:04:19 +01: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-02-09 20:29:07 +00:00
}
2023-04-05 16:10:21 +01:00
async function isRegistered() { // Check if a device is registered, returns BOOL
2023-05-01 21:04:19 +01:00
if (localStorage.getItem('uuid')) {
return true
// Also need an API Call here to check if auth is working.
// 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
2023-05-01 21:04:19 +01:00
if (! await isRegistered()) {
showLoading()
let url = `${window.location.origin}/api/v1/register/request`
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()
hideLoading()
return
} else if (res.status == 403) {
log(`settings.register: Error: Fetch returned: ${res.body['errorCode']}`, 'err')
document.getElementsByName('eml')[0].placeholder = 'Not Authorised'
2023-04-10 20:42:58 +01:00
}
2023-05-01 21:04:19 +01:00
} else {
logout()
}
}
2023-04-05 16:10:21 +01:00
async function logout() { // Simply removed the UUID from localstorage
2023-05-01 21:04:19 +01:00
localStorage.removeItem('uuid')
location.reload()
return
}
2023-04-05 16:10:21 +01:00
async function showDone() { // Diaplays the 'Done' dialogue.
2023-05-01 21:04:19 +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-05-01 21:04:19 +01:00
document.getElementById('done').style = 'opacity: 0; display: none'
2023-02-09 20:29:07 +00:00
}