|
| 1 | +import { Component, OnInit, OnDestroy } from '@angular/core'; |
| 2 | +import { AppService } from '../app.service'; |
| 3 | +import { FormGroup } from '@angular/forms'; |
| 4 | +import { CloudAppRestService, CloudAppEventsService, PageInfo, HttpMethod } from '@exlibris/exl-cloudapp-angular-lib'; |
| 5 | +import { Subscription } from 'rxjs'; |
| 6 | +import { finalize, switchMap, tap } from 'rxjs/operators'; |
| 7 | +import { ToastrService } from 'ngx-toastr'; |
| 8 | +import { toFormGroup } from '../utils' |
| 9 | + |
| 10 | +@Component({ |
| 11 | + selector: 'app-bind', |
| 12 | + templateUrl: './bind.component.html', |
| 13 | + styleUrls: ['./bind.component.scss'] |
| 14 | +}) |
| 15 | +export class BindComponent implements OnInit, OnDestroy { |
| 16 | + private pageLoad$: Subscription; |
| 17 | + form: FormGroup; |
| 18 | + saving = false; |
| 19 | + |
| 20 | + constructor( |
| 21 | + private appService: AppService, |
| 22 | + private restService: CloudAppRestService, |
| 23 | + private eventsService: CloudAppEventsService, |
| 24 | + private toastr: ToastrService |
| 25 | + ) { } |
| 26 | + |
| 27 | + ngOnInit() { |
| 28 | + this.appService.setTitle('Model Binding'); |
| 29 | + this.pageLoad$ = this.eventsService.onPageLoad((pageInfo: PageInfo) => { |
| 30 | + const entities = (pageInfo.entities||[]).filter(e=>e.type=='ITEM'); |
| 31 | + if (entities.length > 0) { |
| 32 | + this.restService.call(entities[0].link) |
| 33 | + .subscribe(res=>this.form = toFormGroup(res) as FormGroup); |
| 34 | + } |
| 35 | + }); |
| 36 | + } |
| 37 | + |
| 38 | + ngOnDestroy() { |
| 39 | + this.pageLoad$.unsubscribe(); |
| 40 | + } |
| 41 | + |
| 42 | + save() { |
| 43 | + this.saving = true; |
| 44 | + this.restService.call({ |
| 45 | + url: this.form.get('link').value, |
| 46 | + requestBody: this.form.value, |
| 47 | + method: HttpMethod.PUT |
| 48 | + }).pipe( |
| 49 | + switchMap(res => this.eventsService.refreshPage()), |
| 50 | + tap(() => this.toastr.success('Item updated')), |
| 51 | + finalize(() => this.saving=false) |
| 52 | + ) |
| 53 | + .subscribe({ |
| 54 | + error: e => this.toastr.error(e.message) |
| 55 | + }); |
| 56 | + } |
| 57 | +} |
0 commit comments