]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/admin/acq/funds/fund-tags.component.ts
LP#1904244: Angular funds interface
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / admin / acq / funds / fund-tags.component.ts
1 import {Component, OnInit, Input, ViewChild} from '@angular/core';
2 import {IdlService, IdlObject} from '@eg/core/idl.service';
3 import {EventService} from '@eg/core/event.service';
4 import {NetService} from '@eg/core/net.service';
5 import {AuthService} from '@eg/core/auth.service';
6 import {PcrudService} from '@eg/core/pcrud.service';
7 import {OrgService} from '@eg/core/org.service';
8 import {StringComponent} from '@eg/share/string/string.component';
9 import {ToastService} from '@eg/share/toast/toast.service';
10 import {ComboboxComponent} from '@eg/share/combobox/combobox.component';
11 import {ComboboxEntry} from '@eg/share/combobox/combobox.component';
12 import {Observable} from 'rxjs';
13 import {map} from 'rxjs/operators';
14
15 @Component({
16     selector: 'eg-fund-tags',
17     templateUrl: './fund-tags.component.html'
18 })
19 export class FundTagsComponent implements OnInit {
20
21     @Input() fundId: number;
22     @Input() fundOwner: number;
23
24     @ViewChild('addSuccessString', { static: true }) addSuccessString: StringComponent;
25     @ViewChild('addErrorString', { static: true }) addErrorString: StringComponent;
26     @ViewChild('removeSuccessString', { static: true }) removeSuccessString: StringComponent;
27     @ViewChild('removeErrorString', { static: true }) removeErrorString: StringComponent;
28     @ViewChild('tagSelector', { static: false }) tagSelector: ComboboxComponent;
29
30     tagMaps: IdlObject[];
31     newTag: ComboboxEntry = null;
32     tagSelectorDataSource: (term: string) => Observable<ComboboxEntry>;
33
34     constructor(
35         private idl: IdlService,
36         private evt: EventService,
37         private net: NetService,
38         private auth: AuthService,
39         private pcrud: PcrudService,
40         private org: OrgService,
41         private toast: ToastService
42     ) {}
43
44     ngOnInit() {
45         this._loadTagMaps();
46         this.tagSelectorDataSource = term => {
47             const field = 'name';
48             const args = {};
49             const extra_args = { order_by : {} };
50             args[field] = {'ilike': `%${term}%`}; // could -or search on label
51             args['owner'] = this.org.ancestors(this.fundOwner, true);
52             extra_args['order_by']['acqft'] = field;
53             extra_args['limit'] = 100;
54             extra_args['flesh'] = 2;
55             const flesh_fields: Object = {};
56             flesh_fields['acqft'] = ['owner'];
57             extra_args['flesh_fields'] = flesh_fields;
58             return this.pcrud.search('acqft', args, extra_args).pipe(map(data => {
59                 return {
60                     id: data.id(),
61                     label: data.name() + ' (' + data.owner().shortname() + ')',
62                     fm: data
63                 };
64             }));
65         };
66     }
67
68     _loadTagMaps() {
69         this.tagMaps = [];
70         this.pcrud.search('acqftm', { fund: this.fundId }, {
71             flesh: 2,
72             flesh_fields: {
73                 acqftm: ['tag'],
74                 acqft:  ['owner']
75             }
76         }).subscribe(
77             res => this.tagMaps.push(res),
78             err => {},
79             () => this.tagMaps.sort((a, b) => {
80                 return a.tag().name() < b.tag().name() ? -1 : 1;
81             })
82         );
83     }
84
85     checkNewTagAlreadyMapped(): boolean {
86         if ( this.newTag == null) { return false; }
87         const matches: IdlObject[] = this.tagMaps.filter(tm => tm.tag().id() === this.newTag.id);
88         return matches.length > 0 ? true : false;
89     }
90
91     addTagMap() {
92         const ftm = this.idl.create('acqftm');
93         ftm.tag(this.newTag.id);
94         ftm.fund(this.fundId);
95         this.pcrud.create(ftm).subscribe(
96             ok => {
97               this.addSuccessString.current()
98                 .then(str => this.toast.success(str));
99             },
100             err => {
101               this.addErrorString.current()
102                 .then(str => this.toast.danger(str));
103             },
104             () => {
105                 this.newTag = null;
106                 this.tagSelector.selectedId = null;
107                 this._loadTagMaps();
108             }
109         );
110     }
111     removeTagMap(ftm: IdlObject) {
112         this.pcrud.remove(ftm).subscribe(
113             ok => {
114               this.removeSuccessString.current()
115                 .then(str => this.toast.success(str));
116             },
117             err => {
118               this.removeErrorString.current()
119                 .then(str => this.toast.danger(str));
120             },
121             () => this._loadTagMaps()
122         );
123     }
124 }