Skip to content

Commit 85f56e5

Browse files
committed
Configuration tutorial
To be published in the June release.
1 parent f4daaa4 commit 85f56e5

File tree

11 files changed

+123
-36
lines changed

11 files changed

+123
-36
lines changed

cloudapp/src/app/app-routing.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { XmlComponent } from './xml/xml.component';
1010
import { BindComponent } from './bind/bind.component';
1111
import { StoreComponent } from './store/store.component';
1212
import { TranslateComponent } from './translate/translate.component';
13+
import { ConfigurationComponent, ConfigurationGuard } from './configuration/configuration.component';
1314

1415
const routes: Routes = [
1516
{ path: '', component: MainComponent },
@@ -22,6 +23,7 @@ const routes: Routes = [
2223
{ path: 'bind', component: BindComponent },
2324
{ path: 'store', component: StoreComponent },
2425
{ path: 'translate', component: TranslateComponent },
26+
{ path: 'configuration', component: ConfigurationComponent, canActivate: [ConfigurationGuard] },
2527
];
2628

2729
@NgModule({

cloudapp/src/app/app.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { XmlComponent } from './xml/xml.component';
1919
import { BindComponent } from './bind/bind.component';
2020
import { StoreComponent } from './store/store.component';
2121
import { TranslateComponent } from './translate/translate.component';
22+
import { ConfigurationComponent } from './configuration/configuration.component';
2223

2324
export function getToastrModule() {
2425
return ToastrModule.forRoot({
@@ -40,7 +41,8 @@ export function getToastrModule() {
4041
XmlComponent,
4142
BindComponent,
4243
StoreComponent,
43-
TranslateComponent
44+
TranslateComponent,
45+
ConfigurationComponent
4446
],
4547
imports: [
4648
MaterialModule,

cloudapp/src/app/bind/bind.component.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { Component, OnInit, OnDestroy } from '@angular/core';
22
import { AppService } from '../app.service';
33
import { FormGroup } from '@angular/forms';
4-
import { CloudAppRestService, CloudAppEventsService, PageInfo, HttpMethod } from '@exlibris/exl-cloudapp-angular-lib';
4+
import { CloudAppRestService, CloudAppEventsService, PageInfo, HttpMethod, FormGroupUtil } from '@exlibris/exl-cloudapp-angular-lib';
55
import { Subscription } from 'rxjs';
66
import { finalize, switchMap, tap } from 'rxjs/operators';
77
import { ToastrService } from 'ngx-toastr';
8-
import { toFormGroup } from '../utils'
98

109
@Component({
1110
selector: 'app-bind',
@@ -30,7 +29,7 @@ export class BindComponent implements OnInit, OnDestroy {
3029
const entities = (pageInfo.entities||[]).filter(e=>e.type=='ITEM');
3130
if (entities.length > 0) {
3231
this.restService.call(entities[0].link)
33-
.subscribe(res=>this.form = toFormGroup(res));
32+
.subscribe(res=>this.form = FormGroupUtil.toFormGroup(res));
3433
} else {
3534
this.form = null;
3635
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<form [formGroup]="form">
2+
<section class="config-section">
3+
<mat-form-field>
4+
<input matInput placeholder="Service URL" formControlName="serviceUrl">
5+
</mat-form-field>
6+
</section>
7+
</form>
8+
9+
<div class="commands-container">
10+
<button mat-stroked-button type="button" color="primary" (click)="save()" [disabled]="!form.dirty">Save</button>
11+
<mat-spinner diameter="30" class="spinner" *ngIf="saving"></mat-spinner>
12+
</div>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.config-section {
2+
display: flex;
3+
align-content: center;
4+
align-items: center;
5+
margin: 10px 0;
6+
width: 100%;
7+
}
8+
9+
mat-form-field {
10+
width: 100%;
11+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
}

cloudapp/src/app/main/main.component.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ <h1>
1212
<li><a [routerLink]="['xml']">Working with bibliographic records in XML</a></li>
1313
<li><a [routerLink]="['bind']">Model binding with REST objects</a></li>
1414
<li><a [routerLink]="['store']">Using the Store Service</a></li>
15-
<li><a [routerLink]="['translate']">Translating your app</a></li>
15+
<li><a [routerLink]="['translate']">Translating your app</a></li>
16+
<li *ngIf="isAdmin"><a [routerLink]="['configuration']">Using the Configuration Service</a></li>
1617
</ul>
1718
</section>
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
import { Component, OnInit } from '@angular/core';
22
import { AppService } from '../app.service';
3+
import { CloudAppEventsService } from '@exlibris/exl-cloudapp-angular-lib';
34

45
@Component({
56
selector: 'app-main',
67
templateUrl: './main.component.html',
78
styleUrls: ['./main.component.scss']
89
})
910
export class MainComponent implements OnInit {
11+
isAdmin = false;
1012

11-
constructor(private appService: AppService) { }
13+
constructor(
14+
private appService: AppService,
15+
private eventsService: CloudAppEventsService
16+
) { }
1217

1318
ngOnInit() {
1419
this.appService.setTitle('');
20+
this.eventsService.getInitData().subscribe(data=>this.isAdmin = data.user.isAdmin)
1521
}
1622

1723
}

cloudapp/src/app/store/store.component.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ import { Component, OnInit } from '@angular/core';
22
import { AppService } from '../app.service';
33
import { FormGroup, FormControl } from '@angular/forms';
44
import { debounceTime, switchMap } from 'rxjs/operators';
5-
import { CloudAppStoreService } from '@exlibris/exl-cloudapp-angular-lib';
6-
import { toFormGroup } from '../utils';
5+
import { CloudAppStoreService, FormGroupUtil } from '@exlibris/exl-cloudapp-angular-lib';
76

8-
const FORM_NAME = 'StoreForm';
7+
const KEY = 'StoreForm';
98

109
@Component({
1110
selector: 'app-store',
@@ -26,14 +25,14 @@ export class StoreComponent implements OnInit {
2625
content: new FormControl()
2726
});
2827

29-
this.storeService.get(FORM_NAME).subscribe( value => {
28+
this.storeService.get(KEY).subscribe( value => {
3029
if (value) {
31-
this.form = toFormGroup(value);
30+
this.form = FormGroupUtil.toFormGroup(value);
3231
}
3332

3433
this.form.valueChanges.pipe(
3534
debounceTime(500),
36-
switchMap(val=>this.storeService.set(FORM_NAME, val))
35+
switchMap(val=>this.storeService.set(KEY, val))
3736
).subscribe()
3837
})
3938
}

cloudapp/src/app/utils.ts

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)