From ddcb2469554ee02f0fa333d58cfad881c9622683 Mon Sep 17 00:00:00 2001 From: James Cole Date: Wed, 26 Jul 2023 07:07:53 +0200 Subject: [PATCH] Add options --- .../assets/v2/api/v1/preferences/post.js | 28 +++++++++++ resources/assets/v2/api/v1/preferences/put.js | 28 +++++++++++ .../assets/v2/pages/dashboard/accounts.js | 20 ++++---- resources/assets/v2/store/get-variable.js | 11 ++++- resources/assets/v2/store/set-variable.js | 48 +++++++++++++++++++ 5 files changed, 125 insertions(+), 10 deletions(-) create mode 100644 resources/assets/v2/api/v1/preferences/post.js create mode 100644 resources/assets/v2/api/v1/preferences/put.js create mode 100644 resources/assets/v2/store/set-variable.js diff --git a/resources/assets/v2/api/v1/preferences/post.js b/resources/assets/v2/api/v1/preferences/post.js new file mode 100644 index 0000000000..b3501a34f4 --- /dev/null +++ b/resources/assets/v2/api/v1/preferences/post.js @@ -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 . + */ + +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}); + } +} diff --git a/resources/assets/v2/api/v1/preferences/put.js b/resources/assets/v2/api/v1/preferences/put.js new file mode 100644 index 0000000000..e9c4bd5cce --- /dev/null +++ b/resources/assets/v2/api/v1/preferences/put.js @@ -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 . + */ + +import {api} from "../../../boot/axios"; + +export default class Put { + put(name, value) { + let url = '/api/v1/preferences/' + name; + return api.put(url, {data: value}); + } +} diff --git a/resources/assets/v2/pages/dashboard/accounts.js b/resources/assets/v2/pages/dashboard/accounts.js index 6c043d9586..7cd83716a1 100644 --- a/resources/assets/v2/pages/dashboard/accounts.js +++ b/resources/assets/v2/pages/dashboard/accounts.js @@ -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(); }); diff --git a/resources/assets/v2/store/get-variable.js b/resources/assets/v2/store/get-variable.js index 1c8497972a..2a59337e0b 100644 --- a/resources/assets/v2/store/get-variable.js +++ b/resources/assets/v2/store/get-variable.js @@ -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)); + }); }); } diff --git a/resources/assets/v2/store/set-variable.js b/resources/assets/v2/store/set-variable.js new file mode 100644 index 0000000000..0617c28db7 --- /dev/null +++ b/resources/assets/v2/store/set-variable.js @@ -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 . + */ + +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'); + }); + }); +}