Add static/settings.js for the settings, rates and password forms
This commit is contained in:
1 parent
966ccf9e54
commit
cb8ace5ec6
1 file changed
+159
@@ -0,0 +1,159 @@
|
||||
/* Skuld — settings: the lot method, the tax rate, wealth tax, the admin cost
|
||||
* switch, the rate table with editing, and the admin password. Changing the
|
||||
* method warns first, because it changes every figure. */
|
||||
|
||||
(function () {
|
||||
"use strict";
|
||||
|
||||
var settingsForm = document.querySelector("[data-settings-form]");
|
||||
var priceForm = document.querySelector("[data-price-form]");
|
||||
var passwordForm = document.querySelector("[data-password-form]");
|
||||
var rateRows = document.querySelector("[data-rates]");
|
||||
|
||||
var loadedMethod = "FIFO";
|
||||
|
||||
function value(form, name) {
|
||||
var input = form.querySelector('[name="' + name + '"]');
|
||||
return input ? String(input.value).trim() : "";
|
||||
}
|
||||
|
||||
function checked(form, name) {
|
||||
var input = form.querySelector('[name="' + name + '"]');
|
||||
return input ? Boolean(input.checked) : false;
|
||||
}
|
||||
|
||||
function set(form, name, text) {
|
||||
var input = form.querySelector('[name="' + name + '"]');
|
||||
if (input) {
|
||||
input.value = text === null || text === undefined ? "" : String(text);
|
||||
}
|
||||
}
|
||||
|
||||
function setChecked(form, name, isChecked) {
|
||||
var input = form.querySelector('[name="' + name + '"]');
|
||||
if (input) {
|
||||
input.checked = Boolean(isChecked);
|
||||
}
|
||||
}
|
||||
|
||||
function cell(row, text, className) {
|
||||
var td = document.createElement("td");
|
||||
td.textContent = text;
|
||||
if (className) {
|
||||
td.className = className;
|
||||
}
|
||||
row.appendChild(td);
|
||||
return td;
|
||||
}
|
||||
|
||||
function loadSettings() {
|
||||
return Skuld.api("GET", "/api/settings").then(function (settings) {
|
||||
loadedMethod = settings.lot_method || "FIFO";
|
||||
set(settingsForm, "lot_method", loadedMethod);
|
||||
set(settingsForm, "tax_rate_percent", settings.tax_rate_percent);
|
||||
setChecked(settingsForm, "wealth_tax_enabled", settings.wealth_tax_enabled === "1");
|
||||
set(
|
||||
settingsForm,
|
||||
"wealth_tax_threshold_ore",
|
||||
settings.wealth_tax_threshold_ore === "0"
|
||||
? ""
|
||||
: Skuld.formatOre(settings.wealth_tax_threshold_ore)
|
||||
);
|
||||
set(settingsForm, "wealth_tax_rate_percent", settings.wealth_tax_rate_percent);
|
||||
setChecked(settingsForm, "deduct_admin_costs", settings.deduct_admin_costs === "1");
|
||||
set(settingsForm, "year_end_price_source", settings.year_end_price_source);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
function renderRates(rates) {
|
||||
rateRows.textContent = "";
|
||||
rates.forEach(function (rate) {
|
||||
var row = document.createElement("tr");
|
||||
cell(row, Skuld.formatDate(rate.date), "");
|
||||
cell(row, Skuld.formatKroner(rate.nok_per_btc_ore), "numeric");
|
||||
cell(row, rate.source, "");
|
||||
var actions = document.createElement("td");
|
||||
var button = document.createElement("button");
|
||||
button.type = "button";
|
||||
button.className = "button-secondary";
|
||||
button.textContent = "Endre";
|
||||
button.addEventListener("click", function () {
|
||||
set(priceForm, "date", rate.date);
|
||||
set(priceForm, "nok_per_btc", Skuld.formatOre(rate.nok_per_btc_ore));
|
||||
set(priceForm, "source", rate.source);
|
||||
priceForm.querySelector('[name="nok_per_btc"]').focus();
|
||||
});
|
||||
actions.appendChild(button);
|
||||
row.appendChild(actions);
|
||||
rateRows.appendChild(row);
|
||||
});
|
||||
}
|
||||
|
||||
function loadRates() {
|
||||
return Skuld.api("GET", "/api/prices").then(function (rates) {
|
||||
renderRates(rates);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
settingsForm.addEventListener("submit", function (event) {
|
||||
event.preventDefault();
|
||||
Skuld.clearErrors();
|
||||
var method = value(settingsForm, "lot_method");
|
||||
if (method !== loadedMethod
|
||||
&& !window.confirm("Å byte metode frå " + loadedMethod + " til " + method
|
||||
+ " reknar alle tala på nytt. Vil du halde fram?")) {
|
||||
set(settingsForm, "lot_method", loadedMethod);
|
||||
return;
|
||||
}
|
||||
Skuld.api("PATCH", "/api/settings", {
|
||||
lot_method: method,
|
||||
tax_rate_percent: value(settingsForm, "tax_rate_percent"),
|
||||
wealth_tax_enabled: checked(settingsForm, "wealth_tax_enabled") ? 1 : 0,
|
||||
wealth_tax_threshold_ore: value(settingsForm, "wealth_tax_threshold_ore"),
|
||||
wealth_tax_rate_percent: value(settingsForm, "wealth_tax_rate_percent"),
|
||||
deduct_admin_costs: checked(settingsForm, "deduct_admin_costs") ? 1 : 0,
|
||||
year_end_price_source: value(settingsForm, "year_end_price_source")
|
||||
}).then(function () {
|
||||
Skuld.setMessage("Innstillingane er lagra.", false);
|
||||
return loadSettings();
|
||||
}).catch(function (error) {
|
||||
Skuld.showError(error);
|
||||
});
|
||||
});
|
||||
|
||||
priceForm.addEventListener("submit", function (event) {
|
||||
event.preventDefault();
|
||||
Skuld.clearErrors();
|
||||
var date = value(priceForm, "date");
|
||||
Skuld.api("PUT", "/api/prices/" + encodeURIComponent(date), {
|
||||
nok_per_btc: value(priceForm, "nok_per_btc"),
|
||||
source: value(priceForm, "source")
|
||||
}).then(function () {
|
||||
Skuld.setMessage("Kursen er lagra.", false);
|
||||
return loadRates();
|
||||
}).catch(function (error) {
|
||||
Skuld.showError(error);
|
||||
});
|
||||
});
|
||||
|
||||
passwordForm.addEventListener("submit", function (event) {
|
||||
event.preventDefault();
|
||||
Skuld.clearErrors();
|
||||
Skuld.api("POST", "/api/auth/password", {
|
||||
current_password: value(passwordForm, "current_password"),
|
||||
new_password: value(passwordForm, "new_password")
|
||||
}).then(function () {
|
||||
set(passwordForm, "current_password", "");
|
||||
set(passwordForm, "new_password", "");
|
||||
Skuld.setMessage("Passordet er endra.", false);
|
||||
}).catch(function (error) {
|
||||
Skuld.showError(error);
|
||||
});
|
||||
});
|
||||
|
||||
Skuld.on("settings", function () {
|
||||
return loadSettings().then(loadRates);
|
||||
});
|
||||
})();
|
||||
Reference in new issue
Block a user