1+ import { Component , OnInit , Injectable } from '@angular/core' ;
2+ import { AppService } from '../app.service' ;
3+ import { FormBuilder , FormGroup } from '@angular/forms' ;
4+ import { CloudAppConfigService , CloudAppEventsService } from '@exlibris/exl-cloudapp-angular-lib' ;
5+ import { ToastrService } from 'ngx-toastr' ;
6+ import { CanActivate , Router , ActivatedRouteSnapshot , RouterStateSnapshot } from '@angular/router' ;
7+ import { Observable } from 'rxjs' ;
8+ import { map } from 'rxjs/operators' ;
9+
10+ @Component ( {
11+ selector : 'app-configuration' ,
12+ templateUrl : './configuration.component.html' ,
13+ styleUrls : [ './configuration.component.scss' ]
14+ } )
15+ export class ConfigurationComponent implements OnInit {
16+ form : FormGroup ;
17+ saving = false ;
18+
19+ constructor (
20+ private appService : AppService ,
21+ private fb : FormBuilder ,
22+ private configService : CloudAppConfigService ,
23+ private toastr : ToastrService
24+ ) { }
25+
26+ ngOnInit ( ) {
27+ this . appService . setTitle ( 'Configuration' ) ;
28+ this . form = this . fb . group ( {
29+ serviceUrl : this . fb . control ( '' )
30+ } ) ;
31+ this . load ( ) ;
32+ }
33+
34+ load ( ) {
35+ this . configService . getAsFormGroup ( ) . subscribe ( settings => {
36+ if ( Object . keys ( settings . value ) . length != 0 ) {
37+ this . form = settings ;
38+ }
39+ } ) ;
40+ }
41+
42+ save ( ) {
43+ this . saving = true ;
44+ this . configService . set ( this . form . value ) . subscribe (
45+ response => {
46+ this . toastr . success ( 'Settings successfully saved.' ) ;
47+ this . form . markAsPristine ( ) ;
48+ } ,
49+ err => this . toastr . error ( err . message ) ,
50+ ( ) => this . saving = false
51+ ) ;
52+ }
53+
54+ }
55+
56+ @Injectable ( {
57+ providedIn : 'root' ,
58+ } )
59+ export class ConfigurationGuard implements CanActivate {
60+ constructor (
61+ private eventsService : CloudAppEventsService ,
62+ private router : Router
63+ ) { }
64+ canActivate (
65+ next : ActivatedRouteSnapshot ,
66+ state : RouterStateSnapshot ) : Observable < boolean > {
67+ return this . eventsService . getInitData ( ) . pipe ( map ( data => {
68+ if ( ! data . user . isAdmin ) {
69+ this . router . navigate ( [ '/' ] ) ;
70+ return false ;
71+ }
72+ return true ;
73+ } ) )
74+ }
75+ }
0 commit comments