move local settings to utils

This commit is contained in:
Arthur Lu 2024-06-17 21:02:15 +00:00
parent d3281fc27f
commit f58abd4d4d
3 changed files with 36 additions and 19 deletions

View File

@ -1,17 +1,8 @@
import { requestAPI } from "./utils.js"; import { getSyncSettings, requestAPI } from "./utils.js";
import { API } from "../vars.js"; import { API } from "../vars.js";
export async function setupClientSync (callback) { export async function setupClientSync (callback) {
let scheme = localStorage.getItem("sync-scheme"); const { scheme, rate } = getSyncSettings();
let rate = Number(localStorage.getItem("sync-rate"));
if (!scheme) {
scheme = "always";
localStorage.setItem("sync-scheme", "always");
}
if (!rate) {
rate = "5";
localStorage.setItem("sync-rate", "5");
}
if (scheme === "always") { if (scheme === "always") {
callback(); callback();

View File

@ -1,4 +1,4 @@
import { requestPVE, requestAPI, goToPage, setTitleAndHeader, setAppearance } from "./utils.js"; import { requestPVE, requestAPI, goToPage, setTitleAndHeader, setAppearance, getSearchSettings } from "./utils.js";
import { alert, dialog } from "./dialog.js"; import { alert, dialog } from "./dialog.js";
import { setupClientSync } from "./clientsync.js"; import { setupClientSync } from "./clientsync.js";
import wfAlign from "../modules/wfa.js"; import wfAlign from "../modules/wfa.js";
@ -40,11 +40,7 @@ async function getInstances () {
} }
async function populateInstances () { async function populateInstances () {
let searchCriteria = localStorage.getItem("search-criteria"); const searchCriteria = getSearchSettings();
if (!searchCriteria) {
searchCriteria = "fuzzy";
localStorage.setItem("search-criteria", "fuzzy");
}
const searchQuery = document.querySelector("#search").value || null; const searchQuery = document.querySelector("#search").value || null;
let criteria; let criteria;
if (!searchQuery) { if (!searchQuery) {

View File

@ -268,11 +268,41 @@ export function setTitleAndHeader () {
document.querySelector("h1").innerText = organization; document.querySelector("h1").innerText = organization;
} }
const settingsDefault = {
"sync-scheme": "always",
"sync-rate": 5,
"search-criteria": "fuzzy",
"appearance-theme": "auto"
};
export function getSyncSettings () {
let scheme = localStorage.getItem("sync-scheme");
let rate = Number(localStorage.getItem("sync-rate"));
if (!scheme) {
scheme = settingsDefault["sync-scheme"];
localStorage.setItem("sync-scheme", scheme);
}
if (!rate) {
rate = settingsDefault["sync-rate"];
localStorage.setItem("sync-rate", rate);
}
return { scheme, rate };
}
export function getSearchSettings () {
let searchCriteria = localStorage.getItem("search-criteria");
if (!searchCriteria) {
searchCriteria = settingsDefault["search-criteria"];
localStorage.setItem("search-criteria", searchCriteria);
}
return searchCriteria;
}
export function setAppearance () { export function setAppearance () {
let theme = localStorage.getItem("appearance-theme"); let theme = localStorage.getItem("appearance-theme");
if (!theme) { if (!theme) {
theme = "auto"; theme = settingsDefault["appearance-theme"];
localStorage.setItem("appearance-theme", "auto"); localStorage.setItem("appearance-theme", theme);
} }
if (theme === "auto") { if (theme === "auto") {