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';
15 * Dialog for managing copy notes.
18 export interface CopyNotesChanges {
19 newNotes: IdlObject[];
20 delNotes: IdlObject[];
24 selector: 'eg-copy-notes-dialog',
25 templateUrl: 'copy-notes-dialog.component.html'
28 export class CopyNotesDialogComponent
29 extends DialogComponent {
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[] = [];
35 mode: string; // create | manage
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;
41 // In 'create' mode, we may be adding notes to multiple copies.
42 copies: IdlObject[] = [];
44 // In 'manage' mode we only handle a single copy.
49 curNotePublic = false;
50 newNotes: IdlObject[] = [];
51 delNotes: IdlObject[] = [];
55 @ViewChild('successMsg', { static: true }) private successMsg: StringComponent;
56 @ViewChild('errorMsg', { static: true }) private errorMsg: StringComponent;
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
71 open(args: NgbModalOptions): Observable<CopyNotesChanges> {
77 if (this.copyIds.length === 0 && !this.inPlaceCreateMode) {
78 return throwError('copy ID required');
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) {
90 // Observify data loading
91 const obs = from(this.getCopies());
93 // Return open() observable to caller
94 return obs.pipe(switchMap(_ => super.open(args)));
97 getCopies(): Promise<any> {
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(); }
103 return this.pcrud.search('acp', {id: this.copyIds},
104 {flesh: 1, flesh_fields: {acp: ['notes']}},
107 .toPromise().then(copies => {
108 this.copies = copies;
109 if (copies.length === 1) {
110 this.copy = copies[0];
115 removeNote(note: IdlObject) {
116 this.newNotes = this.newNotes.filter(t => t.id() !== note.id());
118 if (note.isnew() || this.mode === 'create') { return; }
120 const existing = this.copy.notes().filter(n => n.id() === note.id())[0];
121 if (!existing) { return; }
123 existing.isdeleted(true);
124 this.delNotes.push(existing);
126 // Remove from copy for dialog display
127 this.copy.notes(this.copy.notes().filter(n => n.id() !== note.id()));
131 if (!this.curNoteTitle || !this.curNote) { return; }
133 const note = this.idl.create('acpn');
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--);
141 this.newNotes.push(note);
144 this.curNoteTitle = '';
145 this.curNotePublic = false;
150 if (this.inPlaceCreateMode) {
151 this.close({ newNotes: this.newNotes, delNotes: this.delNotes });
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());
165 this.pcrud.create(notes).toPromise()
167 if (this.delNotes.length) {
168 return this.pcrud.remove(this.delNotes).toPromise();
171 this.successMsg.current().then(msg => this.toast.success(msg));
172 this.close({ newNotes: this.newNotes, delNotes: this.delNotes });