diff --git a/src/store/config/index.js b/src/store/config/index.js index 8ec3ae3..1513ee9 100644 --- a/src/store/config/index.js +++ b/src/store/config/index.js @@ -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'] diff --git a/src/store/travail/index.js b/src/store/travail/index.js index 4224efe..1b9b85e 100644 --- a/src/store/travail/index.js +++ b/src/store/travail/index.js @@ -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) { diff --git a/src/views/home.vue b/src/views/home.vue index 4b583b5..3f84d10 100644 --- a/src/views/home.vue +++ b/src/views/home.vue @@ -39,12 +39,14 @@ export default { }, methods: { ...mapActions('config', { - 'loadData': 'loadData', 'writeData': 'writeData', }), + ...mapActions('travail', { + 'loadMonths': 'loadMonths', + }), }, mounted () { - this.loadData() + this.loadMonths() }, }