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';
18 selector: 'eg-course-associate-users-dialog',
19 templateUrl: './course-associate-users.component.html'
22 export class CourseAssociateUsersComponent extends DialogComponent implements OnInit {
23 @Input() currentCourse: IdlObject;
24 @Input() courseId: number;
25 @Input() displayMode: String;
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;
43 userRoleInput: ComboboxEntry;
46 private auth: AuthService,
47 private course: CourseService,
48 private net: NetService,
49 private pcrud: PcrudService,
50 private toast: ToastService,
51 private modal: NgbModal
54 this.usersDataSource = new GridDataSource();
58 this.usersDataSource.getRows = (pager: Pager, sort: any[]) => {
59 return this.course.getUsers([this.courseId]);
64 return this.displayMode === 'dialog';
67 associateUser(barcode) {
70 currentCourse: this.currentCourse,
71 barcode: barcode.trim(),
74 if (this.userRoleInput) {
75 args['role'] = this.userRoleInput.id;
78 this.userBarcode = null;
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());
88 this.userAddFailedString.current().then(str => this.toast.danger(str));
94 editSelectedUsers(userFields: IdlObject[]) {
95 // Edit each IDL thing one at a time
96 const editOneThing = (user: IdlObject) => {
97 if (!user) { return; }
99 this.showEditDialog(user).then(
100 () => editOneThing(userFields.shift()));
103 editOneThing(userFields.shift());
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(
112 this.userEditSuccessString.current()
113 .then(str => this.toast.success(str));
114 this.usersGrid.reload();
118 this.userEditFailedString.current()
119 .then(str => this.toast.danger(str));
126 deleteSelectedUsers(users) {
128 this.pcrud.search('acmcu', {course: this.courseId, usr: user_ids}).subscribe(user => {
129 user.isdeleted(true);
130 this.pcrud.autoApply(user).subscribe(
132 console.debug('deleted: ' + val);
133 this.userDeleteSuccessString.current().then(str => this.toast.success(str));
134 this.usersGrid.reload();
137 this.userDeleteFailedString.current()
138 .then(str => this.toast.danger(str));