import { Injectable } from '@angular/core' ; import { Resolve , ActivatedRouteSnapshot , Router } from '@angular/router' ; import { ContextService } from "../../services/context.service" ; import { UsersService } from "../../services/users/users.service" ; import { GroupService } from "../../services/groups/group.service" ; import { UsersUtilsService } from "../../services/users/users-utils.service" ; @ Injectable ( ) export class ContextResolve implements Resolve < any > { constructor ( private context , private router , private usersUtils , private groupService , private usersService ) { } resolve ( route ) { if ( this . context . loggedIn ( ) ) { return this . usersService . getUsers ( ) . then ( ( users ) => { this . context . setAllUsers ( users ) ; return this . groupService . getGroups ( ) ; } ) . then ( ( groups ) => { this . context . setAllGroups ( groups ) ; let user = this . usersUtils . getSelf ( ) ; this . context . setUser ( user ) ; if ( this . context . getUser ( ) ) { console . log ( 'Logging in as ' + this . context . getUser ( ) . name + ' with local storage' ) ; } else { console . log ( this . context . getUser ( ) ) ; } } ) ; } else { this . router . navigate ( [ 'base/start/login' ] ) ; return Promise . reject ( 'Unable to login' ) ; } } }