]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/admin/local/staff_portal_page/staff-portal-page.component.ts
LP1615805 No inputs after submit in patron search (AngularJS)
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / admin / local / staff_portal_page / staff-portal-page.component.ts
1 import {Component, Input, ViewChild, OnInit} from '@angular/core';
2 import {Location} from '@angular/common';
3 import {FormatService} from '@eg/core/format.service';
4 import {AdminPageComponent} from '@eg/staff/share/admin-page/admin-page.component';
5 import {ActivatedRoute} from '@angular/router';
6 import {IdlService, IdlObject} from '@eg/core/idl.service';
7 import {ToastService} from '@eg/share/toast/toast.service';
8 import {PcrudService} from '@eg/core/pcrud.service';
9 import {OrgService} from '@eg/core/org.service';
10 import {PermService} from '@eg/core/perm.service';
11 import {AuthService} from '@eg/core/auth.service';
12 import {NetService} from '@eg/core/net.service';
13 import {GridCellTextGenerator} from '@eg/share/grid/grid';
14 import {StringComponent} from '@eg/share/string/string.component';
15 import {FmRecordEditorComponent} from '@eg/share/fm-editor/fm-editor.component';
16 import {ClonePortalEntriesDialogComponent} from './clone-portal-entries-dialog.component';
17 import {ConfirmDialogComponent} from '@eg/share/dialog/confirm.component';
18 import {merge, Observable, empty} from 'rxjs';
19
20 @Component({
21     templateUrl: './staff-portal-page.component.html'
22 })
23
24 export class AdminStaffPortalPageComponent extends AdminPageComponent implements OnInit {
25
26     idlClass = 'cusppe';
27     fieldOrder = 'label,entry_type,target_url,entry_text,image_url,page_col,col_pos,owner,id';
28     classLabel: string;
29
30     refreshSelected: (idlThings: IdlObject[]) => void;
31     createNew: () => void;
32     cellTextGenerator: GridCellTextGenerator;
33
34     @ViewChild('refreshString', { static: true }) refreshString: StringComponent;
35     @ViewChild('refreshErrString', { static: true }) refreshErrString: StringComponent;
36     @ViewChild('cloneSuccessString', { static: true }) cloneSuccessString: StringComponent;
37     @ViewChild('cloneFailedString', { static: true }) cloneFailedString: StringComponent;
38     @ViewChild('cloneDialog', { static: true}) cloneDialog: ClonePortalEntriesDialogComponent;
39     @ViewChild('delConfirm', { static: true }) delConfirm: ConfirmDialogComponent;
40
41     constructor(
42         route: ActivatedRoute,
43         ngLocation: Location,
44         format: FormatService,
45         idl: IdlService,
46         org: OrgService,
47         auth: AuthService,
48         pcrud: PcrudService,
49         perm: PermService,
50         toast: ToastService,
51         private net: NetService
52     ) {
53         super(route, ngLocation, format, idl, org, auth, pcrud, perm, toast);
54     }
55
56     ngOnInit() {
57         super.ngOnInit();
58
59         this.defaultNewRecord = this.idl.create(this.idlClass);
60         this.defaultNewRecord.owner(this.auth.user().ws_ou());
61     }
62
63     cloneEntries() {
64         this.cloneDialog.open().subscribe(
65             result => {
66                 this._handleClone(result.source_library, result.target_library, result.overwrite_target);
67             }
68         );
69     }
70
71     deleteSelected(idlThings: IdlObject[]) {
72         this.delConfirm.open().subscribe(confirmed => {
73             if (!confirmed) { return; }
74             super.deleteSelected(idlThings);
75         });
76     }
77
78     _handleClone(src: number, tgt: number, overwrite: Boolean) {
79         const updates: IdlObject[] = [];
80
81         const delObs = (overwrite) ?
82             this.pcrud.search('cusppe', { owner: tgt }, {}, {}) :
83             empty();
84         const newObs = this.pcrud.search('cusppe', { owner: src }, {}, {});
85         merge(delObs, newObs).subscribe(
86             entry => {
87                 if (entry.owner() === tgt) {
88                     entry.isdeleted(true);
89                 } else {
90                     entry.owner(tgt);
91                     entry.id(null);
92                     entry.isnew(true);
93                 }
94                 updates.push(entry);
95             },
96             err => {},
97         ).add(() => {
98             this.pcrud.autoApply(updates).subscribe(
99                 val => {},
100                 err => {
101                     this.cloneFailedString.current()
102                         .then(str => this.toast.danger(str));
103                 },
104                 () => {
105                     this.cloneSuccessString.current()
106                         .then(str => this.toast.success(str));
107                     this.searchOrgs = {primaryOrgId: tgt}; // change the org filter to the
108                                                            // the one we just cloned into
109                     this.grid.reload();
110                 }
111             );
112         });
113     }
114 }