From 22664ecc0f6f9978545dce51ecbea032ca68e25d Mon Sep 17 00:00:00 2001 From: Christopher Charbonneau Wells Date: Sun, 29 Oct 2017 16:01:13 -0400 Subject: [PATCH] Reload the dashboard page every 60 seconds and on visibility state change. --- dashboard/static_src/js/dashboard.js | 36 ++++++++++++++++++++++++ dashboard/templates/dashboard/child.html | 8 +++++- 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 dashboard/static_src/js/dashboard.js diff --git a/dashboard/static_src/js/dashboard.js b/dashboard/static_src/js/dashboard.js new file mode 100644 index 00000000..bde66ecd --- /dev/null +++ b/dashboard/static_src/js/dashboard.js @@ -0,0 +1,36 @@ +/* Baby Buddy Dashboard + * + * Provides a "watch" function to update the dashboard at one minute intervals + * and/or on visibility state changes. + */ +BabyBuddy.Dashboard = function ($) { + var runIntervalId = null; + var dashboardElement = null; + var lastUpdate = moment(); + + var Dashboard = { + watch: function(element_id) { + dashboardElement = $('#' + element_id); + + if (dashboardElement.length == 0) { + console.error('Baby Buddy: Dashboard element not found.'); + return false; + } + + runIntervalId = setInterval(this.update, 60000); + + Visibility.change(function (e, state) { + if (state == 'visible' && moment().diff(lastUpdate) > 60000) { + Dashboard.update(); + } + }); + }, + + update: function() { + // TODO: Someday maybe update in place? + location.reload(); + } + }; + + return Dashboard; +}(jQuery); diff --git a/dashboard/templates/dashboard/child.html b/dashboard/templates/dashboard/child.html index 68c70c53..3ea854c3 100644 --- a/dashboard/templates/dashboard/child.html +++ b/dashboard/templates/dashboard/child.html @@ -10,7 +10,7 @@ {% endblock %} {% block content %} -
+
{% card_feeding_last object %} {% card_feeding_last_method object %} @@ -27,4 +27,10 @@ {% card_diaperchange_types object %}
+{% endblock %} + +{% block javascript %} + {% endblock %} \ No newline at end of file