From 138ec4501493acaea9081009dada63197b075962 Mon Sep 17 00:00:00 2001 From: Karan Takalkar Date: Sun, 16 Aug 2020 18:52:18 +0530 Subject: [PATCH] feat: automate frequent activities (#1211) Fixes: #1210 --- .../core/shell/sidenav/frequent-activities.ts | 57 ++++++++++++++ .../core/shell/sidenav/sidenav.component.html | 16 +--- .../core/shell/sidenav/sidenav.component.ts | 76 ++++++++++++++++++- src/app/shared/icons.module.ts | 8 +- 4 files changed, 141 insertions(+), 16 deletions(-) create mode 100644 src/app/core/shell/sidenav/frequent-activities.ts diff --git a/src/app/core/shell/sidenav/frequent-activities.ts b/src/app/core/shell/sidenav/frequent-activities.ts new file mode 100644 index 000000000..164c5f25f --- /dev/null +++ b/src/app/core/shell/sidenav/frequent-activities.ts @@ -0,0 +1,57 @@ +export const frequentActivities: any[] = [ + { + path: '/clients', + icon: 'user', + toolTip: 'Clients' + }, + { + path: '/groups', + icon: 'users', + toolTip: 'Groups' + }, + { + path: '/centers', + icon: 'building', + toolTip: 'Centers' + }, + { + path: '/accounting', + icon: 'money-bill-alt', + toolTip: 'Accounting' + }, + { + path: '/reports', + icon: 'chart-bar', + toolTip: 'Reports' + }, + { + path: '/users', + icon: 'users', + toolTip: 'Users' + }, + { + path: '/organization', + icon: 'id-badge', + toolTip: 'Organization' + }, + { + path: '/system', + icon: 'cog', + toolTip: 'System' + }, + { + path: '/products', + icon: 'book', + toolTip: 'Products' + }, + { + path: '/templates', + icon: 'address-card', + toolTip: 'Templates' + }, + { + path: '/self-service', + icon: 'laptop', + toolTip: 'Self Service' + }, +]; diff --git a/src/app/core/shell/sidenav/sidenav.component.html b/src/app/core/shell/sidenav/sidenav.component.html index d029508c9..42ed00c23 100644 --- a/src/app/core/shell/sidenav/sidenav.component.html +++ b/src/app/core/shell/sidenav/sidenav.component.html @@ -38,19 +38,9 @@ Frequently Accessed
-
- -
-
- -
-
-
diff --git a/src/app/core/shell/sidenav/sidenav.component.ts b/src/app/core/shell/sidenav/sidenav.component.ts index 10866d60a..8229f6efe 100644 --- a/src/app/core/shell/sidenav/sidenav.component.ts +++ b/src/app/core/shell/sidenav/sidenav.component.ts @@ -5,6 +5,9 @@ import { Router } from '@angular/router'; /** Custom Services */ import { AuthenticationService } from '../../authentication/authentication.service'; +/** Custom Imports */ +import { frequentActivities } from './frequent-activities'; + /** * Sidenav component. */ @@ -17,15 +20,24 @@ export class SidenavComponent implements OnInit { /** True if sidenav is in collapsed state. */ @Input() sidenavCollapsed: boolean; + /** Username of authenticated user. */ username: string; + /** Array of all user activities */ + userActivity: string[]; + /** Mapped Activites */ + mappedActivities: any[] = []; + /** Collection of possible frequent activities */ + frequentActivities: any[] = frequentActivities; /** * @param {Router} router Router for navigation. * @param {AuthenticationService} authenticationService Authentication Service. */ constructor(private router: Router, - private authenticationService: AuthenticationService) { } + private authenticationService: AuthenticationService) { + this.userActivity = JSON.parse(localStorage.getItem('mifosXLocation')); + } /** * Sets the username of the authenticated user. @@ -33,6 +45,7 @@ export class SidenavComponent implements OnInit { ngOnInit() { const credentials = this.authenticationService.getCredentials(); this.username = credentials.username; + this.setMappedAcitivites(); } /** @@ -50,4 +63,65 @@ export class SidenavComponent implements OnInit { window.open('https://mifosforge.jira.com/wiki/spaces/docs/pages/52035622/User+Manual', '_blank'); } + /** + * Returns top three frequent activities. + */ + getFrequentActivities() { + const frequencyCounts: any = {}; + let index = this.userActivity.length; + while (index) { + frequencyCounts[this.userActivity[--index]] = (frequencyCounts[this.userActivity[index]] || 0) + 1; + } + const frequencyCountsArray = Object.entries(frequencyCounts); + const topThreeFrequentActivities = + frequencyCountsArray + .sort((a: any, b: any) => b[1] - a[1]) + .map((entry: any[]) => entry[0]) + .filter((activity: string) => !['/', '/login', '/home', '/dashboard'].includes(activity)) + .slice(0, 3); + return topThreeFrequentActivities; + } + + /** + * Maps frequently accessed urls to button objects. + */ + setMappedAcitivites() { + const activities: string[] = this.getFrequentActivities(); + activities.forEach((activity: string) => { + if (activity.includes('/clients')) { + this.pushActivity('/clients'); + } else if (activity.includes('/groups')) { + this.pushActivity('/groups'); + } else if (activity.includes('/centers')) { + this.pushActivity('/centers'); + } else if (activity.includes('/accounting')) { + this.pushActivity('/accounting'); + } else if (activity.includes('/reports')) { + this.pushActivity('/reports'); + } else if (activity.includes('/users')) { + this.pushActivity('/users'); + } else if (activity.includes('/organization')) { + this.pushActivity('/organization'); + } else if (activity.includes('/system')) { + this.pushActivity('/system'); + } else if (activity.includes('/products')) { + this.pushActivity('/products'); + } else if (activity.includes('/templates')) { + this.pushActivity('/templates'); + } else if (activity.includes('/self-service')) { + this.pushActivity('/self-service'); + } + }); + this.mappedActivities.reverse(); + } + + /** + * Pushes activity to mapped activities + * @param {string} path Activity Path + */ + pushActivity(path: string) { + const activity = this.frequentActivities.find((entry: any) => entry.path === path); + this.mappedActivities.push(activity); + } + } diff --git a/src/app/shared/icons.module.ts b/src/app/shared/icons.module.ts index f0f7366e9..5c0f6a1bc 100644 --- a/src/app/shared/icons.module.ts +++ b/src/app/shared/icons.module.ts @@ -94,7 +94,9 @@ import { faFlag, faArrowUp, faArrowDown, - faFolderOpen + faFolderOpen, + faIdBadge, + faAddressCard } from '@fortawesome/free-solid-svg-icons'; /** Add icons to the library for convenient access in other components. */ @@ -188,7 +190,9 @@ library.add( faFlag, faArrowUp, faArrowDown, - faFolderOpen + faFolderOpen, + faIdBadge, + faAddressCard ); /**