From 0bba1a91315e43cc9071fe7f0a98b1baaf5ac0d0 Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Fri, 30 Nov 2018 12:07:39 +0100 Subject: [PATCH] Feat: Parse and render csv --- src/App.vue | 9 +++++-- src/store/modules/config.js | 47 +++++++++++++++++++++++-------------- src/views/analysis.vue | 4 +--- 3 files changed, 37 insertions(+), 23 deletions(-) diff --git a/src/App.vue b/src/App.vue index c11d6a8..556542b 100644 --- a/src/App.vue +++ b/src/App.vue @@ -1,7 +1,9 @@ @@ -17,6 +19,9 @@ export default { diff --git a/src/store/modules/config.js b/src/store/modules/config.js index d423e57..187f51b 100644 --- a/src/store/modules/config.js +++ b/src/store/modules/config.js @@ -1,12 +1,13 @@ import { readdir, readFile } from 'fs' import path from 'path' +import Papa from 'papaparse' export default { namespaced: true, state: { data_dir: '/home/lafrite/scripts/comptes/data/', csv_files: [], - datas: [] + datas: {} }, getters: { data_dir: (state) => { @@ -20,36 +21,46 @@ export default { } }, mutations: { - APPEND_DATA: (state, { content }) => { - state.datas.push(content) - }, SET_CSV_FILES: (state, { csvs }) => { state.csv_files = csvs + }, + SET_DATA: (state, { data }) => { + state.datas = data + }, + APPEND_DATA: (state, { content }) => { + state.datas.push(content) } - }, 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' - }) - for (var i in csvs) { - context.dispatch('load_csv', csvs[i]) + 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 }) } - 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 { - context.commit('APPEND_DATA', { content }) + var parse_config = { + header: true + } + var parsed = Papa.parse(content, parse_config) + context.commit('SET_DATA', { data: parsed }) } }) } diff --git a/src/views/analysis.vue b/src/views/analysis.vue index 7047f61..d687721 100644 --- a/src/views/analysis.vue +++ b/src/views/analysis.vue @@ -1,9 +1,7 @@