LP1847800 Admin grids support config_field links
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / admin / local / admin-carousel.component.ts
1 import {Component, Input, ViewChild, OnInit} from '@angular/core';
2 import {Location} from '@angular/common';
3 import {FormatService} from '@eg/core/format.service';
4 import {AdminPageComponent} from '@eg/staff/share/admin-page/admin-page.component';
5 import {ActivatedRoute} from '@angular/router';
6 import {IdlService, IdlObject} from '@eg/core/idl.service';
7 import {ToastService} from '@eg/share/toast/toast.service';
8 import {PcrudService} from '@eg/core/pcrud.service';
9 import {OrgService} from '@eg/core/org.service';
10 import {PermService} from '@eg/core/perm.service';
11 import {AuthService} from '@eg/core/auth.service';
12 import {NetService} from '@eg/core/net.service';
13 import {StringComponent} from '@eg/share/string/string.component';
14
15 @Component({
16     templateUrl: './admin-carousel.component.html'
17 })
18
19 export class AdminCarouselComponent extends AdminPageComponent implements OnInit {
20
21     idlClass = 'cc';
22     classLabel: string;
23
24     refreshSelected: (idlThings: IdlObject[]) => void;
25     createNew: () => void;
26     deleteSelected: (idlThings: IdlObject[]) => void;
27
28     @ViewChild('refreshString', { static: true }) refreshString: StringComponent;
29     @ViewChild('refreshErrString', { static: true }) refreshErrString: StringComponent;
30
31     constructor(
32         route: ActivatedRoute,
33         ngLocation: Location,
34         format: FormatService,
35         idl: IdlService,
36         org: OrgService,
37         auth: AuthService,
38         pcrud: PcrudService,
39         perm: PermService,
40         toast: ToastService,
41         private net: NetService
42     ) {
43         super(route, ngLocation, format, idl, org, auth, pcrud, perm, toast);
44     }
45
46     ngOnInit() {
47         super.ngOnInit();
48
49         this.classLabel = this.idlClassDef.label;
50         this.includeOrgDescendants = true;
51
52
53         this.createNew = () => {
54             super.createNew();
55         };
56
57         this.deleteSelected = (idlThings: IdlObject[]) => {
58             super.deleteSelected(idlThings);
59         };
60
61         this.refreshSelected = (idlThings: IdlObject[]) =>  {
62             idlThings.forEach(cc => {
63                 if (cc.type().automatic() === 't') {
64                     this.net.request(
65                         'open-ils.actor',
66                         'open-ils.actor.carousel.refresh',
67                         this.auth.token(), cc.id()
68                     ).toPromise(); // fire and forget, as this could take a couple minutes
69                     this.refreshString.current({ name: cc.name() }).then(str => this.toast.success(str));
70                 } else {
71                     this.refreshErrString.current({ name: cc.name() }).then(str => this.toast.warning(str));
72                 }
73             });
74         };
75     }
76
77     mungeCarousel(editMode: string, rec: IdlObject) {
78         if (editMode === 'create') {
79             rec.creator(this.auth.user().id());
80         }
81         rec.editor(this.auth.user().id());
82         rec.edit_time('now');
83
84         // convert empty string to nulls as needed
85         // for int[] columns
86         if (rec.owning_lib_filter() === '') {
87             rec.owning_lib_filter(null);
88         }
89         if (rec.copy_location_filter() === '') {
90             rec.copy_location_filter(null);
91         }
92     }
93
94     postSave(rec: IdlObject) {
95         if (rec._isfieldmapper) {
96             // if we got an actual IdlObject back, the
97             // record had just been created, not just
98             // edited. therefore, we probably need
99             if (rec.bucket() == null) {
100                 const bucket = this.idl.create('cbreb');
101                 bucket.owner(this.auth.user().id());
102                 bucket.name('System-generated bucket for carousel: ' + rec.id()); // FIXME I18N
103                 bucket.btype('carousel');
104                 bucket.pub('t');
105                 bucket.owning_lib(rec.owner());
106                 rec.bucket(bucket);
107                 this.net.request(
108                     'open-ils.actor',
109                     'open-ils.actor.container.create',
110                     this.auth.token(), 'biblio', bucket
111                 ).toPromise().then(
112                     newBucket => {
113                         const ccou = this.idl.create('ccou');
114                         ccou.carousel(rec.id());
115                         ccou.org_unit(rec.owner());
116                         ccou.seq(0);
117                         rec.bucket(newBucket);
118                         this.pcrud.create(ccou).subscribe(
119                             ok => {
120                                 this.pcrud.update(rec).subscribe(
121                                     ok2 => console.debug('updated'),
122                                     err => console.error(err),
123                                     () => { this.grid.reload(); }
124                                 );
125                             },
126                             err => console.error(err),
127                             () => { this.grid.reload(); }
128                         );
129                     }
130                 );
131             }
132         }
133     }
134 }