diff --git a/src/store/index.js b/src/store/index.js index c45b454..c2e64e0 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -1,11 +1,13 @@ import Vue from 'vue' import Vuex from 'vuex' import configModule from './modules/config' +import datasModule from './modules/datas' Vue.use(Vuex) export default new Vuex.Store({ modules: { - config: configModule + config: configModule, + datas: datasModule } }) diff --git a/src/store/modules/< b/src/store/modules/< deleted file mode 100644 index 340cbe5..0000000 --- a/src/store/modules/< +++ /dev/null @@ -1,53 +0,0 @@ -import { readdir, readFile } from 'fs' - -export default { - namespaced: true, - state: { - data_dir: '/home/lafrite/scripts/comptes/data/', - csv_files: [], - data: [] - }, - getters: { - data_dir: (state) => { - return state.data_dir - }, - csvs: (state) => { - return state.csv_files - }, - data: (state) => { - return state.data - } - }, - mutations: { - APPEND_DATA: (state, { data }) => { - state.data.push(data) - }, - SET_CSV_FILES: (state, { csvs }) => { - state.csv_files = csvs - } - - }, - actions: { - async find_csv (context) { - readdir(context.getters.data_dir, (err, list) => { - if (err) { - console.log(err) - } else { - var csvs = list.filter(x => { - return x.split('.').pop() == 'csv' - }) - context.commit('SET_CSV_FILES', { csvs }) - } - }) - }, - async load_csv (context, csv) { - readFile(path.join(context.getters.data_dir, csv) , (err, content) => { - if (err) { - console.log(err) - } else { - context.commit('APPEND_DATA', { content }) - } - }) - } - } -} diff --git a/src/store/modules/config.js b/src/store/modules/config.js index 53cd0f7..ed18905 100644 --- a/src/store/modules/config.js +++ b/src/store/modules/config.js @@ -1,99 +1,15 @@ -import Vue from 'vue' -import { readdir, readFile } from 'fs' -import path from 'path' -import Papa from 'papaparse' - -var today = new Date() -var monthAgo = new Date() -monthAgo.setMonth(monthAgo.getMonth() - 1) - export default { namespaced: true, state: { - data_dir: '/home/lafrite/scripts/comptes/data/', - csv_files: [], - rows: {}, - dates_boundaries: [monthAgo, today] + data_dir: '/home/lafrite/scripts/comptes/data/' }, getters: { data_dir: (state) => { return state.data_dir - }, - csvs: (state) => { - return state.csv_files - }, - rows: (state) => { - return state.rows.data - }, - dates_boundaries: (state) => { - return state.dates_boundaries.map(x => x.toISOString().split('T')[0]) - }, - filter_rows: (state, getters) => { - if (state.rows.data) { - return state.rows.data.filter(x => { - return (new Date(x.Date) >= state.dates_boundaries[0]) & (new Date(x.Date) < state.dates_boundaries[1]) - }) - } else { - return [] - } } - }, mutations: { - SET_CSV_FILES: (state, { csvs }) => { - state.csv_files = csvs - }, - SET_DATA: (state, { data }) => { - state.rows = data - }, - APPEND_DATA: (state, { content }) => { - state.rows.push(content) - }, - SET_START_DATE: (state, { start }) => { - Vue.set(state.dates_boundaries, 0, new Date(start)) - }, - SET_END_DATE: (state, { end }) => { - Vue.set(state.dates_boundaries, 1, new Date(end)) - } }, actions: { - async find_csv (context) { - try { - readdir(context.getters.data_dir, (err, list) => { - if (err) { - console.log(err) - } else { - var csvs = list.filter(x => { - return x.split('.').pop() === 'csv' - }) - for (var i in csvs) { - context.dispatch('load_csv', csvs[i]) - } - context.commit('SET_CSV_FILES', { csvs }) - } - }) - } catch (e) { - console.log(e) - } - }, - async load_csv (context, csv) { - readFile(path.join(context.getters.data_dir, csv), 'utf8', (err, content) => { - if (err) { - console.log(err) - } else { - var parseConfig = { - header: true - } - var parsed = Papa.parse(content, parseConfig) - context.commit('SET_DATA', { data: parsed }) - } - }) - }, - set_start_date (context, start) { - context.commit('SET_START_DATE', { start }) - }, - set_end_date (context, end) { - context.commit('SET_END_DATE', { end }) - } } } diff --git a/src/store/modules/datas.js b/src/store/modules/datas.js new file mode 100644 index 0000000..d8cb0c6 --- /dev/null +++ b/src/store/modules/datas.js @@ -0,0 +1,95 @@ +import Vue from 'vue' +import { readdir, readFile } from 'fs' +import path from 'path' +import Papa from 'papaparse' + +var today = new Date() +var monthAgo = new Date() +monthAgo.setMonth(monthAgo.getMonth() - 1) + +export default { + namespaced: true, + state: { + csv_files: [], + rows: {}, + dates_boundaries: [monthAgo, today] + }, + getters: { + csvs: (state) => { + return state.csv_files + }, + rows: (state) => { + return state.rows.data + }, + dates_boundaries: (state) => { + return state.dates_boundaries.map(x => x.toISOString().split('T')[0]) + }, + filter_rows: (state, getters) => { + if (state.rows.data) { + return state.rows.data.filter(x => { + return (new Date(x.Date) >= state.dates_boundaries[0]) & (new Date(x.Date) < state.dates_boundaries[1]) + }) + } else { + return [] + } + } + + }, + mutations: { + SET_CSV_FILES: (state, { csvs }) => { + state.csv_files = csvs + }, + SET_DATA: (state, { data }) => { + state.rows = data + }, + APPEND_DATA: (state, { content }) => { + state.rows.push(content) + }, + SET_START_DATE: (state, { start }) => { + Vue.set(state.dates_boundaries, 0, new Date(start)) + }, + SET_END_DATE: (state, { end }) => { + Vue.set(state.dates_boundaries, 1, new Date(end)) + } + }, + actions: { + async find_csv (context) { + try { + readdir(context.rootGetters['config/data_dir'], (err, list) => { + if (err) { + console.log(err) + } else { + var csvs = list.filter(x => { + return x.split('.').pop() === 'csv' + }) + for (var i in csvs) { + context.dispatch('load_csv', csvs[i]) + } + context.commit('SET_CSV_FILES', { csvs }) + } + }) + } catch (e) { + console.log(e) + } + }, + async load_csv (context, csv) { + readFile(path.join(context.rootGetters['config/data_dir'], csv), 'utf8', (err, content) => { + if (err) { + console.log(err) + } else { + var parseConfig = { + header: true + } + var parsed = Papa.parse(content, parseConfig) + context.commit('SET_DATA', { data: parsed }) + } + }) + }, + set_start_date (context, start) { + context.commit('SET_START_DATE', { start }) + }, + set_end_date (context, end) { + context.commit('SET_END_DATE', { end }) + } + } +} diff --git a/src/views/home.vue b/src/views/home.vue index 28d7cbf..d25d839 100644 --- a/src/views/home.vue +++ b/src/views/home.vue @@ -38,19 +38,18 @@ export default { key: 'Libellé', sortable: true } - ], + ], spending: true } }, mounted: function () { - this.$store.dispatch('config/find_csv') + this.$store.dispatch('datas/find_csv') }, computed: { ...mapGetters({ - 'csvs': 'config/csvs', - 'data_dir': 'config/data_dir', - 'rows': 'config/filter_rows', - 'date': 'config/dates_boundaries' + 'csvs': 'datas/csvs', + 'rows': 'datas/filter_rows', + 'date': 'datas/dates_boundaries' }), date_start () { return this.date[0] @@ -68,10 +67,10 @@ export default { }, methods: { update_date_start (e) { - this.$store.dispatch('config/set_start_date', e) + this.$store.dispatch('datas/set_start_date', e) }, update_date_end (e) { - this.$store.dispatch('config/set_end_date', e) + this.$store.dispatch('datas/set_end_date', e) }, } }