Replace localstorage with node_helper write to file

Seeing a bunch of weird issues with this data not saving through the
browser/electron, so let's just use a known-good method instead.
This commit is contained in:
2021-04-22 21:26:21 -05:00
parent 7ef497affb
commit 247877805f
3 changed files with 60 additions and 4 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
saved.json

View File

@ -17,6 +17,7 @@ Module.register("MMM-chores", {
start() {
this.doUpdate()
this.sendSocketNotification("CHORES_READY")
},
doUpdate() {
@ -49,6 +50,17 @@ Module.register("MMM-chores", {
return wrapper
},
socketNotificationReceived(notification, payload) {
if (notification === "CHORES_DATA_READ") {
for (const v in payload) {
const elem = document.getElementById(v)
if (elem) {
elem.checked = payload[v] === 1
}
}
}
},
getDateString(date) {
var td = String(date.getDate()).padStart(2, '0')
var tm = String(date.getMonth() + 1).padStart(2, '0')
@ -71,11 +83,11 @@ Module.register("MMM-chores", {
if (id !== undefined) {
cb.id = id
}
if (parseInt(localStorage.getItem(cb.id), 10)) {
cb.checked = true
}
cb.onclick = (ev) => {
localStorage.setItem(ev.target.id, cb.checked ? 1 : 0)
let targetId = ev.target.id
let val = cb.checked ? 1 : 0
this.sendSocketNotification("CHORES_ADD_DATA", { "key": targetId, "val": val })
const numItems = 2
const randVal = Math.floor(Math.random() * 1000)
if (this.config.showConfetti && cb.checked) {

43
node_helper.js Normal file
View File

@ -0,0 +1,43 @@
const NodeHelper = require("node_helper");
const path = require("path");
const fs = require("fs");
const filename = "/saved.json";
var configFilename = path.resolve(__dirname + filename);
module.exports = NodeHelper.create({
start() {
console.log("##### Starting node helper for: " + this.name);
fs.readFile(configFilename, (err, data) => {
if (err) {
if (err.code === 'ENOENT') {
return;
}
throw err;
}
this.saved_data = JSON.parse(data);
});
},
writeToFile(data) {
var json = JSON.stringify(data);
fs.writeFileSync(configFilename, json, "utf8");
},
socketNotificationReceived(notification, payload) {
if (notification === "CHORES_ADD_DATA") {
if (!this.saved_data) {
this.saved_data = {}
}
this.saved_data[payload.key] = payload.val
this.writeToFile(this.saved_data)
} else if (notification === "CHORES_READY") {
if (this.saved_data) {
this.sendSocketNotification("CHORES_DATA_READ", this.saved_data)
}
}
}
});