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 {PatronSearchDialogComponent} from '@eg/staff/share/patron/search-dialog.component';
14 import {ToastService} from '@eg/share/toast/toast.service';
15 import {CourseService} from '@eg/staff/share/course.service';
16 import {ComboboxEntry} from '@eg/share/combobox/combobox.component';
19 selector: 'eg-course-associate-users-dialog',
20 templateUrl: './course-associate-users.component.html'
23 export class CourseAssociateUsersComponent extends DialogComponent implements OnInit {
24 @Input() currentCourse: IdlObject;
25 @Input() courseId: number;
26 @Input() courseIsArchived: String;
27 @Input() displayMode: String;
29 @ViewChild('editDialog', { static: true }) editDialog: FmRecordEditorComponent;
30 @ViewChild('patronSearch') patronSearch: PatronSearchDialogComponent;
31 @ViewChild('usersGrid') usersGrid: GridComponent;
32 @ViewChild('userDeleteFailedString', { static: true })
33 userDeleteFailedString: StringComponent;
34 @ViewChild('userDeleteSuccessString', { static: true })
35 userDeleteSuccessString: StringComponent;
36 @ViewChild('userAddSuccessString', { static: true })
37 userAddSuccessString: StringComponent;
38 @ViewChild('userAddFailedString', { static: true })
39 userAddFailedString: StringComponent;
40 @ViewChild('userEditSuccessString', { static: true })
41 userEditSuccessString: StringComponent;
42 @ViewChild('userEditFailedString', { static: true })
43 userEditFailedString: StringComponent;
44 usersDataSource: GridDataSource;
46 userRoleInput: ComboboxEntry;
49 private auth: AuthService,
50 private course: CourseService,
51 private net: NetService,
52 private pcrud: PcrudService,
53 private toast: ToastService,
54 private modal: NgbModal
57 this.usersDataSource = new GridDataSource();
61 this.usersDataSource.getRows = (pager: Pager, sort: any[]) => {
62 return this.course.getUsers([this.courseId]);
67 return this.displayMode === 'dialog';
70 associateUser(barcode) {
73 currentCourse: this.currentCourse,
74 barcode: barcode.trim(),
77 if (this.userRoleInput) {
78 args['role'] = this.userRoleInput.id;
81 this.userBarcode = null;
85 'open-ils.actor.user.retrieve_id_by_barcode_or_username',
86 this.auth.token(), barcode.trim()
87 ).subscribe(patron => {
88 this.course.associateUsers(patron, args)
89 .then(() => this.usersGrid.reload());
90 }, (err: unknown) => {
91 this.userAddFailedString.current().then(str => this.toast.danger(str));
97 editSelectedUsers(userFields: IdlObject[]) {
98 // Edit each IDL thing one at a time
99 const editOneThing = (user: IdlObject) => {
100 if (!user) { return; }
102 this.showEditDialog(user).then(
103 () => editOneThing(userFields.shift()));
106 editOneThing(userFields.shift());
110 this.patronSearch.open({size: 'xl'}).toPromise().then(
112 if (!patrons || patrons.length === 0) { return; }
113 const user = patrons[0];
114 this.userBarcode = user.card().barcode();
119 showEditDialog(user: IdlObject): Promise<any> {
120 this.editDialog.mode = 'update';
121 this.editDialog.recordId = user.id();
122 return new Promise((resolve, reject) => {
123 this.editDialog.open({size: 'lg'}).subscribe(
125 this.userEditSuccessString.current()
126 .then(str => this.toast.success(str));
127 this.usersGrid.reload();
130 (error: unknown) => {
131 this.userEditFailedString.current()
132 .then(str => this.toast.danger(str));
139 deleteSelectedUsers(users) {
140 const acmcu_ids = users.map(u => u.id());
141 this.pcrud.search('acmcu', {course: this.courseId, id: acmcu_ids}).subscribe(user => {
142 user.isdeleted(true);
143 // eslint-disable-next-line rxjs/no-nested-subscribe
144 this.pcrud.autoApply(user).subscribe(
146 console.debug('deleted: ' + val);
147 this.userDeleteSuccessString.current().then(str => this.toast.success(str));
148 this.usersGrid.reload();
151 this.userDeleteFailedString.current()
152 .then(str => this.toast.danger(str));
155 }).add(() => this.usersGrid.reload());