Skip to content

Commit

Permalink
nightscout#6701 Save report preferences
Browse files Browse the repository at this point in the history
Proof of concept to demonstrate saving report preferences
  • Loading branch information
mg80 committed Jan 18, 2021
1 parent 8500a1b commit 96c0dbb
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 13 deletions.
2 changes: 2 additions & 0 deletions lib/report/reportclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ var init = function init () {
options.openAps = true;
}

const reportStorage = require('./reportstorage');
reportStorage.saveProps(options);
var matchesneeded = 0;

// date range
Expand Down
35 changes: 35 additions & 0 deletions lib/report/reportstorage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const storage = require('js-storage').localStorage;
const COOKIE_KEY = 'reportProperties';
const defaultValues = {
insulin: true,
carbs: true,
basal: true,
notes: false,
food: true,
raw: false,
iob: false,
cob: false,
predicted: false,
openAps: false,
insulindistribution: true,
predictedTruncate: true
};
let cachedProps;

const saveProps = function (props) {
let propsToSave = {};
for (const prop in props) {
if (!Object.prototype.hasOwnProperty.call(defaultValues, prop))
continue;
propsToSave[prop] = props[prop];
}
storage.set(COOKIE_KEY, propsToSave);
};

const getValue = function (p) {
if (!cachedProps)
cachedProps = storage.get(COOKIE_KEY) || defaultValues;
return cachedProps[p];
};

module.exports = {saveProps: saveProps, getValue: getValue};
27 changes: 14 additions & 13 deletions lib/report_plugins/daytoday.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,23 @@ function init () {
module.exports = init;

daytoday.html = function html (client) {
const reportStorage = require('../report/reportstorage');
var translate = client.translate;
var ret =
'<h2>' + translate('Day to day') + '</h2>' +
'<b>' + translate('To see this report, press SHOW while in this view') + '</b><br>' +
translate('Display') + ': ' +
'<label><input type="checkbox" id="rp_optionsinsulin" checked><span style="color:blue;opacity:0.5">' + translate('Insulin') + '</span></label>' +
'<label><input type="checkbox" id="rp_optionscarbs" checked><span style="color:red;opacity:0.5">' + translate('Carbs') + '</span></label>' +
'<label><input type="checkbox" id="rp_optionsbasal" checked><span style="color:#0099ff;opacity:0.5">' + translate('Basal rate') + '</span></label>' +
'<label><input type="checkbox" id="rp_optionsnotes">' + translate('Notes') + '</label>' +
'<label><input type="checkbox" id="rp_optionsfood" checked>' + translate('Food') + '</label>' +
'<label><input type="checkbox" id="rp_optionsraw"><span style="color:gray;opacity:1">' + translate('Raw') + '</span></label>' +
'<label><input type="checkbox" id="rp_optionsiob"><span style="color:blue;opacity:0.5">' + translate('IOB') + '</span></label>' +
'<label><input type="checkbox" id="rp_optionscob"><span style="color:red;opacity:0.5">' + translate('COB') + '</span></label>' +
'<label><input type="checkbox" id="rp_optionspredicted"><span style="color:sienna;opacity:0.5">' + translate('Predictions') + '</span></label>' +
'<label><input type="checkbox" id="rp_optionsopenaps"><span style="color:sienna;opacity:0.5">' + translate('OpenAPS') + '</span></label>' +
'<label><input type="checkbox" id="rp_optionsdistribution" checked><span style="color:blue;opacity:0.5">' + translate('Insulin distribution') + '</span></label>' +
`<label><input type="checkbox" id="rp_optionsinsulin" ${reportStorage.getValue('insulin') ? "checked" : ""}><span style="color:blue;opacity:0.5"> ${translate('Insulin')} </span></label>` +
`<label><input type="checkbox" id="rp_optionscarbs" ${reportStorage.getValue('carbs') ? "checked" : ""}><span style="color:red;opacity:0.5">${translate('Carbs')}</span></label>` +
`<label><input type="checkbox" id="rp_optionsbasal" ${reportStorage.getValue('basal') ? "checked" : ""}><span style="color:#0099ff;opacity:0.5">${translate('Basal rate')}</span></label>` +
`<label><input type="checkbox" id="rp_optionsnotes" ${reportStorage.getValue('notes') ? "checked" : ""}>${translate('Notes')}</label>` +
`<label><input type="checkbox" id="rp_optionsfood" ${reportStorage.getValue('food') ? "checked" : ""}>${translate('Food')}</label>` +
`<label><input type="checkbox" id="rp_optionsraw" ${reportStorage.getValue('raw') ? "checked" : ""}><span style="color:gray;opacity:1">${translate('Raw')}</span></label>` +
`<label><input type="checkbox" id="rp_optionsiob" ${reportStorage.getValue('iob') ? "checked" : ""}><span style="color:blue;opacity:0.5">${translate('IOB')}</span></label>` +
`<label><input type="checkbox" id="rp_optionscob" ${reportStorage.getValue('cob') ? "checked" : ""}><span style="color:red;opacity:0.5">${translate('COB')}</span></label>` +
`<label><input type="checkbox" id="rp_optionspredicted" ${reportStorage.getValue('predicted') ? "checked" : ""}><span style="color:sienna;opacity:0.5">${translate('Predictions')}</span></label>` +
`<label><input type="checkbox" id="rp_optionsopenaps" ${reportStorage.getValue('openAps') ? "checked" : ""}><span style="color:sienna;opacity:0.5">${translate('OpenAPS')}</span></label>` +
`<label><input type="checkbox" id="rp_optionsdistribution" ${reportStorage.getValue('insulindistribution') ? "checked" : ""}><span style="color:blue;opacity:0.5">${translate('Insulin distribution')}</span></label>` +
'&nbsp;' + translate('Size') +
' <select id="rp_size">' +
' <option x="800" y="250">800x250px</option>' +
Expand All @@ -48,9 +49,9 @@ daytoday.html = function html (client) {
translate('Linear') + '</label>' +
'<label><input type="radio" name="rp_scale" id="rp_log">' +
translate('Logarithmic') + '</label>' +
'<div id="rp_predictedSettings" style="display:none">' +
`<div id="rp_predictedSettings" ${reportStorage.getValue('predicted') ? '' : 'style="display:none"'}>` +
translate('Truncate predictions: ') +
'<input type="checkbox" id="rp_optionsPredictedTruncate" checked>' +
`<input type="checkbox" id="rp_optionsPredictedTruncate" ${reportStorage.getValue('predictedTruncate') ? "checked" : ""}>` +
'<br>' +
translate('Predictions offset') + ': ' +
'<b><label id="rp_predictedOffset"></label> minutes</b>' +
Expand Down

0 comments on commit 96c0dbb

Please sign in to comment.