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

223 lines
7.5 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
2021-04-18 12:29:42 +02:00
import startOfDay from "date-fns/startOfDay";
import endOfDay from 'date-fns/endOfDay'
import startOfWeek from 'date-fns/startOfWeek'
import endOfWeek from 'date-fns/endOfWeek'
import startOfQuarter from 'date-fns/startOfQuarter';
import endOfQuarter from 'date-fns/endOfQuarter';
import endOfMonth from "date-fns/endOfMonth";
import startOfMonth from 'date-fns/startOfMonth';
2021-02-12 20:15:07 +01:00
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
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) {
2021-04-17 20:53:42 +02:00
// console.log('initialiseStore');
// restore from local storage:
context.dispatch('restoreViewRange');
axios.get('./api/v1/preferences/viewRange')
.then(response => {
let viewRange = response.data.data.attributes.data;
let oldViewRange = context.getters.viewRange;
context.commit('setViewRange', viewRange);
if (viewRange !== oldViewRange) {
// console.log('View range changed from "' + oldViewRange + '" to "' + viewRange + '"');
2021-02-12 20:15:07 +01:00
context.dispatch('setDatesFromViewRange');
}
2021-04-18 12:29:42 +02:00
if (viewRange === oldViewRange) {
2021-04-17 20:53:42 +02:00
// console.log('Restore view range dates');
context.dispatch('restoreViewRangeDates');
}
}
).catch(() => {
context.commit('setViewRange', '1M');
context.dispatch('setDatesFromViewRange');
});
2021-02-12 20:15:07 +01:00
},
2021-04-18 12:29:42 +02:00
restoreViewRangeDates: function (context) {
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-04-17 20:53:42 +02:00
},
restoreViewRange: function (context) {
// console.log('restoreViewRange');
let viewRange = localStorage.getItem('viewRange');
if (null !== viewRange) {
// console.log('restored restoreViewRange ' + viewRange );
context.commit('setViewRange', viewRange);
2021-02-12 20:15:07 +01:00
}
2021-04-17 20:53:42 +02:00
},
setDatesFromViewRange(context) {
2021-02-12 20:15:07 +01:00
let start;
let end;
let viewRange = context.getters.viewRange;
2021-04-18 12:29:42 +02:00
let today = new Date;
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':
2021-04-18 12:29:42 +02:00
// today:
start = startOfDay(today);
end = endOfDay(today);
2021-02-12 20:15:07 +01:00
break;
case '1W':
// this week:
2021-04-18 12:29:42 +02:00
start = startOfDay(startOfWeek(today, {weekStartsOn: 1}));
end = endOfDay(endOfWeek(today, {weekStartsOn: 1}));
2021-02-12 20:15:07 +01:00
break;
case '1M':
// this month:
2021-04-18 12:29:42 +02:00
start = startOfDay(startOfMonth(today));
end = endOfDay(endOfMonth(today));
2021-02-12 20:15:07 +01:00
break;
case '3M':
// this quarter
2021-04-18 12:29:42 +02:00
start = startOfDay(startOfQuarter(today));
end = endOfDay(endOfQuarter(today));
2021-02-12 20:15:07 +01:00
break;
case '6M':
// this half-year
2021-04-18 12:29:42 +02:00
if (today.getMonth() <= 5) {
start = new Date(today);
start.setMonth(0);
start.setDate(1);
start = startOfDay(start);
end = new Date(today);
end.setMonth(5);
end.setDate(30);
end = endOfDay(start);
}
if (today.getMonth() > 5) {
start = new Date(today);
start.setMonth(6);
start.setDate(1);
start = startOfDay(start);
end = new Date(today);
end.setMonth(11);
end.setDate(31);
end = endOfDay(start);
}
2021-02-12 20:15:07 +01:00
break;
case '1Y':
// this year
2021-04-18 12:29:42 +02:00
start = new Date(today);
start.setMonth(0);
start.setDate(1);
start = startOfDay(start);
2021-02-12 20:15:07 +01:00
2021-04-18 12:29:42 +02:00
end = new Date(today);
end.setMonth(11);
end.setDate(31);
end = endOfDay(end);
2021-02-12 20:15:07 +01:00
break;
}
2021-04-19 06:49:58 +02: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;
2021-04-17 20:53:42 +02:00
window.localStorage.setItem('viewRange', range);
2021-02-12 20:15:07 +01:00
}
}
export default {
namespaced: true,
state,
getters,
actions,
mutations
}