Skip to content

Commit

Permalink
fix(admin-ui): Display custom fields in Address form
Browse files Browse the repository at this point in the history
Fixes #455
  • Loading branch information
michaelbromley committed Sep 8, 2020
1 parent 5587144 commit f074f65
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@
<label>{{ 'customer.phone-number' | translate }}</label>
<input formControlName="phoneNumber" type="text" clrInput />
</clr-input-container>
<section formGroupName="customFields" *ngIf="addressForm.get('customFields') as customFieldsGroup">
<label>{{ 'common.custom-fields' | translate }}</label>
<ng-container *ngFor="let customField of customFields">
<vdr-custom-field-control
entityName="Facet"
[customFieldsFormGroup]="customFieldsGroup"
[customField]="customField"
></vdr-custom-field-control>
</ng-container>
</section>
</form>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ChangeDetectionStrategy, Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';

import { GetAvailableCountries } from '@vendure/admin-ui/core';
import { CustomFieldConfig, GetAvailableCountries } from '@vendure/admin-ui/core';

@Component({
selector: 'vdr-address-card',
Expand All @@ -12,6 +11,7 @@ import { GetAvailableCountries } from '@vendure/admin-ui/core';
export class AddressCardComponent implements OnInit {
editing = false;
@Input() addressForm: FormGroup;
@Input() customFields: CustomFieldConfig;
@Input() availableCountries: GetAvailableCountries.Items[] = [];
@Input() isDefaultBilling: string;
@Input() isDefaultShipping: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ <h3>{{ 'customer.addresses' | translate }}</h3>
[isDefaultBilling]="defaultBillingAddressId === addressForm.value.id"
[isDefaultShipping]="defaultShippingAddressId === addressForm.value.id"
[addressForm]="addressForm"
[customFields]="addressCustomFields"
(setAsDefaultBilling)="setDefaultBillingAddressId($event)"
(setAsDefaultShipping)="setDefaultShippingAddressId($event)"
></vdr-address-card>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export class CustomerDetailComponent extends BaseDetailComponent<CustomerWithOrd
implements OnInit, OnDestroy {
detailForm: FormGroup;
customFields: CustomFieldConfig[];
addressCustomFields: CustomFieldConfig[];
availableCountries$: Observable<GetAvailableCountries.Items[]>;
orders$: Observable<GetCustomer.Items[]>;
ordersCount$: Observable<number>;
Expand All @@ -74,6 +75,7 @@ export class CustomerDetailComponent extends BaseDetailComponent<CustomerWithOrd
super(route, router, serverConfigService, dataService);

this.customFields = this.getCustomFieldConfig('Customer');
this.addressCustomFields = this.getCustomFieldConfig('Address');
this.detailForm = this.formBuilder.group({
customer: this.formBuilder.group({
title: '',
Expand Down Expand Up @@ -245,6 +247,7 @@ export class CustomerDetailComponent extends BaseDetailComponent<CustomerWithOrd
phoneNumber: address.phoneNumber,
defaultShippingAddress: this.defaultShippingAddressId === address.id,
defaultBillingAddress: this.defaultBillingAddressId === address.id,
customFields: address.customFields,
};
if (!address.id) {
saveOperations.push(
Expand Down Expand Up @@ -403,15 +406,29 @@ export class CustomerDetailComponent extends BaseDetailComponent<CustomerWithOrd
if (entity.addresses) {
const addressesArray = new FormArray([]);
for (const address of entity.addresses) {
addressesArray.push(
this.formBuilder.group({ ...address, countryCode: address.country.code }),
);
const { customFields, ...rest } = address as any;
const addressGroup = this.formBuilder.group({
...rest,
countryCode: address.country.code,
});
addressesArray.push(addressGroup);
if (address.defaultShippingAddress) {
this.defaultShippingAddressId = address.id;
}
if (address.defaultBillingAddress) {
this.defaultBillingAddressId = address.id;
}

if (this.addressCustomFields.length) {
const customFieldsGroup = this.formBuilder.group({});
for (const fieldDef of this.addressCustomFields) {
const key = fieldDef.name;
const value = (address as any).customFields?.[key];
const control = new FormControl(value);
customFieldsGroup.addControl(key, control);
}
addressGroup.addControl('customFields', customFieldsGroup);
}
}
this.detailForm.setControl('addresses', addressesArray);
}
Expand Down

0 comments on commit f074f65

Please sign in to comment.