Skip to content

Commit 1ca5856

Browse files
committed
[fix] Add spinner
1 parent 90e6bf9 commit 1ca5856

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

openwisp_monitoring/device/static/monitoring/js/device-map.js

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,22 @@
6363
success: function (data) {
6464
let devices = data.results;
6565
let nextUrl = data.next;
66-
const uniqueStatus = Array.from(
67-
new Set(devices.map((d) => d.monitoring.status_label)),
68-
);
66+
const uniqueStatus = devices.reduce((acc, device) => {
67+
const { status_label, status } = device.monitoring;
68+
acc[status_label] = status;
69+
return acc;
70+
}, {});
6971
let statusFiltersBtn = "";
70-
uniqueStatus.forEach((status) => {
71-
const label = gettext(status);
72+
Object.entries(uniqueStatus).forEach(([status_label, status]) => {
73+
const label = gettext(status_label);
7274
statusFiltersBtn += `
73-
<span
74-
class="health-status health-${status} status-filter"
75-
data-status="${status}"
76-
>
77-
${label}
78-
</span>
79-
`;
75+
<span
76+
class="health-status health-${status} status-filter"
77+
data-status="${status}"
78+
>
79+
${label}
80+
</span>
81+
`;
8082
});
8183
const has_floorplan = data.has_floorplan;
8284
const floorplan_btn = has_floorplan
@@ -110,6 +112,7 @@
110112
111113
</tbody>
112114
</table>
115+
// Todo: Fix css
113116
<div class="ow-loading-spinner" style="display: none; position: absolute; bottom: 10px; left: 50%; transform: translateX(-50%);"></div>
114117
</div>
115118
${floorplan_btn}
@@ -145,10 +148,14 @@
145148
el.find("tbody").html(rows);
146149
}
147150
let fetchDevicesTimeout;
151+
let loading = false;
148152
function fetchDevices(url, ms = 0) {
149-
if (!url) return;
153+
if (!url || loading) return;
150154
clearTimeout(fetchDevicesTimeout);
151155
fetchDevicesTimeout = setTimeout(() => {
156+
loading = true;
157+
const spinner = el.find(".table-container .ow-loading-spinner");
158+
spinner.show();
152159
let params = new URLSearchParams();
153160
const searchParam = el
154161
.find("#device-search")
@@ -170,6 +177,7 @@
170177
// if nextUrl is the same as url, that means we are fetching for infinite scroll
171178
if (url === nextUrl) fetchUrl = url;
172179
else fetchUrl = queryString ? `${url}?${queryString}` : url;
180+
173181
$.ajax(
174182
{
175183
dataType: "json",
@@ -188,14 +196,17 @@
188196
error() {
189197
console.error("Could not load more devices from", url);
190198
},
199+
complete() {
200+
loading = false;
201+
spinner.hide();
202+
},
191203
},
192-
ms,
193204
);
194-
});
205+
}, ms);
195206
}
196207
renderRows();
197208
el.find("#device-search").on("input", function () {
198-
fetchDevices(url);
209+
fetchDevices(url, 300);
199210
});
200211
let activeStatuses = [];
201212
el.find(".status-filter").on("click", function (e) {
@@ -218,7 +229,7 @@
218229
});
219230
el.find(".table-container").on("scroll", function () {
220231
if (this.scrollTop + this.clientHeight >= this.scrollHeight - 10) {
221-
fetchDevices(nextUrl, 300);
232+
fetchDevices(nextUrl, 100);
222233
}
223234
});
224235
loadingOverlay.hide();

0 commit comments

Comments
 (0)