ea5dc2fc3feb090b63c58839a5380aaec1f1a5c3
[evergreen-equinox.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('courseMaterialDialog', {static: true})
33         private courseMaterialDialog: CourseAssociateMaterialComponent;
34     @Input() sort_field: string;
35     @Input() idl_class = "acmc";
36     @Input() dialog_size: 'sm' | 'lg' = 'lg';
37     @Input() table_name = "Course";
38     grid_source: GridDataSource = new GridDataSource();
39     currentMaterials: any[] = [];
40     search_value = '';
41
42     constructor(
43         private auth: AuthService,
44         private courseSvc: CourseService,
45         private net: NetService,
46         private org: OrgService,
47         private pcrud: PcrudService,
48         private toast: ToastService,
49     ){}
50
51     ngOnInit() {
52         this.getSource();
53     }
54
55     /**
56      * Gets the data, specified by the class, that is available.
57      */
58     getSource() {
59         this.grid_source.getRows = (pager: Pager, sort: any[]) => {
60             const orderBy: any = {};
61             if (sort.length) {
62                 // Sort specified from grid
63                 orderBy[this.idl_class] = sort[0].name + ' ' + sort[0].dir;
64             } else if (this.sort_field) {
65                 // Default sort field
66                 orderBy[this.idl_class] = this.sort_field;
67             }
68             const searchOps = {
69                 offset: pager.offset,
70                 limit: pager.limit,
71                 order_by: orderBy
72             };
73             return this.pcrud.retrieveAll(this.idl_class, searchOps, {fleshSelectors: true});
74         };
75     }
76
77     showEditDialog(standingPenalty: IdlObject): Promise<any> {
78         this.editDialog.mode = 'update';
79         this.editDialog.recordId = standingPenalty['id']();
80         return new Promise((resolve, reject) => {
81             this.editDialog.open({size: this.dialog_size}).subscribe(
82                 result => {
83                     this.successString.current()
84                         .then(str => this.toast.success(str));
85                     this.grid.reload();
86                     resolve(result);
87                 },
88                 error => {
89                     this.updateFailedString.current()
90                         .then(str => this.toast.danger(str));
91                     reject(error);
92                 }
93             );
94         });
95     }
96
97     createNew() {
98         this.editDialog.mode = 'create';
99         this.editDialog.recordId = null;
100         this.editDialog.record = null;
101         this.editDialog.open({size: this.dialog_size}).subscribe(
102             ok => {
103                 this.createString.current()
104                     .then(str => this.toast.success(str));
105                 this.grid.reload();
106             },
107             rejection => {
108                 if (!rejection.dismissed) {
109                     this.createErrString.current()
110                         .then(str => this.toast.danger(str));
111                 }
112             }
113         );
114     }
115
116     editSelected(fields: IdlObject[]) {
117         // Edit each IDL thing one at a time
118         const editOneThing = (field_object: IdlObject) => {
119             if (!field_object) { return; }
120             this.showEditDialog(field_object).then(
121                 () => editOneThing(fields.shift()));
122         };
123         editOneThing(fields.shift());
124     }
125
126     deleteSelected(idl_object: IdlObject[]) {
127         this.courseSvc.disassociateMaterials(idl_object).then(res => {
128             console.log(res);
129             idl_object.forEach(idl_object => {
130                 idl_object.isdeleted(true)
131             });
132             this.pcrud.autoApply(idl_object).subscribe(
133                 val => {
134                     console.debug('deleted: ' + val);
135                     this.deleteSuccessString.current()
136                         .then(str => this.toast.success(str));
137                 },
138                 err => {
139                     this.deleteFailedString.current()
140                         .then(str => this.toast.danger(str));
141                 },
142                 () => this.grid.reload()
143             );
144         });
145     };
146
147     fetchCourseMaterials(course, currentMaterials): Promise<any> {
148         return new Promise((resolve, reject) => {
149             this.pcrud.search('acmcm', {course: course}).subscribe(res => {
150                 if (res) this.fleshItemDetails(res.item(), res.relationship());
151             }, err => {
152                 reject(err);
153             }, () => resolve(this.courseMaterialDialog.gridDataSource.data));
154         });
155     }
156
157     fleshItemDetails(itemId, relationship): Promise<any> {
158         return new Promise((resolve, reject) => {
159             this.net.request(
160                 'open-ils.circ',
161                 'open-ils.circ.copy_details.retrieve',
162                 this.auth.token(), itemId
163             ).subscribe(res => {
164                 if (res) {
165                     let item = res.copy;
166                     item.call_number(res.volume);
167                     item._title = res.mvr.title();
168                     item.circ_lib(this.org.get(item.circ_lib()));
169                     item._relationship = relationship;
170                     this.courseMaterialDialog.gridDataSource.data.push(item);
171                 }
172             }, err => {
173                 reject(err);
174             }, () => resolve(this.courseMaterialDialog.gridDataSource.data));
175         });
176     }
177
178     openMaterialsDialog(course) {
179         let currentMaterials = []
180         this.courseMaterialDialog.gridDataSource.data = [];
181         this.fetchCourseMaterials(course[0].id(), currentMaterials).then(res => {
182             this.courseMaterialDialog.currentCourse = course[0];
183             this.courseMaterialDialog.materials = currentMaterials;
184             this.courseMaterialDialog.open({size: 'lg'}).subscribe(res => {
185                 console.log(res);
186             });
187         });
188     }
189 }
190