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