Feat: Use moment js to manage dates

This commit is contained in:
Bertrand Benjamin 2018-12-03 14:38:03 +01:00
parent fc3bf1eb50
commit 877c269ee4
3 changed files with 18 additions and 21 deletions

View File

@ -1,3 +1,4 @@
import moment from 'moment'
export function appendTag (row, keywords, field = 'Libellé') {
// Append row.tag
// if row.libellé contains one of words and not invert it gets tagged
@ -25,6 +26,10 @@ function strContains (string, words, invert) {
}
}
export function formatDate (row, field = 'Date') {
row[field] = moment(row[field], 'DD/MM/YYYY', true)
}
export function total (row, field = 'Montant') {
var sum = row.map(x => parseFloat(x[field]))
.reduce((sum, x) => sum + x, 0)

View File

@ -1,11 +1,8 @@
import { readdir, readFile } from 'fs'
import path from 'path'
import Papa from 'papaparse'
import { appendTag } from '../../libs/data_processing'
var today = new Date()
var monthAgo = new Date()
monthAgo.setMonth(monthAgo.getMonth() - 1)
import moment from 'moment'
import { appendTag, formatDate } from '../../libs/data_processing'
export default {
namespaced: true,
@ -17,8 +14,8 @@ export default {
fields: []
}
},
start: monthAgo.toISOString().split('T')[0],
end: today.toISOString().split('T')[0]
start: moment().subtract(1, 'months'),
end: moment()
},
getters: {
csvs: (state) => {
@ -34,16 +31,14 @@ export default {
return getters.rows.filter(x => x.Montant < 0)
},
start: (state) => {
return state.start
return state.start.format(moment.HTML5_FMT.DATE)
},
end: (state) => {
return state.end
return state.end.format(moment.HTML5_FMT.DATE)
},
date_filter_rows: (state, getters) => {
var start = new Date(state.start)
var end = new Date(state.end)
return getters.spending_rows.filter(x => {
return (new Date(x.Date) >= start) & (new Date(x.Date) < end)
return (x.Date >= state.start) & (x.Date < state.end)
})
},
tag_filter_rows: (state, getters) => (tags, invert) => {
@ -52,7 +47,7 @@ export default {
if (invert) {
return tags.some(t => {
return row.tags.map(t => t.name.toLowerCase())
.indexOf(t.toLowerCase()) < 0
.ÿEindexOf(t.toLowerCase()) < 0
})
} else {
return tags.every(t => {
@ -126,7 +121,6 @@ export default {
}
},
load_csv (context, csv) {
//readFile(path.join(context.rootGetters['config/data_dir'], csv), 'utf8', (err, content) => {
readFile(path.join(context.rootGetters['config/data_dir'], csv), 'Latin1', (err, content) => {
if (err) {
console.log(err)
@ -144,15 +138,16 @@ export default {
parsed.data = parsed.data.filter(x => x.Libellé !== undefined)
parsed.data.forEach(row => {
appendTag(row, tags, 'Libellé')
formatDate(row, 'Date')
})
context.commit('SET_DATA', { data: parsed })
},
set_start (context, start) {
context.commit('SET_START', { start })
context.commit('SET_START', { start: moment(start)})
},
set_end (context, end) {
context.commit('SET_END', { end })
context.commit('SET_END', { end: moment(end)})
}
}
}

View File

@ -5,7 +5,7 @@
<b-container fluid>
<b-row class="date-selector">
<b-col sm="1"><label for="start"> Entre </label> </b-col>
<b-col sm="3">
<b-col sm="4">
<b-form-input id="start" type="date" :value="start" @input="set_start"></b-form-input>
</b-col>
<b-col sm="1"><label for="end"> et </label></b-col>
@ -66,7 +66,7 @@ export default {
{
key: 'Date',
sortable: true,
formatter: this.table_date_format
formatter: d => d.format('DD/MM/YYYY')
},
{
key: 'Montant',
@ -113,9 +113,6 @@ export default {
]),
update_start (e) {
},
table_date_format (date) {
return date
},
set_tags_filter (tagnames) {
this.tags_filter = tagnames
}