Skip to content

Commit

Permalink
fix: minor address form issues (#1451)
Browse files Browse the repository at this point in the history
  • Loading branch information
SGrueber committed Jun 30, 2023
1 parent 57d0fa8 commit 8d9a803
Showing 1 changed file with 13 additions and 6 deletions.
Expand Up @@ -2,7 +2,7 @@ import { ChangeDetectionStrategy, Component, Input, OnChanges, OnInit, SimpleCha
import { FormControl, FormGroup } from '@angular/forms';
import { FormlyFieldConfig, FormlyFormOptions } from '@ngx-formly/core';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { filter, map, shareReplay, tap } from 'rxjs/operators';

import { AppFacade } from 'ish-core/facades/app.facade';
import { Address } from 'ish-core/models/address/address.model';
Expand Down Expand Up @@ -50,9 +50,12 @@ export class FormlyAddressFormComponent implements OnInit, OnChanges {
constructor(private appFacade: AppFacade, private afcProvider: AddressFormConfigurationProvider) {}

ngOnInit(): void {
this.countries$ = this.appFacade
.countries$()
?.pipe(map(countries => countries?.map(country => ({ value: country.countryCode, label: country.name }))));
this.countries$ = this.appFacade.countries$()?.pipe(
filter(countries => !!countries?.length),
map(countries => countries?.map(country => ({ value: country.countryCode, label: country.name }))),
tap(() => this.fillForm(this.prefilledAddress)),
shareReplay(1)
);
this.initForm();
this.parentForm?.setControl('address', this.addressForm);
}
Expand Down Expand Up @@ -123,13 +126,17 @@ export class FormlyAddressFormComponent implements OnInit, OnChanges {
...configuration.getModel(),
};
this.addressFields = [this.createCountrySelectField()].concat(configuration.getFieldConfiguration());
this.fillForm(this.prefilledAddress);
}

private fillForm(prefilledAddress: Partial<Address> = {}) {
if (!this.addressForm || Object.keys(prefilledAddress).length === 0) {
if (!this.addressForm) {
return;
}
if (Object.keys(prefilledAddress).length === 0) {
this.addressModel = {
countryCode: '',
};
}

this.addressModel.countryCode = prefilledAddress.countryCode;
this.handleCountryChange(this.addressModel);
Expand Down

0 comments on commit 8d9a803

Please sign in to comment.