Feat(Store): Start store and read csv

This commit is contained in:
Bertrand Benjamin 2018-11-30 11:53:50 +01:00
parent f82cc19871
commit ce11d32e68
5 changed files with 143 additions and 11 deletions

View File

@ -1,7 +1,7 @@
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import store from './store/index'
import BootstrapVue from 'bootstrap-vue'
import 'bootstrap/dist/css/bootstrap.css'
@ -17,8 +17,6 @@ library.add(
faCar, faChargingStation, faUtensils, faFileInvoiceDollar
)
console.log(library.definitions.fas)
Vue.component('font-awesome-icon', FontAwesomeIcon)
Vue.use(BootstrapVue)

View File

@ -1,16 +1,11 @@
import Vue from 'vue'
import Vuex from 'vuex'
import configModule from './modules/config'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
},
mutations: {
},
actions: {
modules: {
config: configModule
}
})

53
src/store/modules/< Normal file
View File

@ -0,0 +1,53 @@
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 })
}
})
}
}
}

View File

@ -0,0 +1,57 @@
import { readdir, readFile } from 'fs'
import path from 'path'
export default {
namespaced: true,
state: {
data_dir: '/home/lafrite/scripts/comptes/data/',
csv_files: [],
datas: []
},
getters: {
data_dir: (state) => {
return state.data_dir
},
csvs: (state) => {
return state.csv_files
},
datas: (state) => {
return state.datas
}
},
mutations: {
APPEND_DATA: (state, { content }) => {
state.datas.push(content)
},
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'
})
for (var i in csvs) {
context.dispatch('load_csv', csvs[i])
}
context.commit('SET_CSV_FILES', { csvs })
}
})
},
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 })
}
})
}
}
}

View File

@ -1,5 +1,34 @@
<template>
<div class="import">
<h1>Analyse</h1>
<p>{{ data_dir }}</p>
<p>{{ csvs }}</p>
<p>{{ datas }}</p>
</div>
</template>
<script>
import { mapGetters } from 'vuex'
export default {
name: 'analysis',
components: {
},
data () {
return {
}
},
mounted: function () {
this.$store.dispatch('config/find_csv')
},
computed: {
...mapGetters({
'csvs': 'config/csvs',
'data_dir': 'config/data_dir',
'datas': 'config/datas'
})
},
methods: {
}
}
</script>