luci-app-cloudflared: simplify service status render
authorSergey Ponomarev <stokito@gmail.com>
Thu, 1 Feb 2024 21:41:10 +0000 (23:41 +0200)
committerPaul Donald <newtwen@gmail.com>
Thu, 8 Feb 2024 13:32:15 +0000 (14:32 +0100)
Instead of using poll use the o.cfgvalue to render a status.

Signed-off-by: Sergey Ponomarev <stokito@gmail.com>
(cherry picked from commit a5dfb9604c1b9eb7ac220ae83dc0478b0e3a58f0)

applications/luci-app-cloudflared/htdocs/luci-static/resources/view/cloudflared/config.js

index d473a2536d94cf93dce052d44cc16887403d4732..241ed6486ada470523410d473fc18cd24373cd32 100644 (file)
@@ -5,9 +5,7 @@
 
 'use strict';
 'require form';
-'require poll';
 'require rpc';
-'require uci';
 'require view';
 
 var callServiceList = rpc.declare({
@@ -27,26 +25,15 @@ function getServiceStatus() {
        });
 }
 
-function renderStatus(isRunning) {
-       var spanTemp = '<label class="cbi-value-title">Status</label><div class="cbi-value-field"><em><span style="color:%s">%s</span></em></div>';
-       var renderHTML;
-       if (isRunning) {
-               renderHTML = String.format(spanTemp, 'green', _('Running'));
-       } else {
-               renderHTML = String.format(spanTemp, 'red', _('Not Running'));
-       }
-
-       return renderHTML;
-}
-
 return view.extend({
        load: function () {
                return Promise.all([
-                       uci.load('cloudflared')
+                       getServiceStatus()
                ]);
        },
 
        render: function (data) {
+               let isRunning = data[0];
                var m, s, o;
 
                m = new form.Map('cloudflared', _('Cloudflare Zero Trust Tunnel'),
@@ -59,18 +46,15 @@ return view.extend({
 
                s = m.section(form.NamedSection, 'config', 'cloudflared');
 
-               o = s.option(form.DummyValue, 'service_status', _('Status'));
-               o.load = function () {
-                       poll.add(function () {
-                               return L.resolveDefault(getServiceStatus()).then(function (res) {
-                                       var view = document.getElementById('cbi-cloudflared-config-service_status');
-                                       if (view) {
-                                               view.innerHTML = renderStatus(res);
-                                       }
-                               });
-                       });
+               o = s.option(form.DummyValue, '_status', _('Status'));
+               o.rawhtml = true;
+               o.cfgvalue = function(section_id) {
+                       var span = '<b><span style="color:%s">%s</span></b>';
+                       var renderHTML = isRunning ?
+                               String.format(span, 'green', _('Running')) :
+                               String.format(span, 'red', _('Not Running'));
+                       return renderHTML;
                };
-               o.value = _('Collecting data...');
 
                o = s.option(form.Flag, 'enabled', _('Enable'));
                o.rmempty = false;