+<eg-string #createString i18n-text text="New Provider Address Added"></eg-string>
+<eg-string #createErrString i18n-text text="Failed to Create New Provider Address"></eg-string>
+<eg-string #successString i18n-text text="Provider Address Update Succeeded"></eg-string>
+<eg-string #updateFailedString i18n-text text="Provider Address Update Failed"></eg-string>
+<eg-string #deleteFailedString i18n-text text="Delete of Provider Address failed or was not allowed"></eg-string>
+<eg-string #deleteSuccessString i18n-text text="Delete of Provider Address succeeded"></eg-string>
-<eg-grid #acqSearchProviderGrid
+<eg-grid #acqProviderAddressesGrid
persistKey="acq.provider.addresses.grid"
idlClass="acqpa" [dataSource]="gridSource"
[sortable]="true"
[cellTextGenerator]="cellTextGenerator">
+ <eg-grid-toolbar-button
+ label="New Provider Address" 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>
+<eg-fm-record-editor #editDialog
+ idlClass="acqpa"
+ readonlyFields="id,provider"
+ fieldOrder="id,valid,address_type,provider,street1,street2,city,county,state,country,post_code,fax_phone">
+</eg-fm-record-editor>
+
-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 {ProviderRecord, 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-addresses',
templateUrl: 'provider-addresses.component.html',
})
-export class ProviderAddressesComponent implements OnInit {
+export class ProviderAddressesComponent implements OnInit, AfterViewInit {
addresses: any[] = [];
gridSource: GridDataSource;
+ @ViewChild('editDialog', { static: true }) editDialog: FmRecordEditorComponent;
@ViewChild('acqProviderAddressesGrid', { static: true }) providerAddressesGrid: 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 idl: IdlService,
private auth: AuthService,
- private providerRecord: ProviderRecordService) {
+ 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.providerAddressesGrid.reload()
+ );
+ };
+ this.providerAddressesGrid.onRowActivate.subscribe(
+ (idlThing: IdlObject) => this.showEditDialog(idlThing)
+ );
+ }
+
+ ngAfterViewInit() {
+ console.log('this.providerRecord',this.providerRecord);
}
getDataSource(): GridDataSource {
return gridSource;
}
+ showEditDialog(providerAddress: IdlObject): Promise<any> {
+ this.editDialog.mode = 'update';
+ this.editDialog.recordId = providerAddress['id']();
+ return new Promise((resolve, reject) => {
+ this.editDialog.open({size: this.dialogSize}).subscribe(
+ result => {
+ this.successString.current()
+ .then(str => this.toast.success(str));
+ this.providerAddressesGrid.reload();
+ resolve(result);
+ },
+ error => {
+ this.updateFailedString.current()
+ .then(str => this.toast.danger(str));
+ reject(error);
+ }
+ );
+ });
+ }
+
+ editSelected(providerAddressFields: IdlObject[]) {
+ // Edit each IDL thing one at a time
+ const editOneThing = (providerAddress: IdlObject) => {
+ if (!providerAddress) { return; }
+
+ this.showEditDialog(providerAddress).then(
+ () => editOneThing(providerAddressFields.shift()));
+ };
+
+ editOneThing(providerAddressFields.shift());
+ }
+
+ createNew() {
+ this.editDialog.mode = 'create';
+ const address = this.idl.create('acqpa');
+ address.provider(this.provider.id());
+ this.editDialog.record = address;
+ this.editDialog.recordId = null;
+ this.editDialog.open({size: this.dialogSize}).subscribe(
+ ok => {
+ this.createString.current()
+ .then(str => this.toast.success(str));
+ this.providerAddressesGrid.reload();
+ },
+ rejection => {
+ if (!rejection.dismissed) {
+ this.createErrString.current()
+ .then(str => this.toast.danger(str));
+ }
+ }
+ );
+ }
}