-<ngb-tabset #addressTabs *ngIf="orgUnit">
+<ngb-tabset #addressTabs *ngIf="orgUnit" (tabChange)="tabChanged($event)">
<ng-container *ngFor="let type of addrTypes()">
<b>type = {{type}}</b>
- <ngb-tab
+ <ngb-tab *ngIf="addr(type)"
i18n-title id="{{type}}"
title="{{type === 'billing_address' ? 'Physical Address' :
(type === 'holds_address' ? 'Holds Address' :
<eg-fm-record-editor idlClass="aoa" readonlyFields="org_unit"
[mode]="addr(type).isnew() ? 'create': 'update'"
[hideBanner]="true" displayMode="inline" hiddenFields="id"
- [showDelete]="true" (recordSaved)="addrSaved($event)"
- (recordDeleted)="addrDeleted($event)"
+ (recordSaved)="addrSaved($event)"
[record]="addr(type)"
fieldOrder="address_type,street1,street2,city,county,state,country,post_code,san,valid"
>
+ <eg-fm-record-editor-action i18n-label label="Delete" *ngIf="!addr(type).isnew()"
+ (actionClick)="deleteAddress($event)" buttonCss="btn-warning">
+ </eg-fm-record-editor-action>
</eg-fm-record-editor>
<ng-container *ngIf="sharedAddress(addr(type).id())">
import {IdlService, IdlObject} from '@eg/core/idl.service';
import {OrgService} from '@eg/core/org.service';
import {PcrudService} from '@eg/core/pcrud.service';
+import {NgbTabChangeEvent} from '@ng-bootstrap/ng-bootstrap';
const ADDR_TYPES =
['billing_address', 'holds_address', 'mailing_address', 'ill_address'];
export class OrgAddressComponent {
private orgUnit: IdlObject = null;
+ private tabName: string;
private _orgId: number;
private pcrud: PcrudService
) {
this.addrChange = new EventEmitter<IdlObject>();
+ this.tabName = 'billing_address';
}
init() {
});
}
+ tabChanged($event: NgbTabChangeEvent) {
+ this.tabName = $event.nextId;
+ }
+
addrTypes(): string[] { // for UI
return ADDR_TYPES;
}
const addr = this.idl.create('aoa');
addr.isnew(true);
addr.valid('t');
- addr.org_unit(this.orgUnit);
+ addr.org_unit(this.orgId);
this.orgUnit[addrType](addr);
}
}).length > 1;
}
+ deleteAddress($event: any) {
+ const addr = $event.record;
+ const tmpOrg = this.updatableOrg();
+
+ // Set the FKey to NULL on the org unit for deleted addresses
+ ADDR_TYPES.forEach(aType => {
+ const a = this.addr(aType);
+ if (a && a.id() === addr.id()) {
+ tmpOrg[aType](null);
+ this.createAddress(aType);
+ }
+ });
+
+ this.pcrud.update(tmpOrg).toPromise()
+ .then(_ => this.pcrud.remove(addr).toPromise())
+ .then(_ => this.addrChange.emit(addr));
+ }
+
+ // Addr saved by fm-editor.
+ // In the case of new address creation, point the org unit at
+ // the new address ID.
addrSaved(addr: number | IdlObject) {
- const id = typeof addr === 'object' ? addr.id() : addr;
- // TODO: update org if needed...
+ if (typeof addr !== 'object') {
+ // pcrud returns a number on 'update' calls. No need to
+ // reload the data on a simple address change. it's changed
+ // in place.
+ return;
+ }
+
+ // update local copy with version that has an ID.
+ this.orgUnit[this.tabName](addr);
+
+ const org = this.updatableOrg();
+ org[this.tabName](addr.id());
+
+ // Creating a new address -- tell our org about it.
+ this.pcrud.update(org).toPromise().then(_ => this.addrChange.emit(addr));
}
- addrDeleted(addr: number | IdlObject) {
- const id = typeof addr === 'object' ? addr.id() : addr;
- // TODO: update org
+ // Create an unfleshed org unit object that's a clone of this.orgUnit
+ // to use when pushing updates to the server.
+ updatableOrg(): IdlObject {
+ const org = this.idl.clone(this.orgUnit);
+
+ ADDR_TYPES.forEach(aType => {
+ const addr = this.addr(aType);
+ if (addr) { org[aType](addr.id()); }
+ });
+
+ return org;
}
+
}
hiddenFields="id,billing_address,mailing_address,holds_address,ill_address">
<eg-fm-record-editor-action label="Add Child" i18n-label
[disabled]="orgChildTypes().length === 0 || currentOrg().isnew()"
+ buttonCss="btn-outline-info"
(actionClick)="addChild()"></eg-fm-record-editor-action>
</eg-fm-record-editor>
</div>
</div>
</div>
<div class="row d-flex justify-content-end">
+ <div class="alert alert-warning mr-2 p-1"
+ *ngIf="currentOrg().hours_of_operation().isnew()">
+ Hours of Operation Have Not Yet Been Saved.
+ </div>
+ <div class="mr-2">
+ <button class="btn btn-warning" (click)="deleteHours()" i18n>
+ Clear Hours of Operation
+ </button>
+ </div>
<div>
<button class="btn btn-info" (click)="saveHours()" i18n>
Apply Changes