From cd313264af40f537d101d9a57e8dcadd81a7c792 Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Thu, 24 Jan 2019 11:50:27 +0100 Subject: [PATCH] Feat(home): month selector --- src/store/modules/datas.js | 50 ++++++++++++++++++-------------------- src/views/home.vue | 36 ++++++++++++--------------- 2 files changed, 38 insertions(+), 48 deletions(-) diff --git a/src/store/modules/datas.js b/src/store/modules/datas.js index 2e64b28..3eb8250 100644 --- a/src/store/modules/datas.js +++ b/src/store/modules/datas.js @@ -9,20 +9,21 @@ export default { namespaced: true, state: { csv: {}, - start: moment().subtract(1, 'months'), - end: moment() + month: moment(), }, getters: { csvs: (state) => { // return array of csv filename return Object.values(state.csv).sort((a, b) => { - var filenameA=a.filename.toLowerCase(), filenameB=b.filename.toLowerCase(); - if (filenameA < filenameB) //sort string ascending - return -1; - if (filenameA > filenameB) - return 1; - return 0; //default return value (no sorting) - }); + var filenameA=a.filename.toLowerCase(), filenameB=b.filename.toLowerCase() + if (filenameA < filenameB) { + return -1 + } + if (filenameA > filenameB) { + return 1 + } + return 0 + }) }, rows: (state) => { // return all data stored in csv deleting duplicates rows @@ -46,18 +47,14 @@ export default { // return data with negatives 'Montant' return getters.rows.filter(x => x.Montant < 0) }, - start: (state) => { - // Start date - return state.start.format(moment.HTML5_FMT.DATE) - }, - end: (state) => { - // End date - return state.end.format(moment.HTML5_FMT.DATE) + month: (state) => { + // month date + return state.month }, date_filter_rows: (state, getters) => { // return rows filtered by date return getters.spending_rows.filter(x => { - return (x.Date >= state.start) & (x.Date < state.end) + return moment(x.Date).isSame(state.month, 'month') }) }, tag_filter_rows: (state, getters) => (tags, invert) => { @@ -111,11 +108,8 @@ export default { SET_DATA: (state, { filename, data }) => { Vue.set(state.csv, filename, data) }, - SET_START: (state, { start }) => { - state.start = start - }, - SET_END: (state, { end }) => { - state.end = end + SET_MONTH: (state, { month }) => { + state.month = month } }, actions: { @@ -171,11 +165,13 @@ export default { { filename: filename, data: parsed } ) }, - set_start (context, start) { - context.commit('SET_START', { start: moment(start) }) + next_month (context) { + var next = moment(context.getters.month).add(1, 'months') + context.commit('SET_MONTH', { month: next }) + }, + prev_month (context) { + var prev = moment(context.getters.month).subtract(1, 'months') + context.commit('SET_MONTH', { month: prev }) }, - set_end (context, end) { - context.commit('SET_END', { end: moment(end) }) - } } } diff --git a/src/views/home.vue b/src/views/home.vue index ed65b70..ef5c80b 100644 --- a/src/views/home.vue +++ b/src/views/home.vue @@ -4,17 +4,15 @@
- - + + + + + {{ month.format('MMMM YYYY') }} + + + + @@ -58,6 +56,8 @@ import box from '../components/box' import tagsComparison from '../components/tags_comparison' import moment from 'moment' +moment.locale('fr') + export default { name: 'home', components: { @@ -97,27 +97,21 @@ export default { 'csvs': 'datas/csvs', 'tag_filter_rows': 'datas/tag_filter_rows', 'datas_present': 'datas/present', - 'start': 'datas/start', - 'end': 'datas/end', + 'month': 'datas/month', 'tags': 'config/tags' }), filtered_rows () { return this.tag_filter_rows(this.tags_filter) - } - + }, }, methods: { ...mapActions('datas', [ - 'set_start', - 'set_end' + 'next_month', + 'prev_month', ]), set_tags_filter (tagnames) { this.tags_filter = tagnames }, - set_month (month) { - this.set_start(moment(month).startOf("month").toDate()) - this.set_end(moment(month).endOf("month").toDate()) - } } }