]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/admin/local/course-reserves/course-associate-users.component.ts
LP#1849212: (follow-up) fix edit course user dialog
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / admin / local / course-reserves / course-associate-users.component.ts
1 import {Component, Input, ViewChild, OnInit} from '@angular/core';
2 import {DialogComponent} from '@eg/share/dialog/dialog.component';
3 import {AuthService} from '@eg/core/auth.service';
4 import {NetService} from '@eg/core/net.service';
5 import {PcrudService} from '@eg/core/pcrud.service';
6 import {Pager} from '@eg/share/util/pager';
7 import {NgbModal} from '@ng-bootstrap/ng-bootstrap';
8 import {GridDataSource} from '@eg/share/grid/grid';
9 import {GridComponent} from '@eg/share/grid/grid.component';
10 import {IdlObject} from '@eg/core/idl.service';
11 import {StringComponent} from '@eg/share/string/string.component';
12 import {FmRecordEditorComponent} from '@eg/share/fm-editor/fm-editor.component';
13 import {ToastService} from '@eg/share/toast/toast.service';
14 import {CourseService} from '@eg/staff/share/course.service';
15 import {ComboboxEntry} from '@eg/share/combobox/combobox.component';
16
17 @Component({
18     selector: 'eg-course-associate-users-dialog',
19     templateUrl: './course-associate-users.component.html'
20 })
21
22 export class CourseAssociateUsersComponent extends DialogComponent implements OnInit {
23     @Input() currentCourse: IdlObject;
24     @Input() courseId: number;
25     @Input() displayMode: String;
26     users: any[] = [];
27     @ViewChild('editDialog', { static: true }) editDialog: FmRecordEditorComponent;
28     @ViewChild('usersGrid') usersGrid: GridComponent;
29     @ViewChild('userDeleteFailedString', { static: true })
30         userDeleteFailedString: StringComponent;
31     @ViewChild('userDeleteSuccessString', { static: true })
32         userDeleteSuccessString: StringComponent;
33     @ViewChild('userAddSuccessString', { static: true })
34         userAddSuccessString: StringComponent;
35     @ViewChild('userAddFailedString', { static: true })
36         userAddFailedString: StringComponent;
37     @ViewChild('userEditSuccessString', { static: true })
38         userEditSuccessString: StringComponent;
39     @ViewChild('userEditFailedString', { static: true })
40         userEditFailedString: StringComponent;
41     usersDataSource: GridDataSource;
42     userBarcode: String;
43     userRoleInput: ComboboxEntry;
44
45     constructor(
46         private auth: AuthService,
47         private course: CourseService,
48         private net: NetService,
49         private pcrud: PcrudService,
50         private toast: ToastService,
51         private modal: NgbModal
52     ) {
53         super(modal);
54         this.usersDataSource = new GridDataSource();
55     }
56
57     ngOnInit() {
58         this.usersDataSource.getRows = (pager: Pager, sort: any[]) => {
59             return this.course.getUsers([this.courseId]);
60         };
61     }
62
63     isDialog(): boolean {
64         return this.displayMode === 'dialog';
65     }
66
67     associateUser(barcode) {
68         if (barcode) {
69             const args = {
70                 currentCourse: this.currentCourse,
71                 barcode: barcode.trim(),
72             };
73
74             if (this.userRoleInput) {
75                 args['role'] = this.userRoleInput.id;
76             }
77
78             this.userBarcode = null;
79
80             this.net.request(
81                 'open-ils.actor',
82                 'open-ils.actor.user.retrieve_id_by_barcode_or_username',
83                 this.auth.token(), barcode.trim()
84             ).subscribe(patron => {
85                     this.course.associateUsers(patron, args)
86                     .then(() => this.usersGrid.reload());
87                 }, err => {
88                     this.userAddFailedString.current().then(str => this.toast.danger(str));
89                 }
90             );
91         }
92     }
93
94     editSelectedUsers(userFields: IdlObject[]) {
95         // Edit each IDL thing one at a time
96         const editOneThing = (user: IdlObject) => {
97             if (!user) { return; }
98
99             this.showEditDialog(user).then(
100                 () => editOneThing(userFields.shift()));
101         };
102
103         editOneThing(userFields.shift());
104     }
105
106     showEditDialog(user: IdlObject): Promise<any> {
107         this.editDialog.mode = 'update';
108         this.editDialog.recordId = user.id();
109         return new Promise((resolve, reject) => {
110             this.editDialog.open({size: 'lg'}).subscribe(
111                 result => {
112                     this.userEditSuccessString.current()
113                         .then(str => this.toast.success(str));
114                     this.usersGrid.reload();
115                     resolve(result);
116                 },
117                 error => {
118                     this.userEditFailedString.current()
119                         .then(str => this.toast.danger(str));
120                     reject(error);
121                 }
122             );
123         });
124     }
125
126     deleteSelectedUsers(users) {
127         const user_ids = [];
128         this.pcrud.search('acmcu', {course: this.courseId, usr: user_ids}).subscribe(user => {
129             user.isdeleted(true);
130             this.pcrud.autoApply(user).subscribe(
131                 val => {
132                     console.debug('deleted: ' + val);
133                     this.userDeleteSuccessString.current().then(str => this.toast.success(str));
134                     this.usersGrid.reload();
135                 },
136                 err => {
137                     this.userDeleteFailedString.current()
138                         .then(str => this.toast.danger(str));
139                 }
140             );
141         });
142     }
143
144 }