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:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
saved.json
|
@ -17,6 +17,7 @@ Module.register("MMM-chores", {
|
|||||||
|
|
||||||
start() {
|
start() {
|
||||||
this.doUpdate()
|
this.doUpdate()
|
||||||
|
this.sendSocketNotification("CHORES_READY")
|
||||||
},
|
},
|
||||||
|
|
||||||
doUpdate() {
|
doUpdate() {
|
||||||
@ -49,6 +50,17 @@ Module.register("MMM-chores", {
|
|||||||
return wrapper
|
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) {
|
getDateString(date) {
|
||||||
var td = String(date.getDate()).padStart(2, '0')
|
var td = String(date.getDate()).padStart(2, '0')
|
||||||
var tm = String(date.getMonth() + 1).padStart(2, '0')
|
var tm = String(date.getMonth() + 1).padStart(2, '0')
|
||||||
@ -71,11 +83,11 @@ Module.register("MMM-chores", {
|
|||||||
if (id !== undefined) {
|
if (id !== undefined) {
|
||||||
cb.id = id
|
cb.id = id
|
||||||
}
|
}
|
||||||
if (parseInt(localStorage.getItem(cb.id), 10)) {
|
|
||||||
cb.checked = true
|
|
||||||
}
|
|
||||||
cb.onclick = (ev) => {
|
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 numItems = 2
|
||||||
const randVal = Math.floor(Math.random() * 1000)
|
const randVal = Math.floor(Math.random() * 1000)
|
||||||
if (this.config.showConfetti && cb.checked) {
|
if (this.config.showConfetti && cb.checked) {
|
||||||
|
43
node_helper.js
Normal file
43
node_helper.js
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
Reference in New Issue
Block a user