]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/admin/local/course-reserves/course-term-grid.component.ts
LP 2061136 follow-up: ng lint --fix
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / admin / local / course-reserves / course-term-grid.component.ts
1 import {Component, Input, ViewChild, OnInit, AfterViewInit} from '@angular/core';
2 import {Router} from '@angular/router';
3 import {IdlObject, IdlService} from '@eg/core/idl.service';
4 import {PcrudService} from '@eg/core/pcrud.service';
5 import {CourseService} from '@eg/staff/share/course.service';
6 import {GridComponent} from '@eg/share/grid/grid.component';
7 import {Pager} from '@eg/share/util/pager';
8 import {GridDataSource, GridColumn} from '@eg/share/grid/grid';
9 import {FmRecordEditorComponent} from '@eg/share/fm-editor/fm-editor.component';
10 import {ConfirmDialogComponent} from '@eg/share/dialog/confirm.component';
11 import {StringComponent} from '@eg/share/string/string.component';
12 import {ToastService} from '@eg/share/toast/toast.service';
13 import {LocaleService} from '@eg/core/locale.service';
14 import {AuthService} from '@eg/core/auth.service';
15 import {OrgService} from '@eg/core/org.service';
16 import {OrgFamily} from '@eg/share/org-family-select/org-family-select.component';
17
18 @Component({
19     selector: 'eg-course-term-grid',
20     templateUrl: './course-term-grid.component.html'
21 })
22
23 export class TermListComponent implements OnInit, AfterViewInit {
24
25     @ViewChild('editDialog', { static: true }) editDialog: FmRecordEditorComponent;
26     @ViewChild('deleteLinkedTermWarning', { static: true }) deleteLinkedTermWarning: ConfirmDialogComponent;
27     @ViewChild('grid') grid: GridComponent;
28     @ViewChild('successString', { static: true }) successString: StringComponent;
29     @ViewChild('createString') createString: StringComponent;
30     @ViewChild('createErrString') createErrString: StringComponent;
31     @ViewChild('updateFailedString') updateFailedString: StringComponent;
32     @ViewChild('deleteFailedString', { static: true }) deleteFailedString: StringComponent;
33     @ViewChild('deleteSuccessString', { static: true }) deleteSuccessString: StringComponent;
34     @ViewChild('deleteLinkedTermWarningString', { static: true }) deleteLinkedTermWarningString: StringComponent;
35
36     @Input() sortField: string;
37     @Input() idlClass = 'acmt';
38     @Input() dialog_size: 'sm' | 'lg' = 'lg';
39     @Input() tableName = 'Term';
40     grid_source: GridDataSource = new GridDataSource();
41     currentMaterials: any[] = [];
42     search_value = '';
43     defaultOuId: number;
44     searchOrgs: OrgFamily;
45     defaultTerm: IdlObject;
46     termToDelete: String;
47
48
49     constructor(
50         private courseSvc: CourseService,
51         private locale: LocaleService,
52         private auth: AuthService,
53         private idl: IdlService,
54         private org: OrgService,
55         private pcrud: PcrudService,
56         private router: Router,
57         private toast: ToastService
58     ) {}
59
60     ngOnInit() {
61         this.getSource();
62         this.defaultTerm = this.idl.create('acmt');
63         this.defaultOuId = this.auth.user().ws_ou() || this.org.root().id();
64         this.defaultTerm.owning_lib(this.defaultOuId);
65         this.searchOrgs = {primaryOrgId: this.defaultOuId};
66     }
67
68     ngAfterViewInit() {
69         this.grid.onRowActivate.subscribe((term: IdlObject) => {
70             const idToEdit = term.id();
71             this.editSelected([term]);
72         });
73
74     }
75
76     /**
77      * Gets the data, specified by the class, that is available.
78      */
79     getSource() {
80         this.grid_source.getRows = (pager: Pager, sort: any[]) => {
81             const orderBy: any = {};
82             if (sort.length) {
83                 // Sort specified from grid
84                 orderBy[this.idlClass] = sort[0].name + ' ' + sort[0].dir;
85             } else if (this.sortField) {
86                 // Default sort field
87                 orderBy[this.idlClass] = this.sortField;
88             }
89             const search: any = new Array();
90             const orgFilter: any = {};
91             orgFilter['owning_lib'] =
92                 this.searchOrgs.orgIds || [this.defaultOuId];
93             search.push(orgFilter);
94             const searchOps = {
95                 offset: pager.offset,
96                 limit: pager.limit,
97                 order_by: orderBy
98             };
99             return this.pcrud.search(this.idlClass, search, searchOps, {fleshSelectors: true});
100         };
101     }
102
103     createNew() {
104         this.editDialog.mode = 'create';
105         const course_module_term = this.idl.create('acmt');
106         course_module_term.owning_lib(this.auth.user().ws_ou());
107         this.editDialog.recordId = null;
108         this.editDialog.record = course_module_term;
109         this.editDialog.open({size: this.dialog_size}).subscribe(
110             ok => {
111                 this.createString.current()
112                     .then(str => this.toast.success(str));
113                 this.grid.reload();
114             },
115             // eslint-disable-next-line rxjs/no-implicit-any-catch
116             (rejection: any) => {
117                 if (!rejection.dismissed) {
118                     this.createErrString.current()
119                         .then(str => this.toast.danger(str));
120                 }
121             }
122         );
123     }
124
125     editSelected(fields: IdlObject[]) {
126         this.editDialog.mode = 'update';
127         // Edit each IDL thing one at a time
128         const course_ids = [];
129         fields.forEach(field => {
130             this.editDialog.record = field;
131             this.editDialog.open({size: this.dialog_size}).subscribe(
132                 ok => {
133                     this.createString.current()
134                         .then(str => this.toast.success(str));
135                     this.grid.reload();
136                 },
137                 // eslint-disable-next-line rxjs/no-implicit-any-catch
138                 (rejection: any) => {
139                     if (!rejection.dismissed) {
140                         this.createErrString.current()
141                             .then(str => this.toast.danger(str));
142                     }
143                 }
144             );
145         });
146     }
147
148     deleteSelected(fields: IdlObject[]) {
149         console.log(this.deleteLinkedTermWarningString);
150         fields.forEach(field => {
151             let termHasLinkedCourses = false;
152             this.courseSvc.getTermMaps(field.id()).subscribe(map => {
153                 if (map) {
154                     termHasLinkedCourses = true;
155                 }
156             }, (err: unknown) => {
157                 console.error(err);
158             }, () => {
159                 if (termHasLinkedCourses) {
160                     this.termToDelete = field.name();
161                     this.deleteLinkedTermWarning.open().toPromise().then(yes => {
162                         if (!yes) { return; }
163                         this.doDelete(field);
164                     });
165                 } else {
166                     this.doDelete(field);
167                 }
168             });
169         });
170
171     }
172
173     doDelete(idlThing: IdlObject) {
174         idlThing.isdeleted(true);
175         this.pcrud.autoApply(idlThing).subscribe(
176             val => {
177                 console.debug('deleted: ' + val);
178                 this.deleteSuccessString.current()
179                     .then(str => this.toast.success(str));
180             },
181             (err: unknown) => {
182                 this.deleteFailedString.current()
183                     .then(str => this.toast.danger(str));
184             },
185             ()  => this.grid.reload()
186         );
187     }
188 }