1 import {Component, Input, ViewChild, OnInit, TemplateRef} from '@angular/core';
2 import {ActivatedRoute} from '@angular/router';
3 import {from, merge, Observable} from 'rxjs';
4 import {DialogComponent} from '@eg/share/dialog/dialog.component';
5 import {AuthService} from '@eg/core/auth.service';
6 import {NetService} from '@eg/core/net.service';
7 import {EventService} from '@eg/core/event.service';
8 import {OrgService} from '@eg/core/org.service';
9 import {PcrudService} from '@eg/core/pcrud.service';
10 import {Pager} from '@eg/share/util/pager';
11 import {NgbModal, NgbModalOptions} from '@ng-bootstrap/ng-bootstrap';
12 import {GridDataSource} from '@eg/share/grid/grid';
13 import {GridComponent} from '@eg/share/grid/grid.component';
14 import {IdlObject, IdlService} from '@eg/core/idl.service';
15 import {StringComponent} from '@eg/share/string/string.component';
16 import {FmRecordEditorComponent} from '@eg/share/fm-editor/fm-editor.component';
17 import {ToastService} from '@eg/share/toast/toast.service';
18 import {CourseService} from '@eg/staff/share/course.service';
21 selector: 'eg-course-associate-material-dialog',
22 templateUrl: './course-associate-material.component.html'
25 export class CourseAssociateMaterialComponent extends DialogComponent implements OnInit {
26 @Input() currentCourse: IdlObject;
27 @Input() courseId: any;
28 @Input() courseIsArchived: string;
29 @Input() displayMode: string;
30 materials: any[] = [];
31 @ViewChild('editDialog', { static: true }) editDialog: FmRecordEditorComponent;
32 @ViewChild('materialsGrid', {static: false}) materialsGrid: GridComponent;
33 @ViewChild('materialDeleteFailedString', { static: true })
34 materialDeleteFailedString: StringComponent;
35 @ViewChild('materialDeleteSuccessString', { static: true })
36 materialDeleteSuccessString: StringComponent;
37 @ViewChild('materialAddSuccessString', { static: true })
38 materialAddSuccessString: StringComponent;
39 @ViewChild('materialAddFailedString', { static: true })
40 materialAddFailedString: StringComponent;
41 @ViewChild('materialEditSuccessString', { static: true })
42 materialEditSuccessString: StringComponent;
43 @ViewChild('materialEditFailedString', { static: true })
44 materialEditFailedString: StringComponent;
45 @ViewChild('materialAddDifferentLibraryString', { static: true })
46 materialAddDifferentLibraryString: StringComponent;
47 materialsDataSource: GridDataSource;
48 @Input() barcodeInput: string;
49 @Input() relationshipInput: string;
50 @Input() tempCallNumber: string;
51 @Input() tempStatus: number;
52 @Input() tempLocation: number;
53 @Input() tempCircMod: string;
54 @Input() isModifyingStatus: boolean;
55 @Input() isModifyingCircMod: boolean;
56 @Input() isModifyingCallNumber: boolean;
57 @Input() isModifyingLocation: boolean;
60 associateBriefRecord: (newRecord: string) => void;
61 associateElectronicBibRecord: () => void;
64 private auth: AuthService,
65 private course: CourseService,
66 private event: EventService,
67 private idl: IdlService,
68 private net: NetService,
69 private org: OrgService,
70 private pcrud: PcrudService,
71 private route: ActivatedRoute,
72 private toast: ToastService,
73 private modal: NgbModal
76 this.materialsDataSource = new GridDataSource();
78 this.materialsDataSource.getRows = (pager: Pager, sort: any[]) => {
79 return this.net.request(
81 'open-ils.courses.course_materials.retrieve.fleshed',
82 {course: this.courseId}
88 this.associateBriefRecord = (newRecord: string) => {
89 return this.net.request(
91 'open-ils.courses.attach.biblio_record',
95 this.relationshipInput
97 this.materialsGrid.reload();
98 this.materialAddSuccessString.current()
99 .then(str => this.toast.success(str));
103 this.associateElectronicBibRecord = () => {
104 return this.net.request(
106 'open-ils.courses.attach.electronic_resource',
110 this.relationshipInput
112 this.materialsGrid.reload();
113 this.materialAddSuccessString.current()
114 .then(str => this.toast.success(str));
120 isDialog(): boolean {
121 return this.displayMode === 'dialog';
124 editSelectedMaterials(itemFields: IdlObject[]) {
125 // Edit each IDL thing one at a time
126 const editOneThing = (item: IdlObject) => {
127 if (!item) { return; }
129 this.showEditDialog(item).then(
130 () => editOneThing(itemFields.shift()));
133 editOneThing(itemFields.shift());
136 showEditDialog(courseMaterial: IdlObject): Promise<any> {
137 this.editDialog.mode = 'update';
138 this.editDialog.recordId = courseMaterial.id();
139 return new Promise((resolve, reject) => {
140 this.editDialog.open({size: 'lg'}).subscribe(
142 this.materialEditSuccessString.current()
143 .then(str => this.toast.success(str));
144 this.pcrud.retrieve('acmcm', result).subscribe(material => {
145 if (material.course() !== this.courseId) {
146 this.materialsGrid.reload();
148 courseMaterial.relationship = material.relationship();
154 this.materialEditFailedString.current()
155 .then(str => this.toast.danger(str));
162 associateItem(barcode, relationship) {
165 barcode: barcode.trim(),
166 relationship: relationship,
167 isModifyingCallNumber: this.isModifyingCallNumber,
168 isModifyingCircMod: this.isModifyingCircMod,
169 isModifyingLocation: this.isModifyingLocation,
170 isModifyingStatus: this.isModifyingStatus,
171 tempCircMod: this.tempCircMod,
172 tempLocation: this.tempLocation,
173 tempStatus: this.tempStatus,
174 currentCourse: this.currentCourse
176 this.barcodeInput = null;
178 this.pcrud.search('acp', {barcode: args.barcode}, {
179 flesh: 3, flesh_fields: {acp: ['call_number']}
180 }).subscribe(item => {
181 const associatedMaterial = this.course.associateMaterials(item, args);
182 associatedMaterial.material.then(res => {
183 item = associatedMaterial.item;
184 let new_cn = item.call_number().label();
185 if (this.tempCallNumber) { new_cn = this.tempCallNumber; }
186 this.course.updateItem(item, this.currentCourse.owning_lib(),
187 new_cn, args.isModifyingCallNumber
189 this.materialsGrid.reload();
190 if (item.circ_lib() !== this.currentCourse.owning_lib()) {
191 this.materialAddDifferentLibraryString.current()
192 .then(str => this.toast.warning(str));
194 this.materialAddSuccessString.current()
195 .then(str => this.toast.success(str));
199 this.materialAddFailedString.current()
200 .then(str => this.toast.danger(str));
206 deleteSelectedMaterials(items) {
207 let deleteRequest$ = this.course.detachMaterials(items);
208 merge(...deleteRequest$).subscribe(
210 this.materialDeleteSuccessString.current().then(str => this.toast.success(str));
213 this.materialDeleteFailedString.current()
214 .then(str => this.toast.danger(str));
217 this.materialsGrid.reload();