Browse Source

feat(admin-ui): Allow OrderAddress custom fields to be modified

Relates to #979
Michael Bromley 4 years ago
parent
commit
175e61a5b6

+ 18 - 0
packages/admin-ui/src/lib/order/src/components/order-editor/order-editor.component.ts

@@ -14,6 +14,7 @@ import {
     ModalService,
     ModalService,
     ModifyOrderInput,
     ModifyOrderInput,
     NotificationService,
     NotificationService,
+    OrderAddressFragment,
     OrderDetail,
     OrderDetail,
     ProductSelectorSearch,
     ProductSelectorSearch,
     ServerConfigService,
     ServerConfigService,
@@ -137,6 +138,7 @@ export class OrderEditorComponent
                     countryCode: new FormControl(order.shippingAddress?.countryCode),
                     countryCode: new FormControl(order.shippingAddress?.countryCode),
                     phoneNumber: new FormControl(order.shippingAddress?.phoneNumber),
                     phoneNumber: new FormControl(order.shippingAddress?.phoneNumber),
                 });
                 });
+                this.addAddressCustomFieldsFormGroup(this.shippingAddressForm, order.shippingAddress);
             }
             }
             if (!this.billingAddressForm) {
             if (!this.billingAddressForm) {
                 this.billingAddressForm = new FormGroup({
                 this.billingAddressForm = new FormGroup({
@@ -150,6 +152,7 @@ export class OrderEditorComponent
                     countryCode: new FormControl(order.billingAddress?.countryCode),
                     countryCode: new FormControl(order.billingAddress?.countryCode),
                     phoneNumber: new FormControl(order.billingAddress?.phoneNumber),
                     phoneNumber: new FormControl(order.billingAddress?.phoneNumber),
                 });
                 });
+                this.addAddressCustomFieldsFormGroup(this.billingAddressForm, order.billingAddress);
             }
             }
             this.orderLineCustomFieldsFormArray = new FormArray([]);
             this.orderLineCustomFieldsFormArray = new FormArray([]);
             for (const line of order.lines) {
             for (const line of order.lines) {
@@ -430,6 +433,21 @@ export class OrderEditorComponent
             });
             });
     }
     }
 
 
+    private addAddressCustomFieldsFormGroup(
+        parentFormGroup: FormGroup,
+        address?: OrderAddressFragment | null,
+    ) {
+        if (address && this.addressCustomFields.length) {
+            const addressCustomFieldsFormGroup = new FormGroup({});
+            for (const customFieldDef of this.addressCustomFields) {
+                const name = customFieldDef.name;
+                const value = (address as any).customFields?.[name];
+                addressCustomFieldsFormGroup.addControl(name, new FormControl(value));
+            }
+            parentFormGroup.addControl('customFields', addressCustomFieldsFormGroup);
+        }
+    }
+
     protected setFormValues(entity: OrderDetail.Fragment, languageCode: LanguageCode): void {
     protected setFormValues(entity: OrderDetail.Fragment, languageCode: LanguageCode): void {
         /* not used */
         /* not used */
     }
     }