Sousmargot/src/store/config/index.js

44 lines
1.3 KiB
JavaScript

import path from 'path'
import Papa from 'papaparse'
import { writeFile } from 'fs'
const config = {
namespaced: true,
state() {
return {
//userDir: '~/.config/sousmargot/',
userDir: './userDir/',
dataFile: 'datas.csv',
caProPercentage: 0.5,
}
},
getters: {
userDir (state) { return state.userDir },
dataFilePath (state) { return path.join(state.userDir, state.dataFile) },
caProPercentage (state) { return state.caProPercentage },
},
mutations: {
},
actions: {
loadConfig (context) {
// load config file at ~/.config/sousmargot/config.json
return context.state.userDir
},
writeData (context) {
// overwrite the dataFile with months datas
const months = context.rootGetters['travail/monthsAll']
const unpackMonths = Object.keys(months).map(k => {return { ...months[k], date: k}})
const csv = Papa.unparse(unpackMonths)
writeFile(context.getters.dataFilePath, csv, (err) => {
if (err) {
console.log(err)
} else {
console.log("Datas sauvegardées")
}
})
},
},
}
export default config