]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/admin/server/course-reserves/course-list.component.ts
LP1849212: Fix installation issue with circ mod foreign key
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / admin / server / course-reserves / course-list.component.ts
1 import {Component, Input, ViewChild, OnInit} from '@angular/core';
2 import {IdlObject} from '@eg/core/idl.service';
3 import {PcrudService} from '@eg/core/pcrud.service';
4 import {AuthService} from '@eg/core/auth.service';
5 import {CourseService} from './course.service';
6 import {NetService} from '@eg/core/net.service';
7 import {OrgService} from '@eg/core/org.service';
8 import {GridComponent} from '@eg/share/grid/grid.component';
9 import {Pager} from '@eg/share/util/pager';
10 import {GridDataSource, GridColumn} from '@eg/share/grid/grid';
11 import {FmRecordEditorComponent} from '@eg/share/fm-editor/fm-editor.component';
12 import {StringComponent} from '@eg/share/string/string.component';
13 import {ToastService} from '@eg/share/toast/toast.service';
14
15 import {CourseAssociateMaterialComponent
16     } from './course-associate-material.component';
17
18 @Component({
19     templateUrl: './course-list.component.html'
20 })
21
22 export class CourseListComponent implements OnInit { 
23  
24     @ViewChild('editDialog', { static: true }) editDialog: FmRecordEditorComponent;
25     @ViewChild('grid', { static: true }) grid: GridComponent;
26     @ViewChild('successString', { static: true }) successString: StringComponent;
27     @ViewChild('createString', { static: false }) createString: StringComponent;
28     @ViewChild('createErrString', { static: false }) createErrString: StringComponent;
29     @ViewChild('updateFailedString', { static: false }) updateFailedString: StringComponent;
30     @ViewChild('deleteFailedString', { static: true }) deleteFailedString: StringComponent;
31     @ViewChild('deleteSuccessString', { static: true }) deleteSuccessString: StringComponent;
32     @ViewChild('archiveFailedString', { static: true }) archiveFailedString: StringComponent;
33     @ViewChild('archiveSuccessString', { static: true }) archiveSuccessString: StringComponent;
34     @ViewChild('courseMaterialDialog', {static: true})
35         private courseMaterialDialog: CourseAssociateMaterialComponent;
36     @Input() sort_field: string;
37     @Input() idl_class = "acmc";
38     @Input() dialog_size: 'sm' | 'lg' = 'lg';
39     @Input() table_name = "Course";
40     grid_source: GridDataSource = new GridDataSource();
41     currentMaterials: any[] = [];
42     search_value = '';
43
44     constructor(
45         private auth: AuthService,
46         private courseSvc: CourseService,
47         private net: NetService,
48         private org: OrgService,
49         private pcrud: PcrudService,
50         private toast: ToastService,
51     ){}
52
53     ngOnInit() {
54         this.getSource();
55     }
56
57     /**
58      * Gets the data, specified by the class, that is available.
59      */
60     getSource() {
61         this.grid_source.getRows = (pager: Pager, sort: any[]) => {
62             const orderBy: any = {};
63             if (sort.length) {
64                 // Sort specified from grid
65                 orderBy[this.idl_class] = sort[0].name + ' ' + sort[0].dir;
66             } else if (this.sort_field) {
67                 // Default sort field
68                 orderBy[this.idl_class] = this.sort_field;
69             }
70             const searchOps = {
71                 offset: pager.offset,
72                 limit: pager.limit,
73                 order_by: orderBy
74             };
75             return this.pcrud.retrieveAll(this.idl_class, searchOps, {fleshSelectors: true});
76         };
77     }
78
79     showEditDialog(standingPenalty: IdlObject): Promise<any> {
80         this.editDialog.mode = 'update';
81         this.editDialog.recordId = standingPenalty['id']();
82         return new Promise((resolve, reject) => {
83             this.editDialog.open({size: this.dialog_size}).subscribe(
84                 result => {
85                     this.successString.current()
86                         .then(str => this.toast.success(str));
87                     this.grid.reload();
88                     resolve(result);
89                 },
90                 error => {
91                     this.updateFailedString.current()
92                         .then(str => this.toast.danger(str));
93                     reject(error);
94                 }
95             );
96         });
97     }
98
99     createNew() {
100         this.editDialog.mode = 'create';
101         this.editDialog.recordId = null;
102         this.editDialog.record = null;
103         this.editDialog.open({size: this.dialog_size}).subscribe(
104             ok => {
105                 this.createString.current()
106                     .then(str => this.toast.success(str));
107                 this.grid.reload();
108             },
109             rejection => {
110                 if (!rejection.dismissed) {
111                     this.createErrString.current()
112                         .then(str => this.toast.danger(str));
113                 }
114             }
115         );
116     }
117
118     editSelected(fields: IdlObject[]) {
119         // Edit each IDL thing one at a time
120         const editOneThing = (field_object: IdlObject) => {
121             if (!field_object) { return; }
122             this.showEditDialog(field_object).then(
123                 () => editOneThing(fields.shift()));
124         };
125         editOneThing(fields.shift());
126     }
127
128     archiveSelected(course: IdlObject[]) {
129         this.courseSvc.disassociateMaterials(course).then(res => {
130             course.forEach(course => {
131                 console.log(course);
132                 course.is_archived(true);
133             });
134             this.pcrud.update(course).subscribe(
135                 val => {
136                     console.debug('archived: ' + val);
137                     this.archiveSuccessString.current()
138                         .then(str => this.toast.success(str));
139                 }, err => {
140                     this.archiveFailedString.current()
141                         .then(str => this.toast.danger(str));
142                 }, () => {
143                     this.grid.reload();
144                 }
145             );
146         });
147     }
148
149     deleteSelected(idl_object: IdlObject[]) {
150         this.courseSvc.disassociateMaterials(idl_object).then(res => {
151             idl_object.forEach(idl_object => {
152                 idl_object.isdeleted(true)
153             });
154             this.pcrud.autoApply(idl_object).subscribe(
155                 val => {
156                     console.debug('deleted: ' + val);
157                     this.deleteSuccessString.current()
158                         .then(str => this.toast.success(str));
159                 },
160                 err => {
161                     this.deleteFailedString.current()
162                         .then(str => this.toast.danger(str));
163                 },
164                 () => this.grid.reload()
165             );
166         });
167     };
168
169     fetchCourseMaterials(course, currentMaterials): Promise<any> {
170         return new Promise((resolve, reject) => {
171             this.pcrud.search('acmcm', {course: course}).subscribe(res => {
172                 if (res) this.fleshItemDetails(res.item(), res.relationship());
173             }, err => {
174                 reject(err);
175             }, () => resolve(this.courseMaterialDialog.gridDataSource.data));
176         });
177     }
178
179     fleshItemDetails(itemId, relationship): Promise<any> {
180         return new Promise((resolve, reject) => {
181             this.net.request(
182                 'open-ils.circ',
183                 'open-ils.circ.copy_details.retrieve',
184                 this.auth.token(), itemId
185             ).subscribe(res => {
186                 if (res) {
187                     let item = res.copy;
188                     item.call_number(res.volume);
189                     item._title = res.mvr.title();
190                     item.circ_lib(this.org.get(item.circ_lib()));
191                     item._relationship = relationship;
192                     this.courseMaterialDialog.gridDataSource.data.push(item);
193                 }
194             }, err => {
195                 reject(err);
196             }, () => resolve(this.courseMaterialDialog.gridDataSource.data));
197         });
198     }
199
200     openMaterialsDialog(course) {
201         let currentMaterials = []
202         this.courseMaterialDialog.gridDataSource.data = [];
203         this.fetchCourseMaterials(course[0].id(), currentMaterials).then(res => {
204             this.courseMaterialDialog.currentCourse = course[0];
205             this.courseMaterialDialog.materials = currentMaterials;
206             this.courseMaterialDialog.open({size: 'lg'}).subscribe(res => {
207                 console.log(res);
208             });
209         });
210     }
211 }
212