Feat: read data from csv file

This commit is contained in:
Bertrand Benjamin 2021-08-09 10:28:01 +02:00
parent 7ca7af24b9
commit 1a2799e986
3 changed files with 32 additions and 7 deletions

View File

@ -1,6 +1,6 @@
import path from 'path'
import Papa from 'papaparse'
import { writeFile, readFileSync } from 'fs'
import { writeFile } from 'fs'
const config = {
namespaced: true,
@ -22,10 +22,6 @@ const config = {
// load config file at ~/.config/sousmargot/config.json
return context.state.userDir
},
loadData (context) {
// load data in userDir on dataFile
console.log(readFileSync(context.getters.dataFilePath, "utf-8"))
},
writeData (context) {
// overwrite the dataFile with months datas
const months = context.rootGetters['travail/monthsAll']

View File

@ -1,3 +1,5 @@
import { readFile } from 'fs'
import Papa from 'papaparse'
function monthCA(month) {
if (month.ca_react) {
@ -163,6 +165,14 @@ const travail = {
},
},
mutations: {
cleanMonths (state) {
// erase months
state.months = []
},
importMonths(state, months) {
// overwrite months
state.months = months
},
updateMonth(state, { date, month }) {
state.months[date] = month
},
@ -174,6 +184,23 @@ const travail = {
},
},
actions: {
cleanMonths (context) {
context.commit("cleanMonths")
},
loadMonths (context) {
// import all months from storage
readFile(context.rootGetters["config/dataFilePath"], (err, data) => {
if (err) throw err;
const months = Papa.parse(data.toString(), {header: true})
.data.reduce(
(acc, el) => {
acc[el.date] = el;
return acc
}, {})
context.commit("importMonths", months)
})
},
updateMonth(context, { date, month }) {
// update month's datas
if (date in context.state.months) {

View File

@ -39,12 +39,14 @@ export default {
},
methods: {
...mapActions('config', {
'loadData': 'loadData',
'writeData': 'writeData',
}),
...mapActions('travail', {
'loadMonths': 'loadMonths',
}),
},
mounted () {
this.loadData()
this.loadMonths()
},
}
</script>