Feat: Parse and render csv

This commit is contained in:
Bertrand Benjamin 2018-11-30 12:07:39 +01:00
parent ea872699df
commit 0bba1a9131
3 changed files with 37 additions and 23 deletions

View File

@ -1,7 +1,9 @@
<template> <template>
<div id="app"> <div id="app">
<navbar></navbar> <navbar></navbar>
<router-view/> <div id="content">
<router-view/>
</div>
</div> </div>
</template> </template>
@ -17,6 +19,9 @@ export default {
<style> <style>
#app { #app {
text-align: center;
} }
#content {
padding: 5rem;
}
</style> </style>

View File

@ -1,12 +1,13 @@
import { readdir, readFile } from 'fs' import { readdir, readFile } from 'fs'
import path from 'path' import path from 'path'
import Papa from 'papaparse'
export default { export default {
namespaced: true, namespaced: true,
state: { state: {
data_dir: '/home/lafrite/scripts/comptes/data/', data_dir: '/home/lafrite/scripts/comptes/data/',
csv_files: [], csv_files: [],
datas: [] datas: {}
}, },
getters: { getters: {
data_dir: (state) => { data_dir: (state) => {
@ -20,36 +21,46 @@ export default {
} }
}, },
mutations: { mutations: {
APPEND_DATA: (state, { content }) => {
state.datas.push(content)
},
SET_CSV_FILES: (state, { csvs }) => { SET_CSV_FILES: (state, { csvs }) => {
state.csv_files = csvs state.csv_files = csvs
},
SET_DATA: (state, { data }) => {
state.datas = data
},
APPEND_DATA: (state, { content }) => {
state.datas.push(content)
} }
}, },
actions: { actions: {
async find_csv (context) { async find_csv (context) {
readdir(context.getters.data_dir, (err, list) => { try {
if (err) { readdir(context.getters.data_dir, (err, list) => {
console.log(err) if (err) {
} else { console.log(err)
var csvs = list.filter(x => { } else {
return x.split('.').pop() === 'csv' var csvs = list.filter(x => {
}) return x.split('.').pop() === 'csv'
for (var i in csvs) { })
context.dispatch('load_csv', csvs[i]) 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) { async load_csv (context, csv) {
readFile(path.join(context.getters.data_dir, csv), 'utf8', (err, content) => { readFile(path.join(context.getters.data_dir, csv), 'utf8', (err, content) => {
if (err) { if (err) {
console.log(err) console.log(err)
} else { } else {
context.commit('APPEND_DATA', { content }) var parse_config = {
header: true
}
var parsed = Papa.parse(content, parse_config)
context.commit('SET_DATA', { data: parsed })
} }
}) })
} }

View File

@ -1,9 +1,7 @@
<template> <template>
<div class="import"> <div class="import">
<h1>Analyse</h1> <h1>Analyse</h1>
<p>{{ data_dir }}</p> <b-table striped hover :items="datas.data"></b-table>
<p>{{ csvs }}</p>
<p>{{ datas }}</p>
</div> </div>
</template> </template>