Feat(config): Reloading csv files

This commit is contained in:
Bertrand Benjamin 2019-01-21 11:41:47 +01:00
parent 892d74a24d
commit 1c7a2556c4
3 changed files with 29 additions and 10 deletions

View File

@ -16,7 +16,7 @@ export default {
}, },
mounted: function () { mounted: function () {
this.$store.dispatch('config/load') this.$store.dispatch('config/load')
this.$store.dispatch('datas/find_csv') this.$store.dispatch('datas/load_csvs')
} }
} }
</script> </script>

View File

@ -15,7 +15,7 @@ export default {
getters: { getters: {
csvs: (state) => { csvs: (state) => {
// return array of csv filename // return array of csv filename
return Object.keys(state.csv) return Object.values(state.csv)
}, },
rows: (state) => { rows: (state) => {
// return all data stored in csv deleting duplicates rows // return all data stored in csv deleting duplicates rows
@ -23,6 +23,14 @@ export default {
.reduce((acc, d) => acc.concat(d), []) .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) => { present: (state, getters) => {
// is there any datas // is there any datas
return getters.rows.length > 0 return getters.rows.length > 0
@ -90,8 +98,8 @@ export default {
} }
}, },
mutations: { mutations: {
SET_CSV_FILES: (state, { csvs }) => { CLEAR_DATA: (state) => {
state.csv_files = csvs state.csv = {}
}, },
SET_DATA: (state, { filename, data }) => { SET_DATA: (state, { filename, data }) => {
Vue.set(state.csv, filename, data) Vue.set(state.csv, filename, data)
@ -104,7 +112,12 @@ export default {
} }
}, },
actions: { 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 { try {
readdir(context.rootGetters['config/data_dir'], (err, list) => { readdir(context.rootGetters['config/data_dir'], (err, list) => {
if (err) { if (err) {
@ -116,7 +129,6 @@ export default {
for (var i in csvs) { for (var i in csvs) {
context.dispatch('load_csv', csvs[i]) context.dispatch('load_csv', csvs[i])
} }
context.commit('SET_CSV_FILES', { csvs })
} }
}) })
} catch (e) { } catch (e) {
@ -147,6 +159,7 @@ export default {
formatDate(row, 'Date') formatDate(row, 'Date')
}) })
parsed.filename = filename
context.commit('SET_DATA', context.commit('SET_DATA',
{ filename: filename, data: parsed } { filename: filename, data: parsed }
) )

View File

@ -1,6 +1,11 @@
<template> <template>
<div class="tags"> <div class="tags">
<h1>Fichiers CSV</h1> <h1>
Fichiers CSV
<b-button @click="reload_csvs">
<font-awesome-icon icon="sync-alt" class="fa" />
</b-button>
</h1>
<p> <p>
Les fichiers csv sont cherché dans <span class="datadir">{{ data_dir }}</span> Les fichiers csv sont cherché dans <span class="datadir">{{ data_dir }}</span>
<!-- <!--
@ -8,7 +13,7 @@
--> -->
<b-list-group> <b-list-group>
<b-list-group-item v-for="csv in csvs"> <b-list-group-item v-for="csv in csvs">
{{ csv }} {{ csv.filename }} {{ csv.data.length }}
</b-list-group-item> </b-list-group-item>
</b-list-group> </b-list-group>
</p> </p>
@ -46,8 +51,9 @@ export default {
}) })
}, },
methods: { methods: {
...mapActions('datas', [ ...mapActions('datas', {
]), 'reload_csvs': 'load_csvs'
}),
open_filebrowser (dir) { open_filebrowser (dir) {
console.log("plop") console.log("plop")
} }