Skip to content
This repository was archived by the owner on Oct 5, 2021. It is now read-only.

Commit b31c173

Browse files
committed
Merge branch 'rest-api-improvements' of https://github.com/wso2/product-emm into rest-api-improvements
2 parents 5d12ce3 + 4d5966c commit b31c173

File tree

4 files changed

+165
-122
lines changed

4 files changed

+165
-122
lines changed

modules/apps/jaggery/emm/src/emm/units/appbar/public/js/appbar.js

+88-69
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,19 @@
1515
* specific language governing permissions and limitations
1616
* under the License.
1717
*/
18-
var modalPopup = '.wr-modalpopup',
19-
modalPopupContainer = modalPopup + ' .modalpopup-container',
20-
modalPopupContent = modalPopup + ' .modalpopup-content';
21-
function openCollapsedNav() {
22-
$(".wr-hidden-nav-toggle-btn").addClass("active");
23-
$("#hiddenNav").slideToggle("slideDown", function () {
24-
if ($(this).css("display") == "none") {
25-
$(".wr-hidden-nav-toggle-btn").removeClass("active");
26-
}
27-
});
28-
}
2918

19+
var modalPopup = ".wr-modalpopup",
20+
modalPopupContainer = modalPopup + " .modalpopup-container",
21+
modalPopupContent = modalPopup + " .modalpopup-content";
22+
23+
//function openCollapsedNav() {
24+
// $(".wr-hidden-nav-toggle-btn").addClass("active");
25+
// $("#hiddenNav").slideToggle("slideDown", function () {
26+
// if ($(this).css("display") == "none") {
27+
// $(".wr-hidden-nav-toggle-btn").removeClass("active");
28+
// }
29+
// });
30+
//}
3031

3132
/*
3233
* set popup maximum height function.
@@ -56,7 +57,9 @@ function hidePopup() {
5657
$(modalPopup).hide();
5758
}
5859

59-
60+
/*
61+
* QR-code generation function.
62+
*/
6063
function generateQRCode(qrCodeClass) {
6164
var enrollmentURL = $("#qr-code-modal").data("enrollment-url");
6265
$(qrCodeClass).qrcode({
@@ -72,74 +75,85 @@ function toggleEnrollment() {
7275
showPopup();
7376
}
7477

75-
var showNotificationCount = function (data) {
76-
if (data) {
77-
data = JSON.parse(data);
78-
if (data.length > 0) {
79-
$("#notification-bubble").html(data.length);
78+
var updateNotificationCount = function (data, jqXHR) {
79+
if (jqXHR.status == 200 && data) {
80+
var responsePayload = JSON.parse(data);
81+
var newNotificationsCount = responsePayload.devices.length;
82+
if (newNotificationsCount > 0) {
83+
$("#notification-bubble").html(newNotificationsCount);
8084
} else {
81-
hideNotificationCount();
85+
$("#notification-bubble").html("Error");
8286
}
83-
} else {
84-
hideNotificationCount();
8587
}
8688
};
8789

88-
function loadNotificationCount() {
90+
function loadNotificationsPanel() {
8991
if ("true" == $("#right-sidebar").attr("is-authorized")) {
90-
var serviceURL = "/mdm-admin/notifications/NEW";
91-
invokerUtil.get(serviceURL, showNotificationCount, hideNotificationCount);
92-
loadNotifications();
92+
var serviceURL = "/api/device-mgt/v1.0/notifications?status=NEW";
93+
invokerUtil.get(serviceURL, updateNotificationCount, hideNotificationCount);
94+
loadNewNotifications();
9395
} else {
9496
$("#notification-bubble-wrapper").remove();
9597
}
9698
}
9799

98-
function hideNotificationCount() {
99-
$("#notification-bubble").hide();
100+
function hideNotificationCount(jqXHR) {
101+
if (jqXHR.status == 404) {
102+
// this means "no new notifications to show"
103+
$("#notification-bubble").hide();
104+
} else {
105+
$("#notification-bubble").html("Error");
106+
}
100107
}
101108

102-
function loadNotifications() {
103-
if ("true" == $("#right-sidebar").attr("is-authorized")) {
104-
var notificationListing = $("#notifications");
105-
var notificationListingSrc = notificationListing.attr("src");
106-
var currentUser = notificationListing.data("currentUser");
107-
$.template("notification-listing", notificationListingSrc, function (template) {
108-
var serviceURL = "/mdm-admin/notifications/NEW";
109-
var successCallback = function (data) {
110-
var viewModel = {};
111-
data = JSON.parse(data);
112-
viewModel.notifications = data;
113-
if (data.length > 0) {
114-
var content = template(viewModel);
115-
$(".sidebar-messages").html(content);
109+
function loadNewNotifications() {
110+
var messageSideBar = ".sidebar-messages";
111+
if ($("#right-sidebar").attr("is-authorized") == "true") {
112+
var notifications = $("#notifications");
113+
var currentUser = notifications.data("currentUser");
114+
115+
$.template("notification-listing", notifications.attr("src"), function (template) {
116+
var serviceURL = "/api/device-mgt/v1.0/notifications?status=NEW";
117+
118+
var successCallback = function (data, textStatus, jqXHR) {
119+
if (jqXHR.status == 200 && data) {
120+
var viewModel = {};
121+
var responsePayload = JSON.parse(data);
122+
123+
if (responsePayload.devices && responsePayload.devices.length > 0) {
124+
viewModel.notifications = responsePayload.devices;
125+
$(messageSideBar).html(template(viewModel));
126+
} else {
127+
$(messageSideBar).html("<h4 class='text-center'>Error in getting new notifications.</h4>");
128+
}
129+
}
130+
};
131+
132+
var errorCallback = function (jqXHR) {
133+
if (jqXHR.status = 404) {
134+
$(messageSideBar).html("<h4 class='text-center'>No new notifications found...</h4>");
116135
} else {
117-
var content = "<h4 class='text-center' >You have no new notifications</a></h4>";
118-
$(".sidebar-messages").html(content);
136+
$(messageSideBar).html("<h4 class ='message-danger'>Unexpected error occurred while trying " +
137+
"to retrieve any new notifications.</h4>");
119138
}
120139
};
121-
invokerUtil.get(serviceURL, successCallback, function (message) {
122-
var content = "<p>Unexpected error occurred while notification listing </p>";
123-
$(".sidebar-messages").html(content);
124-
});
140+
invokerUtil.get(serviceURL, successCallback, errorCallback);
125141
});
126142
} else {
127-
var content = "<h4 class ='message-danger'>You are not authorized to view notifications</h4>";
128-
$(".sidebar-messages").html(content);
143+
$(messageSideBar).html("<h4 class ='message-danger'>You are not authorized to view notifications</h4>");
129144
}
130145
}
131146

132147
/**
133-
* Sidebar function
148+
* Toggle function for
149+
* notification listing sidebar.
134150
* @return {Null}
135151
*/
136152
$.sidebar_toggle = function (action, target, container) {
137153
var elem = '[data-toggle=sidebar]',
138154
button,
139-
container,
140-
conrainerOffsetLeft,
141-
conrainerOffsetRight,
142-
target,
155+
containerOffsetLeft,
156+
containerOffsetRight,
143157
targetOffsetLeft,
144158
targetOffsetRight,
145159
targetWidth,
@@ -150,18 +164,20 @@ $.sidebar_toggle = function (action, target, container) {
150164

151165
var sidebar_window = {
152166
update: function (target, container, button) {
153-
conrainerOffsetLeft = $(container).data('offset-left') ? $(container).data('offset-left') : 0,
154-
conrainerOffsetRight = $(container).data('offset-right') ? $(container).data('offset-right') : 0,
155-
targetOffsetLeft = $(target).data('offset-left') ? $(target).data('offset-left') : 0,
156-
targetOffsetRight = $(target).data('offset-right') ? $(target).data('offset-right') : 0,
157-
targetWidth = $(target).data('width'),
158-
targetSide = $(target).data("side"),
159-
pushType = $(container).parent().is('body') == true ? 'padding' : 'margin';
167+
containerOffsetLeft = $(container).data('offset-left') ? $(container).data('offset-left') : 0;
168+
containerOffsetRight = $(container).data('offset-right') ? $(container).data('offset-right') : 0;
169+
targetOffsetLeft = $(target).data('offset-left') ? $(target).data('offset-left') : 0;
170+
targetOffsetRight = $(target).data('offset-right') ? $(target).data('offset-right') : 0;
171+
targetWidth = $(target).data('width');
172+
targetSide = $(target).data("side");
173+
pushType = $(container).parent().is('body') == true ? 'padding' : 'margin';
174+
160175
if (button !== undefined) {
161176
relationship = button.attr('rel') ? button.attr('rel') : '';
162177
buttonParent = $(button).parent();
163178
}
164179
},
180+
165181
show: function () {
166182
if ($(target).data('sidebar-fixed') == true) {
167183
$(target).height($(window).height() - $(target).data('fixed-offset'));
@@ -206,6 +222,7 @@ $.sidebar_toggle = function (action, target, container) {
206222
}
207223
$(target).trigger('shown.sidebar');
208224
},
225+
209226
hide: function () {
210227
$(target).trigger('hide.sidebar');
211228
$(target).removeClass('toggled');
@@ -252,8 +269,9 @@ $.sidebar_toggle = function (action, target, container) {
252269
sidebar_window.hide();
253270
}
254271
// binding click function
255-
$('body').off('click', elem);
256-
$('body').on('click', elem, function (e) {
272+
var body = 'body';
273+
$(body).off('click', elem);
274+
$(body).on('click', elem, function (e) {
257275
e.preventDefault();
258276
button = $(this);
259277
container = button.data('container');
@@ -322,13 +340,15 @@ $.fn.collapse_nav_sub = function () {
322340
};
323341

324342
$(document).ready(function () {
325-
loadNotificationCount();
343+
loadNotificationsPanel();
326344
$.sidebar_toggle();
327345

328-
$("#right-sidebar").on("click", ".new-notification", function (e) {
346+
$("#right-sidebar").on("click", ".new-notification", function () {
329347
var notificationId = $(this).data("id");
330348
var redirectUrl = $(this).data("url");
331349
var markAsReadNotificationsAPI = "/mdm-admin/notifications/" + notificationId + "/CHECKED";
350+
var messageSideBar = ".sidebar-messages";
351+
332352
invokerUtil.put(
333353
markAsReadNotificationsAPI,
334354
null,
@@ -337,12 +357,11 @@ $(document).ready(function () {
337357
if (data.statusCode == responseCodes["ACCEPTED"]) {
338358
location.href = redirectUrl;
339359
}
340-
}, function (data) {
360+
}, function () {
341361
var content = "<li class='message message-danger'><h4><i class='icon fw fw-error'></i>Warning</h4>" +
342-
"<p>Unexpected error occurred while loading notification. Please refresh the page and" +
343-
" try again</p></li>";
344-
$(".sidebar-messages").html(content);
345-
$(".sidebar-messages").html(content);
362+
"<p>Unexpected error occurred while loading notification. Please refresh the page and" +
363+
" try again</p></li>";
364+
$(messageSideBar).html(content);
346365
}
347366
);
348367
});

modules/apps/jaggery/emm/src/emm/units/dashboard/public/js/dashboard.js

+40-14
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,61 @@
1+
/*
2+
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
3+
*
4+
* WSO2 Inc. licenses this file to you under the Apache License,
5+
* Version 2.0 (the "License"); you may not use this file except
6+
* in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing,
12+
* software distributed under the License is distributed on an
13+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
14+
* either express or implied. See the License for the
15+
* specific language governing permissions and limitations
16+
* under the License.
17+
*/
18+
119
var updateStats = function (serviceURL, id) {
220
invokerUtil.get(
321
serviceURL,
4-
function (data) {
5-
if (!data) {
22+
function (data, textStatus, jqXHR) {
23+
if (jqXHR.status == 200 && data) {
24+
var responsePayload = JSON.parse(data);
25+
var itemCount = responsePayload.count;
26+
if (itemCount) {
27+
$(id).html(itemCount);
28+
} else {
29+
$(id).html("Error...");
30+
}
31+
} else if (!data) {
632
updateStats(serviceURL, id);
33+
}
34+
},
35+
36+
function (jqXHR) {
37+
if (jqXHR.status == 404) {
38+
$(id).html(0);
39+
$(id + "-view-btn").hide();
740
} else {
8-
var data = JSON.parse(data);
9-
var count =data.count;
10-
$(id).html(count);
11-
if (Number(count) <= 0) {
12-
$(id + "-view-btn").hide();
13-
}
41+
$(id).html("Error...");
1442
}
15-
}, function (message) {
16-
console.log(message.content);
1743
}
1844
);
1945
};
2046

2147
$(document).ready(function () {
2248
if ($("#device-count").data("device-count")) {
23-
updateStats("/api/device-mgt/v1.0/devices", "#device-count");
49+
updateStats("/api/device-mgt/v1.0/devices?offset=0&limit=1", "#device-count");
2450
}
2551
if ($("#policy-count").data("policy-count")) {
26-
updateStats("/api/device-mgt/v1.0/policies", "#policy-count");
52+
updateStats("/api/device-mgt/v1.0/policies?offset=0&limit=1", "#policy-count");
2753
}
2854
if ($("#user-count").data("user-count")) {
29-
updateStats("/api/device-mgt/v1.0/users", "#user-count");
55+
updateStats("/api/device-mgt/v1.0/users?offset=0&limit=1", "#user-count");
3056
}
3157
if ($("#role-count").data("role-count")) {
32-
updateStats("/api/device-mgt/v1.0/roles", "#role-count");
58+
updateStats("/api/device-mgt/v1.0/roles?offset=0&limit=1", "#role-count");
3359
}
3460
});
3561

0 commit comments

Comments
 (0)