Files
firefly-iii/resources/assets/v4/store/get-variable.js

52 lines
1.7 KiB
JavaScript
Raw Normal View History

2023-07-22 16:42:33 +02:00
/*
* 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 <https://www.gnu.org/licenses/>.
*/
import Get from "../api/preferences/index.js";
export function getVariable(name) {
// 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-23 07:10:31 +02:00
console.log('Get from window');
2023-07-22 16:42:33 +02:00
return Promise.resolve(window[name]);
}
// load from store2, if it's present.
2023-07-23 07:10:31 +02:00
if (window.store.get(name)) {
console.log('Get from store');
return Promise.resolve(window.store.get(name));
2023-07-22 16:42:33 +02:00
}
let getter = (new Get);
return getter.getByName(name).then((response) => {
2023-07-23 07:10:31 +02:00
console.log('Get from API');
2023-07-22 16:42:33 +02:00
return Promise.resolve(parseResponse(name, response));
});
}
function parseResponse(name, response) {
let value = response.data.data.attributes.data;
2023-07-23 07:10:31 +02:00
window.store.set(name, value);
2023-07-22 16:42:33 +02:00
console.log('Store from API');
return value;
}