Files
firefly-iii/resources/assets/v4/store/Basic.js

103 lines
2.7 KiB
JavaScript
Raw Normal View History

2023-07-11 11:45:55 +02:00
// basic store for preferred date range and some other vars.
// used in layout.
import Get from '../api/preferences/index.js';
2023-07-15 15:25:04 +02:00
import store from 'store2';
2023-07-22 16:42:33 +02:00
2023-07-15 15:25:04 +02:00
/**
* A basic store for Firefly III persistent UI data and preferences.
*/
2023-07-22 16:42:33 +02:00
const Basic = () => {
2023-07-15 15:25:04 +02:00
// currently availabel variables:
2023-07-22 16:42:33 +02:00
const viewRange = '1M';
const darkMode = 'browser';
const language = 'en-US';
const locale = 'en-US';
2023-07-15 15:25:04 +02:00
// start and end are used by most pages to allow the user to browse back and forth.
2023-07-22 16:42:33 +02:00
const start = null;
const end = null;
2023-07-14 06:04:48 +02:00
// others, to be used in the future.
2023-07-22 16:42:33 +02:00
const listPageSize = 10;
const currencyCode = 'AAA';
const currencyId = '0';
const ready = false;
//
2023-07-15 15:25:04 +02:00
// a very basic way to signal the store now contains all variables.
2023-07-22 16:42:33 +02:00
const count = 0;
const readyCount = 4;
2023-07-15 15:25:04 +02:00
/**
*
*/
2023-07-22 16:42:33 +02:00
const init = () => {
console.log('Basic store init')
2023-07-11 11:45:55 +02:00
this.loadVariable('viewRange')
this.loadVariable('darkMode')
this.loadVariable('language')
this.loadVariable('locale')
2023-07-11 11:45:55 +02:00
}
2023-07-15 15:25:04 +02:00
/**
* Load a variable, fresh or from storage.
* @param name
*/
2023-07-22 16:42:33 +02:00
const loadVariable = (name) => {
2023-07-15 15:25:04 +02:00
// 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.
if (window.hasOwnProperty(name)) {
2023-07-11 11:45:55 +02:00
this[name] = window[name];
2023-07-15 15:25:04 +02:00
this.triggerReady();
2023-07-11 11:45:55 +02:00
return;
}
2023-07-15 15:25:04 +02:00
// load from store2
if (store.has(name)) {
this[name] = store.get(name);
this.triggerReady();
2023-07-11 11:45:55 +02:00
return;
}
// grab
let getter = (new Get);
getter.getByName(name).then((response) => this.parseResponse(name, response));
}
2023-07-22 16:42:33 +02:00
//
const parseResponse = (name, response) => {
let value = response.data.data.attributes.data;
this[name] = value;
2023-07-22 16:42:33 +02:00
// TODO store.
2023-07-15 15:25:04 +02:00
store.set(name, value);
this.triggerReady();
2023-07-11 11:45:55 +02:00
}
2023-07-22 16:42:33 +02:00
//
// set(name, value) {
// this[name] = value;
// store.set(name, value);
// }
//
// get(name) {
// return store.get(name, this[name]);
// }
//
const isReady = () => {
2023-07-14 06:04:48 +02:00
return this.count === this.readyCount;
}
2023-07-15 15:25:04 +02:00
2023-07-22 16:42:33 +02:00
const triggerReady = () => {
2023-07-15 15:25:04 +02:00
this.count++;
if (this.count === this.readyCount) {
console.log('Basic store is ready!')
// trigger event:
const event = new Event("BasicStoreReady");
document.dispatchEvent(event);
}
}
2023-07-22 16:42:33 +02:00
return {
init
};
2023-07-11 11:45:55 +02:00
}
2023-07-22 16:42:33 +02:00
export const basic = Basic();