Dynamic cache key for all data.

This commit is contained in:
James Cole
2023-10-01 07:17:56 +02:00
parent aa629523a0
commit 272c73054e
9 changed files with 75 additions and 202 deletions

View File

@@ -35,26 +35,6 @@ let apiData = [];
const SUBSCRIPTION_CACHE_KEY = 'subscriptions-data-dashboard';
let subscriptionData = {};
/**
*
*/
// function downloadSubscriptions(params) {
// console.log('Downloading page ' + params.page + '...');
// const getter = new Get();
//
// getter.get(params).then((response) => {
// let data = response.data;
// let totalPages = parseInt(data.meta.pagination.total_pages);
// apiData = [...apiData, ...data.data];
// if (totalPages > params.page) {
// params.page++;
// downloadSubscriptions(params);
// }
// console.log('Done! ' + apiData.length + ' items downloaded.');
// });
// }
function downloadSubscriptions(params) {
const getter = new Get();
return getter.get(params)
@@ -199,43 +179,6 @@ function downloadSubscriptions(params) {
}
//
// function refreshSubscriptions() {
// console.log('refreshSubscriptions');
//
// const cacheValid = window.store.get('cacheValid');
// let cachedData = window.store.get(CACHE_KEY);
//
// // if (cacheValid && typeof cachedData !== 'undefined') {
// // // this.drawChart(this.generateOptions(cachedData));
// // // this.loading = false;
// // // return;
// // }
//
// let params = {
// start: format(new Date(window.store.get('start')), 'y-MM-dd'),
// end: format(new Date(window.store.get('end')), 'y-MM-dd'),
// page: 1,
// };
// downloadSubscriptions(params).then(() => {
// console.log('Done with download!');
// console.log(subscriptionData);
// });
//
//
// //
// // getter.paid(params).then((response) => {
// // let paidData = response.data;
// // getter.unpaid(params).then((response) => {
// // let unpaidData = response.data;
// // let chartData = {paid: paidData, unpaid: unpaidData};
// // window.store.set(CACHE_KEY, chartData);
// // this.drawChart(this.generateOptions(chartData));
// // this.loading = false;
// // });
// // });
// }
export default () => ({
loading: false,
@@ -299,8 +242,8 @@ export default () => ({
label: i18n.t('firefly.subscriptions_in_group', {title: groupTitle}),
data: [paidAmount, unpaidAmount],
backgroundColor: [
'rgb(255, 99, 132)',
'rgb(54, 162, 235)',
'rgb(255, 99, 132)',
],
hoverOffset: 4
}]
@@ -323,114 +266,6 @@ export default () => ({
new Chart(document.querySelector(id), config);
},
// loadChart() {
// if (true === this.loading) {
// return;
// }
// this.loading = true;
//
// if (null !== chartData) {
// //this.drawChart(this.generateOptions(chartData));
// //this.loading = false;
//
// }
// //this.getFreshData();
// },
// drawChart(options) {
// if (null !== chart) {
// // chart.data.datasets = options.data.datasets;
// // chart.update();
//
// }
// // chart = new Chart(document.querySelector("#subscriptions-chart"), options);
// },
// getFreshData() {
// const cacheValid = window.store.get('cacheValid');
// let cachedData = window.store.get(CACHE_KEY);
//
// if (cacheValid && typeof cachedData !== 'undefined') {
// this.drawChart(this.generateOptions(cachedData));
// this.loading = false;
// return;
// }
//
//
// const getter = new Get();
// let params = {
// start: format(new Date(window.store.get('start')), 'y-MM-dd'),
// end: format(new Date(window.store.get('end')), 'y-MM-dd')
// };
//
// getter.paid(params).then((response) => {
// let paidData = response.data;
// getter.unpaid(params).then((response) => {
// let unpaidData = response.data;
// let chartData = {paid: paidData, unpaid: unpaidData};
// window.store.set(CACHE_KEY, chartData);
// this.drawChart(this.generateOptions(chartData));
// this.loading = false;
// });
// });
// },
// generateOptions(data) {
// let options = getDefaultChartSettings('pie');
// // console.log(data);
// options.data.labels = [i18n.t('firefly.paid'), i18n.t('firefly.unpaid')];
// options.data.datasets = [];
// let collection = {};
// for (let i in data.paid) {
// if (data.paid.hasOwnProperty(i)) {
// let current = data.paid[i];
// let currencyCode = this.autoConversion ? current.native_code : current.currency_code;
// let amount = this.autoConversion ? current.native_sum : current.sum;
// if (!collection.hasOwnProperty(currencyCode)) {
// collection[currencyCode] = {
// paid: 0,
// unpaid: 0,
// };
// }
// // in case of paid, add to "paid":
// collection[currencyCode].paid += (parseFloat(amount) * -1);
// }
// }
// // unpaid
// for (let i in data.unpaid) {
// if (data.unpaid.hasOwnProperty(i)) {
// let current = data.unpaid[i];
// let currencyCode = this.autoConversion ? current.native_code : current.currency_code;
// let amount = this.autoConversion ? current.native_sum : current.sum;
// if (!collection.hasOwnProperty(currencyCode)) {
// collection[currencyCode] = {
// paid: 0,
// unpaid: 0,
// };
// }
// // console.log(current);
// // in case of paid, add to "paid":
// collection[currencyCode].unpaid += parseFloat(amount);
// }
// }
// for (let currencyCode in collection) {
// if (collection.hasOwnProperty(currencyCode)) {
// let current = collection[currencyCode];
// options.data.datasets.push(
// {
// label: currencyCode,
// data: [current.paid, current.unpaid],
// backgroundColor: [
// 'rgb(54, 162, 235)', // green (paid)
// 'rgb(255, 99, 132)', // red (unpaid_
// ],
// //hoverOffset: 4
// }
// )
// }
// }
//
// return options;
// },
init() {
console.log('subscriptions init');
Promise.all([getVariable('autoConversion', false), getVariable('language', 'en-US')]).then((values) => {