Files
firefly-iii/resources/assets/v2/src/pages/dashboard/boxes.js

180 lines
6.7 KiB
JavaScript
Raw Normal View History

2023-07-22 16:42:33 +02:00
/*
* dashboard.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/>.
*/
2025-03-23 09:05:06 +01:00
import Summary from "../../api/v1/summary/index.js";
2023-07-22 16:42:33 +02:00
import {format} from "date-fns";
import {getVariable} from "../../store/get-variable.js";
2023-08-06 11:21:20 +02:00
import formatMoney from "../../util/format-money.js";
2023-10-01 07:17:56 +02:00
import {getCacheKey} from "../../support/get-cache-key.js";
2024-04-20 16:18:41 +02:00
import {cleanupCache} from "../../support/cleanup-cache.js";
2023-07-23 07:10:31 +02:00
2023-08-12 07:53:11 +02:00
let afterPromises = false;
2023-07-22 16:42:33 +02:00
export default () => ({
balanceBox: {amounts: [], subtitles: []},
billBox: {paid: [], unpaid: []},
leftBox: {left: [], perDay: []},
netBox: {net: []},
2025-03-23 15:04:38 +01:00
convertToNative: false,
2023-07-24 18:58:35 +02:00
loading: false,
2023-08-06 18:33:29 +02:00
boxData: null,
boxOptions: null,
getFreshData() {
2023-10-01 07:17:56 +02:00
const start = new Date(window.store.get('start'));
const end = new Date(window.store.get('end'));
2024-04-28 09:54:28 +02:00
// TODO cache key is hard coded, problem?
const boxesCacheKey = getCacheKey('ds_boxes_data', {start: start, end: end});
2024-04-20 16:18:41 +02:00
cleanupCache();
2023-10-01 07:17:56 +02:00
2025-03-23 15:04:38 +01:00
//const cacheValid = window.store.get('cacheValid');
2023-10-01 07:17:56 +02:00
let cachedData = window.store.get(boxesCacheKey);
2025-03-23 15:04:38 +01:00
const cacheValid = false; // force refresh
2023-08-27 07:45:09 +02:00
if (cacheValid && typeof cachedData !== 'undefined') {
this.boxData = cachedData;
this.generateOptions(this.boxData);
return;
}
2023-07-23 07:10:31 +02:00
// get stuff
let getter = new Summary();
getter.get(format(start, 'yyyy-MM-dd'), format(end, 'yyyy-MM-dd'), null).then((response) => {
2023-08-06 18:33:29 +02:00
this.boxData = response.data;
2023-10-01 07:17:56 +02:00
window.store.set(boxesCacheKey, response.data);
2023-08-06 18:33:29 +02:00
this.generateOptions(this.boxData);
});
},
generateOptions(data) {
this.balanceBox = {amounts: [], subtitles: []};
this.billBox = {paid: [], unpaid: []};
this.leftBox = {left: [], perDay: []};
this.netBox = {net: []};
let subtitles = {};
2023-07-23 07:10:31 +02:00
2023-08-06 18:33:29 +02:00
// process new content:
for (const i in data) {
if (data.hasOwnProperty(i)) {
const current = data[i];
2023-10-01 11:32:08 +02:00
if (!current.hasOwnProperty('key')) {
continue;
}
2023-08-06 18:33:29 +02:00
let key = current.key;
2025-07-24 06:55:53 +02:00
// console.log('NOT NATIVE');
2025-03-25 17:28:12 +01:00
if (key.startsWith('balance-in-')) {
this.balanceBox.amounts.push(formatMoney(current.monetary_value, current.currency_code));
continue;
}
// spent info is used in subtitle:
if (key.startsWith('spent-in-')) {
// prep subtitles (for later)
if (!subtitles.hasOwnProperty(current.currency_code)) {
subtitles[current.currency_code] = '';
}
// append the amount spent.
subtitles[current.currency_code] =
subtitles[current.currency_code] +
formatMoney(current.monetary_value, current.currency_code);
continue;
}
// earned info is used in subtitle:
if (key.startsWith('earned-in-')) {
// prep subtitles (for later)
if (!subtitles.hasOwnProperty(current.currency_code)) {
subtitles[current.currency_code] = '';
}
// prepend the amount earned.
subtitles[current.currency_code] =
formatMoney(current.monetary_value, current.currency_code) + ' + ' +
subtitles[current.currency_code];
continue;
2023-08-06 18:33:29 +02:00
}
2023-08-06 11:21:20 +02:00
2023-07-23 07:10:31 +02:00
2025-03-25 17:28:12 +01:00
if (key.startsWith('bills-unpaid-in-')) {
this.billBox.unpaid.push(formatMoney(current.monetary_value, current.currency_code));
continue;
}
if (key.startsWith('bills-paid-in-')) {
this.billBox.paid.push(formatMoney(current.monetary_value, current.currency_code));
continue;
}
if (key.startsWith('left-to-spend-in-')) {
this.leftBox.left.push(formatMoney(current.monetary_value, current.currency_code));
continue;
}
if (key.startsWith('left-per-day-to-spend-in-')) {
this.leftBox.perDay.push(formatMoney(current.monetary_value, current.currency_code));
continue;
}
if (key.startsWith('net-worth-in-')) {
this.netBox.net.push(formatMoney(current.monetary_value, current.currency_code));
2023-08-06 11:21:20 +02:00
}
}
2023-08-06 18:33:29 +02:00
}
for (let i in subtitles) {
if (subtitles.hasOwnProperty(i)) {
this.balanceBox.subtitles.push(subtitles[i]);
2023-07-23 07:10:31 +02:00
}
2023-08-06 18:33:29 +02:00
}
2023-08-12 07:53:11 +02:00
this.loading = false;
2023-08-06 18:33:29 +02:00
},
loadBoxes() {
if (true === this.loading) {
return;
}
this.loading = true;
if (null === this.boxData) {
this.getFreshData();
2023-08-12 07:53:11 +02:00
return;
2023-08-06 18:33:29 +02:00
}
2023-08-12 07:53:11 +02:00
this.generateOptions(this.boxData);
2023-08-06 18:33:29 +02:00
this.loading = false;
2023-07-22 16:42:33 +02:00
},
// Getter
init() {
2023-08-12 07:53:11 +02:00
// console.log('boxes init');
2024-04-28 09:54:28 +02:00
// TODO can be replaced by "getVariables"
2025-07-26 19:17:26 +02:00
Promise.all([getVariable('viewRange'), getVariable('convert_to_native', false)]).then((values) => {
2023-08-12 07:53:11 +02:00
// console.log('boxes after promises');
afterPromises = true;
2025-03-23 15:04:38 +01:00
this.convertToNative = values[1];
2023-07-23 07:10:31 +02:00
this.loadBoxes();
});
2023-07-24 18:58:35 +02:00
window.store.observe('end', () => {
2023-08-12 07:53:11 +02:00
if (!afterPromises) {
return;
}
// console.log('boxes observe end');
2023-08-06 18:33:29 +02:00
this.boxData = null;
2023-07-23 07:10:31 +02:00
this.loadBoxes();
2023-07-22 16:42:33 +02:00
});
2025-07-26 19:17:26 +02:00
window.store.observe('convert_to_native', (newValue) => {
2023-08-12 07:53:11 +02:00
if (!afterPromises) {
return;
}
2025-03-23 15:04:38 +01:00
// console.log('boxes observe convertToNative');
this.convertToNative = newValue;
2023-08-06 11:21:20 +02:00
this.loadBoxes();
});
2023-07-22 16:42:33 +02:00
},
});