1 import {Component, 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 {ConfirmDialogComponent} from '@eg/share/dialog/confirm.component';
18 templateUrl: './admin-carousel.component.html'
21 export class AdminCarouselComponent extends AdminPageComponent implements OnInit {
26 refreshSelected: (idlThings: IdlObject[]) => void;
27 mungeCarousel: (editMode: string, rec: IdlObject) => void;
28 postSave: (rec: IdlObject) => void;
29 createNew: () => void;
30 deleteSelected: (idlThings: IdlObject[]) => void;
31 cellTextGenerator: GridCellTextGenerator;
33 @ViewChild('refreshString', { static: true }) refreshString: StringComponent;
34 @ViewChild('refreshErrString', { static: true }) refreshErrString: StringComponent;
35 @ViewChild('delConfirm', { static: true }) delConfirm: ConfirmDialogComponent;
38 route: ActivatedRoute,
40 format: FormatService,
47 private net: NetService
49 super(route, ngLocation, format, idl, org, auth, pcrud, perm, toast);
55 this.classLabel = this.idlClassDef.label;
56 this.includeOrgDescendants = true;
57 this.cellTextGenerator = {
58 bucket: row => row.bucket().name()
61 this.createNew = () => {
65 this.deleteSelected = (idlThings: IdlObject[]) => {
66 this.delConfirm.open().subscribe(confirmed => {
67 if (!confirmed) { return; }
68 super.doDelete(idlThings);
72 this.refreshSelected = (idlThings: IdlObject[]) => {
73 idlThings.forEach(cc => {
74 if (cc.type().automatic() === 't') {
77 'open-ils.actor.carousel.refresh',
78 this.auth.token(), cc.id()
79 ).toPromise(); // fire and forget, as this could take a couple minutes
80 this.refreshString.current({ name: cc.name() }).then(str => this.toast.success(str));
82 this.refreshErrString.current({ name: cc.name() }).then(str => this.toast.warning(str));
87 this.editSelected = (carouselFields: IdlObject[]) => {
88 // Edit each IDL thing one at a time
89 const editOneThing = (carousel: IdlObject) => {
90 if (!carousel) { return; }
92 this.showEditDialog(carousel).then(
93 () => editOneThing(carouselFields.shift()));
96 editOneThing(carouselFields.shift());
99 this.mungeCarousel = (editMode: string, rec: IdlObject) => {
100 if (editMode === 'create') {
101 rec.creator(this.auth.user().id());
103 rec.editor(this.auth.user().id());
104 rec.edit_time('now');
106 // convert empty string to nulls as needed
108 if (rec.owning_lib_filter() === '') {
109 rec.owning_lib_filter(null);
111 if (rec.copy_location_filter() === '') {
112 rec.copy_location_filter(null);
116 this.postSave = (rec: IdlObject) => {
117 if (rec._isfieldmapper) {
118 // if we got an actual IdlObject back, the
119 // record had just been created, not just
120 // edited. therefore, we probably need
121 // eslint-disable-next-line eqeqeq
122 if (rec.bucket() == null) {
123 const bucket = this.idl.create('cbreb');
124 bucket.owner(this.auth.user().id());
125 bucket.name('System-generated bucket for carousel: ' + rec.id()); // FIXME I18N
126 bucket.btype('carousel');
128 bucket.owning_lib(rec.owner());
132 'open-ils.actor.container.create',
133 this.auth.token(), 'biblio', bucket
136 const ccou = this.idl.create('ccou');
137 ccou.carousel(rec.id());
138 ccou.org_unit(rec.owner());
140 rec.bucket(newBucket);
141 this.pcrud.create(ccou).subscribe(
143 // eslint-disable-next-line rxjs/no-nested-subscribe
144 this.pcrud.update(rec).subscribe(
145 ok2 => console.debug('updated'),
146 (err: unknown) => console.error(err),
147 () => { this.grid.reload(); }
150 (err: unknown) => console.error(err),
151 () => { this.grid.reload(); }