d9360ccb00a576c1dd2598e98405ad416cafb073
[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, IdlService} 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
16 @Component({
17     selector: 'eg-course-associate-users-dialog',
18     templateUrl: './course-associate-users.component.html'
19 })
20
21 export class CourseAssociateUsersComponent extends DialogComponent implements OnInit {
22     @Input() currentCourse: IdlObject;
23     @Input() courseId: number;
24     @Input() displayMode: String;
25     users: any[] = [];
26     @ViewChild('editDialog', { static: true }) editDialog: FmRecordEditorComponent;
27     @ViewChild('usersGrid') usersGrid: GridComponent;
28     @ViewChild('userDeleteFailedString', { static: true })
29         userDeleteFailedString: StringComponent;
30     @ViewChild('userDeleteSuccessString', { static: true })
31         userDeleteSuccessString: StringComponent;
32     @ViewChild('userAddSuccessString', { static: true })
33         userAddSuccessString: StringComponent;
34     @ViewChild('userAddFailedString', { static: true })
35         userAddFailedString: StringComponent;
36     @ViewChild('userEditSuccessString', { static: true })
37         userEditSuccessString: StringComponent;
38     @ViewChild('userEditFailedString', { static: true })
39         userEditFailedString: StringComponent;
40     usersDataSource: GridDataSource;
41     userBarcode: String;
42     userRoleInput: String;
43     isPublicRole: Boolean;
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                 role: this.userRoleInput,
73                 is_public: this.isPublicRole
74             };
75
76             this.userBarcode = null;
77
78             this.net.request(
79                 'open-ils.actor',
80                 'open-ils.actor.user.retrieve_id_by_barcode_or_username',
81                 this.auth.token(), barcode.trim()
82             ).subscribe(patron => {
83                     this.course.associateUsers(patron, args)
84                     .then(() => this.usersGrid.reload());
85                 }, err => {
86                     this.userAddFailedString.current().then(str => this.toast.danger(str));
87                 }
88             );
89         }
90     }
91
92     editSelectedUsers(userFields: IdlObject[]) {
93         // Edit each IDL thing one at a time
94         const editOneThing = (user: IdlObject) => {
95             if (!user) { return; }
96
97             this.showEditDialog(user).then(
98                 () => editOneThing(userFields.shift()));
99         };
100
101         editOneThing(userFields.shift());
102     }
103
104     showEditDialog(user: IdlObject): Promise<any> {
105         this.editDialog.mode = 'update';
106         this.editDialog.recordId = user._id;
107         return new Promise((resolve, reject) => {
108             this.editDialog.open({size: 'lg'}).subscribe(
109                 result => {
110                     this.userEditSuccessString.current()
111                         .then(str => this.toast.success(str));
112                     this.usersGrid.reload();
113                     resolve(result);
114                 },
115                 error => {
116                     this.userEditFailedString.current()
117                         .then(str => this.toast.danger(str));
118                     reject(error);
119                 }
120             );
121         });
122     }
123
124     deleteSelectedUsers(users) {
125         const user_ids = [];
126         this.pcrud.search('acmcu', {course: this.courseId, usr: user_ids}).subscribe(user => {
127             user.isdeleted(true);
128             this.pcrud.autoApply(user).subscribe(
129                 val => {
130                     console.debug('deleted: ' + val);
131                     this.userDeleteSuccessString.current().then(str => this.toast.success(str));
132                     this.usersGrid.reload();
133                 },
134                 err => {
135                     this.userDeleteFailedString.current()
136                         .then(str => this.toast.danger(str));
137                 }
138             );
139         });
140     }
141
142 }