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';
17 selector: 'eg-course-associate-users-dialog',
18 templateUrl: './course-associate-users.component.html'
21 export class CourseAssociateUsersComponent extends DialogComponent implements OnInit {
22 @Input() currentCourse: IdlObject;
23 @Input() courseId: number;
24 @Input() displayMode: String;
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;
42 userRoleInput: String;
43 isPublicRole: Boolean;
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(),
72 role: this.userRoleInput,
73 is_public: this.isPublicRole
76 this.userBarcode = null;
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());
86 this.userAddFailedString.current().then(str => this.toast.danger(str));
92 editSelectedUsers(userFields: IdlObject[]) {
93 // Edit each IDL thing one at a time
94 const editOneThing = (user: IdlObject) => {
95 if (!user) { return; }
97 this.showEditDialog(user).then(
98 () => editOneThing(userFields.shift()));
101 editOneThing(userFields.shift());
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(
110 this.userEditSuccessString.current()
111 .then(str => this.toast.success(str));
112 this.usersGrid.reload();
116 this.userEditFailedString.current()
117 .then(str => this.toast.danger(str));
124 deleteSelectedUsers(users) {
126 this.pcrud.search('acmcu', {course: this.courseId, usr: user_ids}).subscribe(user => {
127 user.isdeleted(true);
128 this.pcrud.autoApply(user).subscribe(
130 console.debug('deleted: ' + val);
131 this.userDeleteSuccessString.current().then(str => this.toast.success(str));
132 this.usersGrid.reload();
135 this.userDeleteFailedString.current()
136 .then(str => this.toast.danger(str));