Add options

This commit is contained in:
James Cole 2023-07-26 07:07:53 +02:00
parent 8796168580
commit ddcb246955
No known key found for this signature in database
GPG Key ID: B49A324B7EAD6D80
5 changed files with 125 additions and 10 deletions

View File

@ -0,0 +1,28 @@
/*
* post.js
* Copyright (c) 2022 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {api} from "../../../boot/axios";
export default class Post {
post(name, value) {
let url = '/api/v1/preferences';
return api.post(url, {name: name, data: value});
}
}

View File

@ -0,0 +1,28 @@
/*
* post.js
* Copyright (c) 2022 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {api} from "../../../boot/axios";
export default class Put {
put(name, value) {
let url = '/api/v1/preferences/' + name;
return api.put(url, {data: value});
}
}

View File

@ -20,6 +20,7 @@
import ApexCharts from "apexcharts";
import {getVariable} from "../../store/get-variable.js";
import {setVariable} from "../../store/set-variable.js";
import Dashboard from "../../api/v2/chart/account/dashboard.js";
import formatLocal from "../../util/format.js";
import {format} from "date-fns";
@ -34,10 +35,11 @@ export default () => ({
loading: false,
loadingAccounts: false,
accountList: [],
autoConvert: false,
autoConversion: false,
chart: null,
switchConversion() {
this.autoConvert = !this.autoConvert;
switchAutoConversion() {
this.autoConversion = !this.autoConversion;
setVariable('autoConversion', this.autoConversion);
this.loadChart();
},
loadChart() {
@ -98,11 +100,11 @@ export default () => ({
let entry = [];
let collection = [];
// use the "native" currency code and use the "converted_entries" as array
if (this.autoConvert) {
if (this.autoConversion) {
window.currencies.push(current.native_code);
collection = current.converted_entries;
}
if (!this.autoConvert) {
if (!this.autoConversion) {
window.currencies.push(current.currency_code);
collection = current.entries;
}
@ -169,7 +171,7 @@ export default () => ({
this.loadingAccounts = false;
});
}).then(() => {
console.log(this.accountList);
// console.log(this.accountList);
});
}
}
@ -178,8 +180,10 @@ export default () => ({
},
init() {
console.log('init');
Promise.all([getVariable('viewRange'),]).then((values) => {
// console.log('init');
Promise.all([getVariable('viewRange', '1M'), getVariable('autoConversion', false),]).then((values) => {
this.autoConversion = values[1];
// console.log(values[1]);
this.loadChart();
this.loadAccounts();
});

View File

@ -19,8 +19,9 @@
*/
import Get from "../api/v1/preferences/index.js";
import Post from "../api/v1/preferences/post.js";
export function getVariable(name) {
export function getVariable(name, defaultValue = null) {
// currently unused, window.X can be used by the blade template
// to make things available quicker than if the store has to grab it through the API.
@ -38,7 +39,13 @@ export function getVariable(name) {
return getter.getByName(name).then((response) => {
// console.log('Get from API');
return Promise.resolve(parseResponse(name, response));
}).catch(() => {
// preference does not exist (yet).
// POST it and then return it anyway.
let poster = (new Post);
poster.post(name, defaultValue).then((response) => {
return Promise.resolve(parseResponse(name, response));
});
});
}

View File

@ -0,0 +1,48 @@
/*
* get-variable.js
* Copyright (c) 2023 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import Put from "../api/v1/preferences/put.js";
import Post from "../api/v1/preferences/post.js";
export function setVariable(name, value = null) {
// currently unused, window.X can be used by the blade template
// to make things available quicker than if the store has to grab it through the API.
// then again, it's not that slow.
// set in window.x
// window[name] = value;
// set in store:
window.store.set(name, value);
// post to user preferences (because why not):
let putter = new Put();
putter.put(name, value).then((response) => {
// console.log('Put in API');
}).catch(() => {
// preference does not exist (yet).
// POST it
let poster = (new Post);
poster.post(name, value).then((response) => {
// console.log('Post in API');
});
});
}