]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/holdings/copy-notes-dialog.component.ts
LP1959048: manual ng lint fixes
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / share / holdings / copy-notes-dialog.component.ts
1 import {Component, Input, ViewChild} from '@angular/core';
2 import {Observable, throwError, from, empty} from 'rxjs';
3 import {switchMap} from 'rxjs/operators';
4 import {NetService} from '@eg/core/net.service';
5 import {IdlService, IdlObject} from '@eg/core/idl.service';
6 import {ToastService} from '@eg/share/toast/toast.service';
7 import {AuthService} from '@eg/core/auth.service';
8 import {PcrudService} from '@eg/core/pcrud.service';
9 import {OrgService} from '@eg/core/org.service';
10 import {StringComponent} from '@eg/share/string/string.component';
11 import {DialogComponent} from '@eg/share/dialog/dialog.component';
12 import {NgbModal, NgbModalOptions} from '@ng-bootstrap/ng-bootstrap';
13
14 /**
15  * Dialog for managing copy notes.
16  */
17
18 export interface CopyNotesChanges {
19     newNotes: IdlObject[];
20     delNotes: IdlObject[];
21 }
22
23 @Component({
24   selector: 'eg-copy-notes-dialog',
25   templateUrl: 'copy-notes-dialog.component.html'
26 })
27
28 export class CopyNotesDialogComponent
29     extends DialogComponent {
30
31     // If there are multiple copyIds, only new notes may be applied.
32     // If there is only one copyId, then notes may be applied or removed.
33     @Input() copyIds: number[] = [];
34
35     mode: string; // create | manage
36
37     // If true, no attempt is made to save the new notes to the
38     // database.  It's assumed this takes place in the calling code.
39     @Input() inPlaceCreateMode = false;
40
41     // In 'create' mode, we may be adding notes to multiple copies.
42     copies: IdlObject[] = [];
43
44     // In 'manage' mode we only handle a single copy.
45     copy: IdlObject;
46
47     curNote: string;
48     curNoteTitle: string;
49     curNotePublic = false;
50     newNotes: IdlObject[] = [];
51     delNotes: IdlObject[] = [];
52
53     autoId = -1;
54
55     @ViewChild('successMsg', { static: true }) private successMsg: StringComponent;
56     @ViewChild('errorMsg', { static: true }) private errorMsg: StringComponent;
57
58     constructor(
59         private modal: NgbModal, // required for passing to parent
60         private toast: ToastService,
61         private net: NetService,
62         private idl: IdlService,
63         private pcrud: PcrudService,
64         private org: OrgService,
65         private auth: AuthService) {
66         super(modal); // required for subclassing
67     }
68
69     /**
70      */
71     open(args: NgbModalOptions): Observable<CopyNotesChanges> {
72         this.copy = null;
73         this.copies = [];
74         this.newNotes = [];
75         this.delNotes = [];
76
77         if (this.copyIds.length === 0 && !this.inPlaceCreateMode) {
78             return throwError('copy ID required');
79         }
80
81         // In manage mode, we can only manage a single copy.
82         // But in create mode, we can add notes to multiple copies.
83         // We can only manage copies that already exist in the database.
84         if (this.copyIds.length === 1 && this.copyIds[0] > 0) {
85             this.mode = 'manage';
86         } else {
87             this.mode = 'create';
88         }
89
90         // Observify data loading
91         const obs = from(this.getCopies());
92
93         // Return open() observable to caller
94         return obs.pipe(switchMap(_ => super.open(args)));
95     }
96
97     getCopies(): Promise<any> {
98
99         // Avoid fetch if we're only adding notes to isnew copies.
100         const ids = this.copyIds.filter(id => id > 0);
101         if (ids.length === 0) { return Promise.resolve(); }
102
103         return this.pcrud.search('acp', {id: this.copyIds},
104             {flesh: 1, flesh_fields: {acp: ['notes']}},
105             {atomic: true}
106         )
107         .toPromise().then(copies => {
108             this.copies = copies;
109             if (copies.length === 1) {
110                 this.copy = copies[0];
111             }
112         });
113     }
114
115     removeNote(note: IdlObject) {
116         this.newNotes = this.newNotes.filter(t => t.id() !== note.id());
117
118         if (note.isnew() || this.mode === 'create') { return; }
119
120         const existing = this.copy.notes().filter(n => n.id() === note.id())[0];
121         if (!existing) { return; }
122
123         existing.isdeleted(true);
124         this.delNotes.push(existing);
125
126         // Remove from copy for dialog display
127         this.copy.notes(this.copy.notes().filter(n => n.id() !== note.id()));
128     }
129
130     addNew() {
131         if (!this.curNoteTitle || !this.curNote) { return; }
132
133         const note = this.idl.create('acpn');
134         note.isnew(true);
135         note.creator(this.auth.user().id());
136         note.pub(this.curNotePublic ? 't' : 'f');
137         note.title(this.curNoteTitle);
138         note.value(this.curNote);
139         note.id(this.autoId--);
140
141         this.newNotes.push(note);
142
143         this.curNote = '';
144         this.curNoteTitle = '';
145         this.curNotePublic = false;
146     }
147
148     applyChanges() {
149
150         if (this.inPlaceCreateMode) {
151             this.close({ newNotes: this.newNotes, delNotes: this.delNotes });
152             return;
153         }
154
155         const notes = [];
156         this.newNotes.forEach(note => {
157             this.copies.forEach(copy => {
158                 const n = this.idl.clone(note);
159                 n.id(null); // remove temp ID, it will be duped
160                 n.owning_copy(copy.id());
161                 notes.push(n);
162             });
163         });
164
165         this.pcrud.create(notes).toPromise()
166         .then(_ => {
167             if (this.delNotes.length) {
168                 return this.pcrud.remove(this.delNotes).toPromise();
169             }
170         }).then(_ => {
171             this.successMsg.current().then(msg => this.toast.success(msg));
172             this.close({ newNotes: this.newNotes, delNotes: this.delNotes });
173         });
174     }
175 }
176