Skip to content

Commit

Permalink
Merge pull request #879 from eciis/show-all-notifications
Browse files Browse the repository at this point in the history
Show all notifications in notification page
  • Loading branch information
andreldsa authored Mar 9, 2018
2 parents 3a3d93f + 960d8ac commit 92d4abf
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 17 deletions.
9 changes: 8 additions & 1 deletion frontend/notification/notificationDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
notificationCtrl.user = AuthService.getCurrentUser();

notificationCtrl.notifications = [];
notificationCtrl.allNotifications = [];

var type_data = {
"COMMENT": {
Expand Down Expand Up @@ -200,6 +201,11 @@
});
}

notificationCtrl.showNotifications = function showNotifications($mdMenu, $event) {
(notificationCtrl.notifications.length === 0) ? notificationCtrl.seeAll() :
$mdMenu.open($event);
};

notificationCtrl.format = function format(notification) {
return NotificationService.formatMessage(notification);
};
Expand All @@ -210,7 +216,7 @@
});
};

notificationCtrl.number_of_notifications = function number_of_notifications() {
notificationCtrl.numberUnreadNotifications = function numberUnreadNotifications() {
return notificationCtrl.notifications.length < 100 ?
notificationCtrl.notifications.length : "+99";
};
Expand All @@ -237,6 +243,7 @@

(function main() {
NotificationService.watchNotifications(notificationCtrl.user.key, notificationCtrl.notifications);
notificationCtrl.allNotifications = NotificationService.getAllNotifications();
})();
});

Expand Down
30 changes: 17 additions & 13 deletions frontend/notification/notificationService.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

var ref = firebase.database().ref();

var firebaseArrayNotifications;
service.firebaseArrayNotifications;

/** Types of notification based on
* the number of institutions mentioned on it **/
Expand Down Expand Up @@ -57,15 +57,15 @@

service.watchNotifications = function watchNotifications(userKey, notificationsList) {
setupNotifications(userKey, function() {
_.forEach(firebaseArrayNotifications, function each(notification) {
_.forEach(service.firebaseArrayNotifications, function each(notification) {
if (isNew(notification)) {
notificationsList.push(notification);
}
});

firebaseArrayNotifications.$watch(function(ev) {
service.firebaseArrayNotifications.$watch(function(ev) {
if (ev.event === CHILD_ADDED) {
var notification = firebaseArrayNotifications.$getRecord(ev.key);
var notification = service.firebaseArrayNotifications.$getRecord(ev.key);
notificationsList.push(notification);
if (isNew(notification)) {
$rootScope.$emit(notification.entity_type);
Expand All @@ -77,9 +77,9 @@

service.watchPostNotification = function watchPostNotification(userKey, callback) {
setupNotifications(userKey, function() {
firebaseArrayNotifications.$watch(function(ev) {
service.firebaseArrayNotifications.$watch(function(ev) {
if (ev.event === CHILD_ADDED) {
var notification = firebaseArrayNotifications.$getRecord(ev.key);
var notification = service.firebaseArrayNotifications.$getRecord(ev.key);
if (notification.entity_type === POST_NOTIFICATION) {
callback();
}
Expand All @@ -90,15 +90,19 @@

service.markAsRead = function markAsRead(notification) {
notification.status = "READ";
return firebaseArrayNotifications.$save(notification);
return service.firebaseArrayNotifications.$save(notification);
};

service.getAllNotifications = function getAllNotifications() {
return service.firebaseArrayNotifications;
};

function setupNotifications(userKey, callback) {
if (!firebaseArrayNotifications) {
if (!service.firebaseArrayNotifications) {
var notificationsRef = ref.child("notifications/"+userKey);
firebaseArrayNotifications = $firebaseArray(notificationsRef);
service.firebaseArrayNotifications = $firebaseArray(notificationsRef);
}
firebaseArrayNotifications.$loaded().then(function() {
service.firebaseArrayNotifications.$loaded().then(function() {
callback();
});
}
Expand Down Expand Up @@ -131,9 +135,9 @@
* service notifications.
*/
AuthService.$onLogout(function destroy() {
if(firebaseArrayNotifications) {
firebaseArrayNotifications.$destroy();
firebaseArrayNotifications = undefined;
if(service.firebaseArrayNotifications) {
service.firebaseArrayNotifications.$destroy();
service.firebaseArrayNotifications = undefined;
}
});
});
Expand Down
4 changes: 2 additions & 2 deletions frontend/notification/notifications.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<md-menu md-position-mode="target-right target">
<div>
<span ng-if="notificationCtrl.notifications.length > 0" style="position:relative; bottom: -1px; right:-33px;"
class="counter-of-notifications" data-badge="{{notificationCtrl.number_of_notifications()}}">
class="counter-of-notifications" data-badge="{{notificationCtrl.numberUnreadNotifications()}}">
</span>
<md-button aria-label="menu" class="md-icon-button" ng-click="$mdMenu.open(ev)" ng-disabled="notificationCtrl.notifications.length === 0">
<md-button aria-label="menu" class="md-icon-button" ng-click="notificationCtrl.showNotifications($mdMenu, ev)">
<md-icon style="color:#81C784;">{{ notificationCtrl.notifications.length === 0 ? 'notifications_none' : 'notifications'}}</md-icon>
</md-button>
</div>
Expand Down
2 changes: 1 addition & 1 deletion frontend/notification/notifications_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ <h1>
<md-content class="custom-scrollbar">
<md-list style="padding: 10px;">
<md-list-item class="md-3-line" md-colors="{background: 'grey-200'}" style="margin-bottom: 5px; padding: 3 0 3 0;"
ng-repeat="notification in notificationCtrl.notifications | orderBy:'-timestamp' | filter: notificationCtrl.keyword"
ng-repeat="notification in notificationCtrl.allNotifications | orderBy:'-timestamp' | filter: notificationCtrl.keyword"
ng-click="notificationCtrl.action(notification, $event)">
<img ng-src="{{notification.from.photo_url}}" class="md-avatar" style="width: 60px; height: 60px;"/>
<div class="md-list-item-text" layout="column">
Expand Down
126 changes: 126 additions & 0 deletions frontend/test/specs/NotificationControllerSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
'use strict';

(describe('Test NotificationController', function() {

var notCtrl, httpBackend, scope, createCtrl, mdDialog, state, notificationService, http, authService;

var institution = {
name: 'institution',
key: '123456789'
};

var user = {
name: 'user',
institutions: [institution],
current_institution: institution,
follows: institution.key
};

var entity = {
institution_name : institution.name,
key: institution.key
}

var postNotification = {
id: 'abc1234',
status: 'NEW',
timestamp: new Date(),
entity : entity,
from : entity
};

var surveyPostNotification = {
id: 'abcde6789',
status: 'READ',
timestamp: new Date(),
entity : entity,
from : entity
};

institution.admin = user.key;
postNotification.entity.entity_type = "POST";
surveyPostNotification.entity.entity_type = "SURVEY_POST";

beforeEach(module('app'));

beforeEach(inject(function($controller, $httpBackend, $rootScope, $q, NotificationService,
$mdDialog, $state, AuthService, $http) {
httpBackend = $httpBackend;
http = $http;
scope = $rootScope.$new();
mdDialog = $mdDialog;
state = $state;
authService = AuthService;
notificationService = NotificationService;
httpBackend.when('GET', 'app/user/user_inactive.html').respond(200);

authService.login(user);

createCtrl = function() {
return $controller('NotificationController', {
scope: scope
});
};
notCtrl = createCtrl();
notCtrl.notifications = [];
httpBackend.flush();
}));

afterEach(function() {
httpBackend.verifyNoOutstandingExpectation();
httpBackend.verifyNoOutstandingRequest();
});

describe('main()', function() {

it("should call getAllNotifications.", function() {
spyOn(notificationService, 'getAllNotifications').and.callThrough();
spyOn(notificationService, 'watchNotifications').and.callThrough();

notCtrl = createCtrl();

expect(notificationService.getAllNotifications).toHaveBeenCalled();
expect(notificationService.watchNotifications).toHaveBeenCalledWith(user.key, notCtrl.notifications);
//The user don't have any notifications
expect(_.isEmpty(notCtrl.notifications)).toBe(true);
});
});

describe('numberUnreadNotifications()', function() {

it("should be zero.", function() {
expect(notCtrl.numberUnreadNotifications()).toEqual(0);
});

it("should be one.", function() {
notificationService.firebaseArrayNotifications.push(postNotification);
notCtrl.notifications.push(postNotification);

expect(notCtrl.numberUnreadNotifications()).toEqual(1);
});
});

describe('showNotifications()', function() {

it("should redirect to notification page.", function() {
spyOn(notCtrl, 'seeAll');
notCtrl.showNotifications();
expect(notCtrl.seeAll).toHaveBeenCalled();
});

it("should open menu.", function() {
notificationService.firebaseArrayNotifications.push(postNotification);
notCtrl.notifications.push(postNotification);
var mdMenu = {
open : function(ev){}
};

spyOn(notCtrl, 'seeAll');
spyOn(mdMenu, 'open');

notCtrl.showNotifications(mdMenu, event);
expect(mdMenu.open).toHaveBeenCalled();
expect(notCtrl.seeAll).not.toHaveBeenCalled();
});
});
}));

0 comments on commit 92d4abf

Please sign in to comment.