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

@@ -21,6 +21,8 @@ import {getVariable} from "../../store/get-variable.js";
import Get from "../../api/v2/model/piggy-bank/get.js";
import {I18n} from "i18n-js";
import {loadTranslations} from "../../support/load-translations.js";
import {getCacheKey} from "../../support/get-cache-key.js";
import {format} from "date-fns";
let apiData = {};
let afterPromises = false;
@@ -33,8 +35,12 @@ export default () => ({
sankeyGrouping: 'account',
piggies: [],
getFreshData() {
const start = new Date(window.store.get('start'));
const end = new Date(window.store.get('end'));
const cacheKey = getCacheKey(CACHE_KEY, start, end);
const cacheValid = window.store.get('cacheValid');
let cachedData = window.store.get(CACHE_KEY);
let cachedData = window.store.get(cacheKey);
if (cacheValid && typeof cachedData !== 'undefined') {
apiData = cachedData;
@@ -44,14 +50,16 @@ export default () => ({
}
let params = {
start: window.store.get('start').slice(0, 10),
end: window.store.get('end').slice(0, 10),
start: format(start, 'y-MM-dd'),
end: format(end, 'y-MM-dd'),
page: 1
};
this.downloadPiggyBanks(params);
},
downloadPiggyBanks(params) {
// console.log('Downloading page ' + params.page + '...');
const start = new Date(window.store.get('start'));
const end = new Date(window.store.get('end'));
const cacheKey = getCacheKey(CACHE_KEY, start, end);
const getter = new Get();
getter.get(params).then((response) => {
apiData = [...apiData, ...response.data.data];
@@ -60,7 +68,7 @@ export default () => ({
this.downloadPiggyBanks(params);
return;
}
window.store.set(CACHE_KEY, apiData);
window.store.set(cacheKey, apiData);
this.parsePiggies();
this.loading = false;
});