From ce11d32e6841d72e76e147b404db72a4073fc3ef Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Fri, 30 Nov 2018 11:53:50 +0100 Subject: [PATCH] Feat(Store): Start store and read csv --- src/main.js | 4 +-- src/{store.js => store/index.js} | 11 ++---- src/store/modules/< | 53 +++++++++++++++++++++++++++++ src/store/modules/config.js | 57 ++++++++++++++++++++++++++++++++ src/views/analysis.vue | 29 ++++++++++++++++ 5 files changed, 143 insertions(+), 11 deletions(-) rename src/{store.js => store/index.js} (55%) create mode 100644 src/store/modules/< create mode 100644 src/store/modules/config.js diff --git a/src/main.js b/src/main.js index e96bcfd..0e53ee1 100644 --- a/src/main.js +++ b/src/main.js @@ -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) diff --git a/src/store.js b/src/store/index.js similarity index 55% rename from src/store.js rename to src/store/index.js index 3c7424e..c45b454 100644 --- a/src/store.js +++ b/src/store/index.js @@ -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 } }) diff --git a/src/store/modules/< b/src/store/modules/< new file mode 100644 index 0000000..340cbe5 --- /dev/null +++ b/src/store/modules/< @@ -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 }) + } + }) + } + } +} diff --git a/src/store/modules/config.js b/src/store/modules/config.js new file mode 100644 index 0000000..d423e57 --- /dev/null +++ b/src/store/modules/config.js @@ -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 }) + } + }) + } + } +} diff --git a/src/views/analysis.vue b/src/views/analysis.vue index 219b88b..7047f61 100644 --- a/src/views/analysis.vue +++ b/src/views/analysis.vue @@ -1,5 +1,34 @@ + +