diff --git a/src/App.vue b/src/App.vue index 82e85aa..90b29d0 100644 --- a/src/App.vue +++ b/src/App.vue @@ -16,7 +16,7 @@ export default { }, mounted: function () { this.$store.dispatch('config/load') - this.$store.dispatch('datas/find_csv') + this.$store.dispatch('datas/load_csvs') } } diff --git a/src/store/modules/datas.js b/src/store/modules/datas.js index bc02e0d..036dbb7 100644 --- a/src/store/modules/datas.js +++ b/src/store/modules/datas.js @@ -15,7 +15,7 @@ export default { getters: { csvs: (state) => { // return array of csv filename - return Object.keys(state.csv) + return Object.values(state.csv) }, rows: (state) => { // return all data stored in csv deleting duplicates rows @@ -23,6 +23,14 @@ export default { .reduce((acc, d) => acc.concat(d), []) )] }, + rows_in: (state, getters) => (csv) => { + // return rows in a specific csv file + if (csv in getters.csvs) { + return state.csv[csv].data + } else { + console.log('Unkown csv file - ', csv) + } + }, present: (state, getters) => { // is there any datas return getters.rows.length > 0 @@ -90,8 +98,8 @@ export default { } }, mutations: { - SET_CSV_FILES: (state, { csvs }) => { - state.csv_files = csvs + CLEAR_DATA: (state) => { + state.csv = {} }, SET_DATA: (state, { filename, data }) => { Vue.set(state.csv, filename, data) @@ -104,7 +112,12 @@ export default { } }, actions: { - find_csv (context) { + load_csvs (context) { + // Clean state.csv then load csvs files + context.commit('CLEAR_DATA') + context.dispatch('find_csvs') + }, + find_csvs (context) { try { readdir(context.rootGetters['config/data_dir'], (err, list) => { if (err) { @@ -116,7 +129,6 @@ export default { for (var i in csvs) { context.dispatch('load_csv', csvs[i]) } - context.commit('SET_CSV_FILES', { csvs }) } }) } catch (e) { @@ -147,6 +159,7 @@ export default { formatDate(row, 'Date') }) + parsed.filename = filename context.commit('SET_DATA', { filename: filename, data: parsed } ) diff --git a/src/views/config.vue b/src/views/config.vue index 5f1b02e..b034d83 100644 --- a/src/views/config.vue +++ b/src/views/config.vue @@ -1,6 +1,11 @@