LP1851831 Group perm editor null descriptions OK
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / cat / vandelay / match-set-quality.component.ts
1 import {Component, OnInit, ViewChild, AfterViewInit, Input} from '@angular/core';
2 import {Observable, of} from 'rxjs';
3 import {IdlObject, IdlService} from '@eg/core/idl.service';
4 import {PcrudService} from '@eg/core/pcrud.service';
5 import {NetService} from '@eg/core/net.service';
6 import {AuthService} from '@eg/core/auth.service';
7 import {OrgService} from '@eg/core/org.service';
8 import {GridComponent} from '@eg/share/grid/grid.component';
9 import {GridDataSource} from '@eg/share/grid/grid';
10 import {Pager} from '@eg/share/util/pager';
11 import {MatchSetNewPointComponent} from './match-set-new-point.component';
12
13 @Component({
14   selector: 'eg-match-set-quality',
15   templateUrl: 'match-set-quality.component.html'
16 })
17 export class MatchSetQualityComponent implements OnInit {
18
19     // Match set arrives from parent async.
20     matchSet_: IdlObject;
21     @Input() set matchSet(ms: IdlObject) {
22         this.matchSet_ = ms;
23         if (ms) {
24             this.matchSetType = ms.mtype();
25             if (this.grid) {
26                 this.grid.reload();
27             }
28         }
29     }
30
31     newPointType: string;
32     matchSetType: string;
33     dataSource: GridDataSource;
34     @ViewChild('newPoint') newPoint: MatchSetNewPointComponent;
35     @ViewChild('grid') grid: GridComponent;
36     deleteSelected: (rows: IdlObject[]) => void;
37
38     constructor(
39         private idl: IdlService,
40         private pcrud: PcrudService,
41         private net: NetService,
42         private auth: AuthService,
43         private org: OrgService
44     ) {
45
46         this.dataSource = new GridDataSource();
47         this.dataSource.getRows = (pager: Pager, sort: any[]) => {
48
49             if (!this.matchSet_) {
50                 return of();
51             }
52
53             const orderBy: any = {};
54             if (sort.length) {
55                 orderBy.vmsq = sort[0].name + ' ' + sort[0].dir;
56             }
57
58             const searchOps = {
59                 offset: pager.offset,
60                 limit: pager.limit,
61                 order_by: orderBy
62             };
63
64             const search = {match_set: this.matchSet_.id()};
65             return this.pcrud.search('vmsq', search, searchOps);
66         };
67
68         this.deleteSelected = (rows: any[]) => {
69             this.pcrud.remove(rows).subscribe(
70                 ok  => console.log('deleted ', ok),
71                 err => console.error(err),
72                 ()  => this.grid.reload()
73             );
74         };
75     }
76
77     ngOnInit() {}
78
79     addQuality() {
80         const quality = this.idl.create('vmsq');
81         const values = this.newPoint.values;
82
83         quality.match_set(this.matchSet_.id());
84         quality.quality(values.matchScore);
85         quality.value(values.value);
86
87         if (values.recordAttr) {
88             quality.svf(values.recordAttr);
89         } else {
90             quality.tag(values.marcTag);
91             quality.subfield(values.marcSf);
92         }
93
94         this.pcrud.create(quality).subscribe(
95             ok  => console.debug('created ', ok),
96             err => console.error(err),
97             ()  => {
98                 this.newPointType = null;
99                 this.grid.reload();
100             }
101         );
102     }
103 }
104