contacts modal
authorMike Rylander <mrylander@gmail.com>
Wed, 25 Mar 2020 14:46:45 +0000 (10:46 -0400)
committerGalen Charlton <gmc@equinoxinitiative.org>
Wed, 25 Mar 2020 14:50:11 +0000 (10:50 -0400)
Signed-off-by: Mike Rylander <mrylander@gmail.com>
Signed-off-by: Galen Charlton <gmc@equinoxinitiative.org>
Open-ILS/src/eg2/src/app/staff/acq/provider/provider-contacts.component.html
Open-ILS/src/eg2/src/app/staff/acq/provider/provider-contacts.component.ts

index e109504..7152c2f 100644 (file)
@@ -1,18 +1,36 @@
+<eg-string #createString i18n-text text="New Provider Contact Added"></eg-string>
+<eg-string #createErrString i18n-text text="Failed to Create New Provider Contact"></eg-string>
+<eg-string #successString i18n-text text="Provider Contact Update Succeeded"></eg-string>
+<eg-string #updateFailedString i18n-text text="Provider Contact Update Failed"></eg-string>
+<eg-string #deleteFailedString i18n-text text="Delete of Provider Contact failed or was not allowed"></eg-string>
+<eg-string #deleteSuccessString i18n-text text="Delete of Provider Contact succeeded"></eg-string>
+
 
 <ng-template #emailTmpl let-contact="row">
   <a href="mailto:{{contact.email()}}">{{contact.email()}}</a>
 </ng-template>
 
 <ng-template #phoneTmpl let-contact="row">
-  <a href="mailto:{{contact.phone()}}">{{contact.phone()}}</a>
+  <a href="tel:{{contact.phone()}}">{{contact.phone()}}</a>
 </ng-template>
 
-<eg-grid #acqSearchProviderGrid
+<eg-grid #acqProviderContactsGrid
   persistKey="acq.provider.contacts.grid"
   idlClass="acqpc" [dataSource]="gridSource"
   [sortable]="true"
   [cellTextGenerator]="cellTextGenerator">
+
+  <eg-grid-toolbar-button label="New Provider Contact" i18n-label (onClick)="createNew()"></eg-grid-toolbar-button>
+  <eg-grid-toolbar-action label="Edit Selected" i18n-label (onClick)="editSelected($event)"></eg-grid-toolbar-action>
+  <eg-grid-toolbar-action label="Delete Selected" i18n-label (onClick)="deleteSelected($event)"></eg-grid-toolbar-action>
+
   <eg-grid-column path="email" [cellTemplate]="emailTmpl" [disableTooltip]="true"></eg-grid-column>
   <eg-grid-column path="phone" [cellTemplate]="phoneTmpl" [disableTooltip]="true"></eg-grid-column>
 </eg-grid>
 
+<eg-fm-record-editor #editDialog
+  idlClass="acqpc"
+  readonlyFields="id,provider"
+  fieldOrder="id,provider,name,role,email,phone">
+</eg-fm-record-editor>
+
index ba06c5d..e42e3a7 100644 (file)
@@ -1,42 +1,89 @@
-import {Component, OnInit, Input, ViewChild} from '@angular/core';
+import {Component, OnInit, AfterViewInit, Input, ViewChild} from '@angular/core';
 import {empty, throwError, Observable, from} from 'rxjs';
 import {map} from 'rxjs/operators';
 import {Router, ActivatedRoute, ParamMap} from '@angular/router';
 import {Pager} from '@eg/share/util/pager';
-import {IdlObject} from '@eg/core/idl.service';
+import {IdlService, IdlObject} from '@eg/core/idl.service';
 import {NetService} from '@eg/core/net.service';
 import {AuthService} from '@eg/core/auth.service';
 import {GridComponent} from '@eg/share/grid/grid.component';
 import {GridDataSource, GridCellTextGenerator} from '@eg/share/grid/grid';
 import {ProviderRecordService} from './provider-record.service';
 import {AcqProviderSearchFormComponent} from './acq-provider-search-form.component';
+import {PcrudService} from '@eg/core/pcrud.service';
+import {FmRecordEditorComponent} from '@eg/share/fm-editor/fm-editor.component';
+import {StringComponent} from '@eg/share/string/string.component';
+import {ToastService} from '@eg/share/toast/toast.service';
+
 
 @Component({
   selector: 'eg-provider-contacts',
   templateUrl: 'provider-contacts.component.html',
 })
-export class ProviderContactsComponent implements OnInit {
+export class ProviderContactsComponent implements OnInit, AfterViewInit {
 
     @Input() providerId: any;
     contacts: any[] = [];
 
     gridSource: GridDataSource;
+    @ViewChild('editDialog', { static: true }) editDialog: FmRecordEditorComponent;
     @ViewChild('acqProviderContactsGrid', { static: true }) providerContactsGrid: GridComponent;
+    @ViewChild('successString', { static: true }) successString: StringComponent;
+    @ViewChild('createString', { static: false }) createString: StringComponent;
+    @ViewChild('createErrString', { static: false }) createErrString: StringComponent;
+    @ViewChild('updateFailedString', { static: false }) updateFailedString: StringComponent;
+    @ViewChild('deleteFailedString', { static: true }) deleteFailedString: StringComponent;
+    @ViewChild('deleteSuccessString', { static: true }) deleteSuccessString: StringComponent;
 
     cellTextGenerator: GridCellTextGenerator;
     provider: IdlObject;
 
+    canCreate: boolean;
+    canDelete: boolean;
+    deleteSelected: (rows: IdlObject[]) => void;
+
+    permissions: {[name: string]: boolean};
+
+    // Size of create/edito dialog.  Uses large by default.
+    @Input() dialogSize: 'sm' | 'lg' = 'lg';
+
     constructor(
         private router: Router,
         private route: ActivatedRoute,
         private net: NetService,
         private auth: AuthService,
-        private providerRecord: ProviderRecordService) {
+        private idl: IdlService,
+        private providerRecord: ProviderRecordService,
+        private pcrud: PcrudService,
+        private toast: ToastService) {
+
     }
 
     ngOnInit() {
         this.gridSource = this.getDataSource()
         this.cellTextGenerator = {};
+        this.deleteSelected = (idlThings: IdlObject[]) => {
+            idlThings.forEach(idlThing => idlThing.isdeleted(true));
+            this.pcrud.autoApply(idlThings).subscribe(
+                val => {
+                    console.debug('deleted: ' + val);
+                    this.deleteSuccessString.current()
+                        .then(str => this.toast.success(str));
+                },
+                err => {
+                    this.deleteFailedString.current()
+                        .then(str => this.toast.danger(str));
+                },
+                ()  => this.providerContactsGrid.reload()
+            );
+        };
+        this.providerContactsGrid.onRowActivate.subscribe(
+            (idlThing: IdlObject) => this.showEditDialog(idlThing)
+        );
+    }
+
+    ngAfterViewInit() {
+        console.log('this.providerRecord',this.providerRecord);
     }
 
     getDataSource(): GridDataSource {
@@ -70,4 +117,56 @@ export class ProviderContactsComponent implements OnInit {
         return gridSource;
     }
 
+    showEditDialog(providerContact: IdlObject): Promise<any> {
+        this.editDialog.mode = 'update';
+        this.editDialog.recordId = providerContact['id']();
+        return new Promise((resolve, reject) => {
+            this.editDialog.open({size: this.dialogSize}).subscribe(
+                result => {
+                    this.successString.current()
+                        .then(str => this.toast.success(str));
+                    this.providerContactsGrid.reload();
+                    resolve(result);
+                },
+                error => {
+                    this.updateFailedString.current()
+                        .then(str => this.toast.danger(str));
+                    reject(error);
+                }
+            );
+        });
+    }
+
+    editSelected(providerContactFields: IdlObject[]) {
+        // Edit each IDL thing one at a time
+        const editOneThing = (providerContact: IdlObject) => {
+            if (!providerContact) { return; }
+
+            this.showEditDialog(providerContact).then(
+                () => editOneThing(providerContactFields.shift()));
+        };
+
+        editOneThing(providerContactFields.shift());
+    }
+
+    createNew() {
+        this.editDialog.mode = 'create';
+        const contact = this.idl.create('acqpc');
+        contact.provider(this.provider.id());
+        this.editDialog.record = contact;
+        this.editDialog.recordId = null;
+        this.editDialog.open({size: this.dialogSize}).subscribe(
+            ok => {
+                this.createString.current()
+                    .then(str => this.toast.success(str));
+                this.providerContactsGrid.reload();
+            },
+            rejection => {
+                if (!rejection.dismissed) {
+                    this.createErrString.current()
+                        .then(str => this.toast.danger(str));
+                }
+            }
+        );
+    }
 }