Skip to content

Commit

Permalink
fix: display company input fields for anonymous b2b users on checkout…
Browse files Browse the repository at this point in the history
… edit address form (#1312)
  • Loading branch information
SGrueber committed Nov 21, 2022
1 parent b2c265b commit 23a9257
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ export class FormlyAddressFormComponent implements OnInit, OnChanges {

this.addressModel.countryCode = model.countryCode;
this.addressForm.updateValueAndValidity();
this.addressForm.get('countryCode').markAsDirty();
if (model.countryCode) {
this.addressForm.get('countryCode').markAsDirty();
}

this.parentForm?.setControl('address', this.addressForm);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { of } from 'rxjs';
import { anything, instance, mock, spy, verify, when } from 'ts-mockito';

import { AccountFacade } from 'ish-core/facades/account.facade';
import { FeatureToggleModule } from 'ish-core/feature-toggle.module';
import { FormlyAddressExtensionFormComponent } from 'ish-shared/formly-address-forms/components/formly-address-extension-form/formly-address-extension-form.component';
import { FormlyAddressFormComponent } from 'ish-shared/formly-address-forms/components/formly-address-form/formly-address-form.component';

Expand All @@ -26,7 +27,11 @@ describe('Formly Customer Address Form Component', () => {
MockComponent(FormlyAddressExtensionFormComponent),
MockComponent(FormlyAddressFormComponent),
],
imports: [ReactiveFormsModule, TranslateModule.forRoot()],
imports: [
FeatureToggleModule.forTesting('businessCustomerRegistration'),
ReactiveFormsModule,
TranslateModule.forRoot(),
],
providers: [{ provide: AccountFacade, useFactory: () => instance(accountFacade) }],
}).compileComponents();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import {
EventEmitter,
Input,
OnChanges,
OnDestroy,
OnInit,
Output,
SimpleChanges,
ViewChild,
} from '@angular/core';
import { FormGroup, FormGroupDirective } from '@angular/forms';
import { Observable, Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { Observable } from 'rxjs';
import { map, withLatestFrom } from 'rxjs/operators';

import { AccountFacade } from 'ish-core/facades/account.facade';
import { FeatureToggleService } from 'ish-core/feature-toggle.module';
import { Address } from 'ish-core/models/address/address.model';
import { markAsDirtyRecursive } from 'ish-shared/forms/utils/form-utils';

Expand All @@ -35,7 +35,7 @@ import { markAsDirtyRecursive } from 'ish-shared/forms/utils/form-utils';
templateUrl: './formly-customer-address-form.component.html',
changeDetection: ChangeDetectionStrategy.Default,
})
export class FormlyCustomerAddressFormComponent implements OnInit, OnChanges, OnDestroy {
export class FormlyCustomerAddressFormComponent implements OnInit, OnChanges {
@Input() address: Partial<Address>;
@Input() resetForm = false;
// display address extension form fields
Expand All @@ -54,9 +54,7 @@ export class FormlyCustomerAddressFormComponent implements OnInit, OnChanges, On

@ViewChild('addressForm') addressForm: FormGroupDirective;

private destroy$ = new Subject<void>();

constructor(private accountFacade: AccountFacade) {}
constructor(private accountFacade: AccountFacade, private featureToggleService: FeatureToggleService) {}

get buttonLabel() {
return Object.keys(this.address ?? {}).length > 0
Expand All @@ -65,7 +63,14 @@ export class FormlyCustomerAddressFormComponent implements OnInit, OnChanges, On
}

ngOnInit() {
this.businessCustomer$ = this.accountFacade.isBusinessCustomer$.pipe(takeUntil(this.destroy$));
this.businessCustomer$ = this.accountFacade.isBusinessCustomer$.pipe(
withLatestFrom(this.accountFacade.isLoggedIn$),
map(([isBusinessCustomer, isLoggedIn]) =>
isBusinessCustomer || isLoggedIn
? isBusinessCustomer
: this.featureToggleService.enabled('businessCustomerRegistration')
)
);

// create form for creating a new address
this.form = new FormGroup({
Expand Down Expand Up @@ -118,9 +123,4 @@ export class FormlyCustomerAddressFormComponent implements OnInit, OnChanges, On
cancelForm() {
this.cancel.emit();
}

ngOnDestroy() {
this.destroy$.next();
this.destroy$.complete();
}
}

0 comments on commit 23a9257

Please sign in to comment.