/** Angular Imports */ import { Component , OnInit , Input } from '@angular/core' ; import { MatDialog } from '@angular/material/dialog' ; import { Router } from '@angular/router' ; /** Custom Components */ import { KeyboardShortcutsDialogComponent } from 'app/shared/keyboard-shortcuts-dialog/keyboard-shortcuts-dialog.component' ; /** Custom Services */ import { AuthenticationService } from '../../authentication/authentication.service' ; /** Custom Imports */ import { frequentActivities } from './frequent-activities' ; /**
 * Sidenav component.
 */ @ Component ( { selector : 'mifosx-sidenav' , templateUrl : './sidenav.component.html' , styleUrls : [ './sidenav.component.scss' ] } ) 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 param { MatDialog } dialog param { AuthenticationService } authenticationService constructor ( private router , public dialog , private authenticationService ) { this . userActivity = JSON . parse ( localStorage . getItem ( 'mifosXLocation' ) ) ; } /**
     * Sets the username of the authenticated user.
     */ ngOnInit ( ) { const credentials = this . authenticationService . getCredentials ( ) ; this . username = credentials . username ; this . setMappedAcitivites ( ) ; } /**
     * Logs out the authenticated user and redirects to login page.
     */ logout ( ) { this . authenticationService . logout ( ) . subscribe ( ( ) => this . router . navigate ( [ '/login' ] , { replaceUrl : true } ) ) ; } /**
     * Opens Mifos JIRA Wiki page.
     */ help ( ) { window . open ( 'https://mifosforge.jira.com/wiki/spaces/docs/pages/52035622/User+Manual' , '_blank' ) ; } /**
     * Opens Keyboard shortcuts dialog.
     */ showKeyboardShortcuts ( ) { const dialogRef = this . dialog . open ( KeyboardShortcutsDialogComponent ) ; dialogRef . afterClosed ( ) . subscribe ( ( response ) => { } ) ; } /**
     * Returns top three frequent activities.
     */ getFrequentActivities ( ) { const frequencyCounts = { } ; 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 , b ) => b [ 1 ] - a [ 1 ] ) . map ( ( entry ) => entry [ 0 ] ) . filter ( ( activity ) => ! [ '/' , '/login' , '/home' , '/dashboard' ] . includes ( activity ) ) . slice ( 0 , 3 ) ; return topThreeFrequentActivities ; } /**
     * Maps frequently accessed urls to button objects.
     */ setMappedAcitivites ( ) { const activities = this . getFrequentActivities ( ) ; activities . forEach ( ( activity ) => { 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 ( ) ; } param { string } path pushActivity ( path ) { const activity = this . frequentActivities . find ( ( entry ) => entry . path === path ) ; if ( ! this . mappedActivities . includes ( activity ) ) { this . mappedActivities . push ( activity ) ; } } }