From 082fb27101b8cc65b7f5668656b5b617f9275baa Mon Sep 17 00:00:00 2001 From: Adwaith Rao Date: Wed, 8 Dec 2021 16:11:05 +0530 Subject: [PATCH] Add login and notification services to inject into components --- .../notifications/notifications.component.ts | 81 +------------------ src/app/layout/tabs/tabs.component.html | 4 +- src/app/layout/tabs/tabs.component.ts | 9 ++- src/app/services/login.service.spec.ts | 16 ++++ src/app/services/login.service.ts | 40 +++++++++ src/app/services/notification.service.spec.ts | 16 ++++ src/app/services/notification.service.ts | 78 ++++++++++++++++++ 7 files changed, 163 insertions(+), 81 deletions(-) create mode 100644 src/app/services/login.service.spec.ts create mode 100644 src/app/services/login.service.ts create mode 100644 src/app/services/notification.service.spec.ts create mode 100644 src/app/services/notification.service.ts diff --git a/src/app/layout/notifications/notifications.component.ts b/src/app/layout/notifications/notifications.component.ts index c0eaee6..4ab6359 100644 --- a/src/app/layout/notifications/notifications.component.ts +++ b/src/app/layout/notifications/notifications.component.ts @@ -1,14 +1,5 @@ import { Component, OnInit } from '@angular/core'; - -type Role = 'Officer'|'Investigator'|'Hod'|'Panel'|'Customer'; - -interface Notification { - text: string, - timeStamp: string, - roles: Array, - description?: string, - redirectionUrl?: string, -} +import { Notification, NotificationService } from 'src/app/services/notification.service'; @Component({ selector: 'app-notifications', @@ -16,76 +7,10 @@ interface Notification { styleUrls: ['./notifications.component.scss'] }) export class NotificationsComponent implements OnInit { - loginRole: Role; - - notifications: Array = [{ - text: 'New name application has been submitted', - description: 'A new Applicant in the company name Kimao has applied for an appeal', - roles: ['Officer'], - timeStamp: '1 hour ago', - }, { - text: 'Entity Corp. flagged for investigation', - roles: ['Investigator'], - timeStamp: '2 days ago', - redirectionUrl: '/tabs/investigate-business-entities-and-individuals', - }, { - text: 'Request to create a panel', - roles: ['Hod'], - timeStamp: '1 day ago', - redirectionUrl: '/tabs/investigate-business-entities-and-individuals', - }, { - text: 'Review non-compliance for Entity Corp.', - roles: ['Investigator'], - timeStamp: '4 hours ago', - redirectionUrl: '/tabs/investigate-business-entities-and-individuals', - }, { - text: 'Concur non-compliance for Entity Corp.', - roles: ['Panel'], - timeStamp: '2 hours ago', - redirectionUrl: '/tabs/investigate-business-entities-and-individuals', - }, { - text: 'Prepare preliminary letter', - roles: ['Investigator'], - timeStamp: '2 hours ago', - redirectionUrl: '/tabs/investigate-business-entities-and-individuals', - }, { - text: 'Investigation complete', - roles: ['Investigator'], - timeStamp: '1 hours ago', - redirectionUrl: '/tabs/investigate-business-entities-and-individuals', - }, { - text: 'New name application has been submitted', - roles: ['Officer'], - description: 'A new Applicant in the company name Kimao has applied for an appeal', - timeStamp: '1 hour ago', - }, { - text: 'Request to create a new Committee', - roles: ['Officer'], - timeStamp: '2 hours ago', - }, { - text: 'Respond to non-compliance preliminary letter', - roles: ['Customer'], - timeStamp: '1 hour ago', - redirectionUrl: '/tabs/investigate-business-entities-and-individuals', - }]; allowedNotifications: Array = []; - constructor() { - const loginEmail = localStorage.getItem('loginEmail'); - - if (loginEmail && loginEmail.toLocaleLowerCase().startsWith('hod')) { - this.loginRole = 'Hod'; - } else if (loginEmail && loginEmail.toLocaleLowerCase().startsWith('panel')) { - this.loginRole = 'Panel'; - } else if (loginEmail && loginEmail.toLocaleLowerCase().startsWith('customer')) { - this.loginRole = 'Customer'; - } else if (loginEmail && loginEmail.toLocaleLowerCase().startsWith('investigator')) { - this.loginRole = 'Investigator'; - } else { - this.loginRole = 'Officer'; - } - - this.allowedNotifications = this.notifications.filter(notification => notification.roles.includes(this.loginRole)); + constructor(notificationService: NotificationService) { + this.allowedNotifications = notificationService.getAllowedNotifications(); } ngOnInit(): void { diff --git a/src/app/layout/tabs/tabs.component.html b/src/app/layout/tabs/tabs.component.html index 58d0a39..09bf70f 100644 --- a/src/app/layout/tabs/tabs.component.html +++ b/src/app/layout/tabs/tabs.component.html @@ -17,13 +17,13 @@ [ngClass]="{'active' : showNotifications}"> bell icon - 9+ + {{ notificationsCount }}
user icon - Hi, Joe Ghatto + Hi, {{ loginName }} chevron down image diff --git a/src/app/layout/tabs/tabs.component.ts b/src/app/layout/tabs/tabs.component.ts index 667e33f..9792089 100644 --- a/src/app/layout/tabs/tabs.component.ts +++ b/src/app/layout/tabs/tabs.component.ts @@ -1,4 +1,6 @@ import { Component, OnInit } from '@angular/core'; +import { LoginService } from 'src/app/services/login.service'; +import { NotificationService } from 'src/app/services/notification.service'; @Component({ selector: 'app-tabs', @@ -9,7 +11,12 @@ export class TabsComponent implements OnInit { showNotifications: boolean = false; showLogout: boolean = false; - constructor() { + loginName: string = ''; + notificationsCount: number = 0; + + constructor(loginService: LoginService, notificationService: NotificationService) { + this.loginName = loginService.getLoginName(); + this.notificationsCount = notificationService.getAllowedNotifications().length; } ngOnInit(): void { diff --git a/src/app/services/login.service.spec.ts b/src/app/services/login.service.spec.ts new file mode 100644 index 0000000..299b0d5 --- /dev/null +++ b/src/app/services/login.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { LoginService } from './login.service'; + +describe('LoginService', () => { + let service: LoginService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(LoginService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/services/login.service.ts b/src/app/services/login.service.ts new file mode 100644 index 0000000..682a048 --- /dev/null +++ b/src/app/services/login.service.ts @@ -0,0 +1,40 @@ +import { Injectable } from '@angular/core'; + +export type Role = 'Officer'|'Investigator'|'Hod'|'Panel'|'Customer'; + +@Injectable({ + providedIn: 'root' +}) +export class LoginService { + private loginRole: Role; + + constructor() { + const loginEmail = localStorage.getItem('loginEmail'); + + if (loginEmail && loginEmail.toLocaleLowerCase().startsWith('hod')) { + this.loginRole = 'Hod'; + } else if (loginEmail && loginEmail.toLocaleLowerCase().startsWith('panel')) { + this.loginRole = 'Panel'; + } else if (loginEmail && loginEmail.toLocaleLowerCase().startsWith('customer')) { + this.loginRole = 'Customer'; + } else if (loginEmail && loginEmail.toLocaleLowerCase().startsWith('investigator')) { + this.loginRole = 'Investigator'; + } else { + this.loginRole = 'Officer'; + } + } + + getLoginName() { + const loginEmail = localStorage.getItem('loginEmail'); + if (loginEmail && loginEmail.includes('@')) { + const name = loginEmail.substring(0, loginEmail.indexOf('@')); + return name[0].toUpperCase() + name.substring(1); + } else { + return 'Joe Ghatto'; + } + } + + getLoginRole() { + return this.loginRole; + } +} diff --git a/src/app/services/notification.service.spec.ts b/src/app/services/notification.service.spec.ts new file mode 100644 index 0000000..c4f2cd6 --- /dev/null +++ b/src/app/services/notification.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { NotificationService } from './notification.service'; + +describe('NotificationService', () => { + let service: NotificationService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(NotificationService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/services/notification.service.ts b/src/app/services/notification.service.ts new file mode 100644 index 0000000..9e08d8d --- /dev/null +++ b/src/app/services/notification.service.ts @@ -0,0 +1,78 @@ +import { Injectable } from '@angular/core'; +import { LoginService, Role } from './login.service'; + +export interface Notification { + text: string, + timeStamp: string, + roles: Array, + description?: string, + redirectionUrl?: string, +} + +@Injectable({ + providedIn: 'root' +}) +export class NotificationService { + + private notifications: Array = [{ + text: 'New name application has been submitted', + description: 'A new Applicant in the company name Kimao has applied for an appeal', + roles: ['Officer'], + timeStamp: '1 hour ago', + }, { + text: 'Entity Corp. flagged for investigation', + roles: ['Investigator'], + timeStamp: '2 days ago', + redirectionUrl: '/tabs/investigate-business-entities-and-individuals', + }, { + text: 'Request to create a panel', + roles: ['Hod'], + timeStamp: '1 day ago', + redirectionUrl: '/tabs/investigate-business-entities-and-individuals', + }, { + text: 'Review non-compliance for Entity Corp.', + roles: ['Investigator'], + timeStamp: '4 hours ago', + redirectionUrl: '/tabs/investigate-business-entities-and-individuals', + }, { + text: 'Concur non-compliance for Entity Corp.', + roles: ['Panel'], + timeStamp: '2 hours ago', + redirectionUrl: '/tabs/investigate-business-entities-and-individuals', + }, { + text: 'Prepare preliminary letter', + roles: ['Investigator'], + timeStamp: '2 hours ago', + redirectionUrl: '/tabs/investigate-business-entities-and-individuals', + }, { + text: 'Investigation complete', + roles: ['Investigator'], + timeStamp: '1 hours ago', + redirectionUrl: '/tabs/investigate-business-entities-and-individuals', + }, { + text: 'New name application has been submitted', + roles: ['Officer'], + description: 'A new Applicant in the company name Kimao has applied for an appeal', + timeStamp: '1 hour ago', + }, { + text: 'Request to create a new Committee', + roles: ['Officer'], + timeStamp: '2 hours ago', + }, { + text: 'Respond to non-compliance preliminary letter', + roles: ['Customer'], + timeStamp: '1 hour ago', + redirectionUrl: '/tabs/investigate-business-entities-and-individuals', + }]; + + private allowedNotifications: Array = []; + + constructor(loginService: LoginService) { + const loginRole = loginService.getLoginRole(); + this.allowedNotifications = this.notifications.filter(notification => notification.roles.includes(loginRole)); + } + + getAllowedNotifications() { + return this.allowedNotifications; + } +}