Files
firefly-iii/frontend/src/components/store/modules/dashboard/index.js

219 lines
7.6 KiB
JavaScript
Raw Normal View History

2021-02-12 20:15:07 +01:00
/*
* index.js
* Copyright (c) 2020 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/>.
*/
// initial state
const state = () => (
{
2021-02-14 10:43:58 +01:00
viewRange: 'default',
2021-02-12 20:15:07 +01:00
start: null,
end: null,
2021-02-14 10:43:58 +01:00
// default range:
defaultStart: null,
defaultEnd: null,
2021-02-12 20:15:07 +01:00
}
)
// getters
const getters = {
start: state => {
return state.start;
},
end: state => {
return state.end;
},
2021-02-14 10:43:58 +01:00
defaultStart: state => {
return state.defaultStart;
},
defaultEnd: state => {
return state.defaultEnd;
},
2021-02-12 20:15:07 +01:00
viewRange: state => {
return state.viewRange;
}
}
// actions
const actions = {
initialiseStore(context) {
if ('default' === context.state.viewRange) {
axios.get('./api/v1/preferences/viewRange')
.then(response => {
let viewRange = response.data.data.attributes.data;
context.commit('setViewRange', viewRange);
// call another action:
context.dispatch('setDatesFromViewRange');
}
).catch(error => {
2021-02-13 20:04:18 +01:00
// console.log(error);
2021-02-12 20:15:07 +01:00
context.commit('setViewRange', '1M');
// call another action:
context.dispatch('setDatesFromViewRange');
});
}
},
setDatesFromViewRange(context) {
2021-02-13 20:04:18 +01:00
// console.log('Must set dates from viewRange "' + context.state.viewRange + '"');
2021-02-12 20:15:07 +01:00
// check local storage first?
if (localStorage.viewRangeStart) {
2021-02-14 10:43:58 +01:00
// console.log('view range start set from local storage.');
2021-02-13 20:04:18 +01:00
context.commit('setStart', new Date(localStorage.viewRangeStart));
2021-02-12 20:15:07 +01:00
}
if (localStorage.viewRangeEnd) {
2021-02-14 10:43:58 +01:00
// console.log('view range end set from local storage.');
2021-02-13 20:04:18 +01:00
context.commit('setEnd', new Date(localStorage.viewRangeEnd));
2021-02-12 20:15:07 +01:00
}
2021-02-14 10:43:58 +01:00
// also set default:
2021-02-26 06:39:20 +01:00
if (localStorage.viewRangeDefaultStart) {
2021-02-14 10:43:58 +01:00
// console.log('view range default start set from local storage.');
// console.log(localStorage.viewRangeDefaultStart);
context.commit('setDefaultStart', new Date(localStorage.viewRangeDefaultStart));
}
2021-02-26 06:39:20 +01:00
if (localStorage.viewRangeDefaultEnd) {
2021-02-14 10:43:58 +01:00
// console.log('view range default end set from local storage.');
// console.log(localStorage.viewRangeDefaultEnd);
context.commit('setDefaultEnd', new Date(localStorage.viewRangeDefaultEnd));
}
2021-02-12 20:15:07 +01:00
if (null !== context.getters.end && null !== context.getters.start) {
return;
}
let start;
let end;
let viewRange = context.getters.viewRange;
2021-02-13 20:04:18 +01:00
// console.log('Will recreate view range on ' + viewRange);
2021-02-12 20:15:07 +01:00
switch (viewRange) {
case '1D':
// one day:
start = new Date;
end = new Date(start.getTime());
start.setHours(0, 0, 0, 0);
end.setHours(23, 59, 59, 999);
break;
case '1W':
// this week:
start = new Date;
end = new Date(start.getTime());
// start of week
let diff = start.getDate() - start.getDay() + (start.getDay() === 0 ? -6 : 1);
start.setDate(diff);
start.setHours(0, 0, 0, 0);
// end of week
let lastday = end.getDate() - (end.getDay() - 1) + 6;
end.setDate(lastday);
end.setHours(23, 59, 59, 999);
break;
case '1M':
// this month:
start = new Date;
start = new Date(start.getFullYear(), start.getMonth(), 1);
start.setHours(0, 0, 0, 0);
end = new Date(start.getFullYear(), start.getMonth() + 1, 0);
end.setHours(23, 59, 59, 999);
break;
case '3M':
// this quarter
start = new Date;
end = new Date;
2021-02-13 20:04:18 +01:00
let quarter = Math.floor((start.getMonth() + 3) / 3) - 1;
2021-02-12 20:15:07 +01:00
// start and end months? I'm sure this could be better:
2021-02-13 20:04:18 +01:00
let startMonths = [0, 3, 6, 9];
let endMonths = [2, 5, 8, 11];
2021-02-12 20:15:07 +01:00
// set start to the correct month, day one:
start = new Date(start.getFullYear(), startMonths[quarter], 1);
start.setHours(0, 0, 0, 0);
// set end to the correct month, day one
end = new Date(end.getFullYear(), endMonths[quarter], 1);
// then to the last day of the month:
end = new Date(end.getFullYear(), end.getMonth() + 1, 0);
end.setHours(23, 59, 59, 999);
break;
case '6M':
// this half-year
start = new Date;
end = new Date;
2021-02-13 20:04:18 +01:00
let half = start.getMonth() <= 5 ? 0 : 1;
2021-02-12 20:15:07 +01:00
2021-02-13 20:04:18 +01:00
let startHalf = [0, 6];
let endHalf = [5, 11];
2021-02-12 20:15:07 +01:00
// set start to the correct month, day one:
start = new Date(start.getFullYear(), startHalf[half], 1);
start.setHours(0, 0, 0, 0);
// set end to the correct month, day one
end = new Date(end.getFullYear(), endHalf[half], 1);
// then to the last day of the month:
end = new Date(end.getFullYear(), end.getMonth() + 1, 0);
end.setHours(23, 59, 59, 999);
break;
case '1Y':
// this year
start = new Date;
end = new Date;
start = new Date(start.getFullYear(), 0, 1);
end = new Date(end.getFullYear(), 11, 31);
start.setHours(0, 0, 0, 0);
end.setHours(23, 59, 59, 999);
break;
}
2021-02-13 20:04:18 +01:00
// console.log('Range is ' + viewRange);
// console.log('Start is ' + start);
// console.log('End is ' + end);
2021-02-12 20:15:07 +01:00
context.commit('setStart', start);
context.commit('setEnd', end);
2021-02-14 10:43:58 +01:00
context.commit('setDefaultStart', start);
context.commit('setDefaultEnd', end);
2021-02-12 20:15:07 +01:00
}
}
// mutations
const mutations = {
setStart(state, value) {
state.start = value;
2021-02-13 20:04:18 +01:00
window.localStorage.setItem('viewRangeStart', value);
2021-02-12 20:15:07 +01:00
},
setEnd(state, value) {
state.end = value;
2021-02-13 20:04:18 +01:00
window.localStorage.setItem('viewRangeEnd', value);
2021-02-12 20:15:07 +01:00
},
2021-02-14 10:43:58 +01:00
setDefaultStart(state, value) {
state.defaultStart = value;
window.localStorage.setItem('viewRangeDefaultStart', value);
},
setDefaultEnd(state, value) {
state.defaultEnd = value;
window.localStorage.setItem('viewRangeDefaultEnd', value);
},
2021-02-12 20:15:07 +01:00
setViewRange(state, range) {
state.viewRange = range;
}
}
export default {
namespaced: true,
state,
getters,
actions,
mutations
}