From c4f66e0d8177a2e5454d5036a5c0050c3272c484 Mon Sep 17 00:00:00 2001 From: kpawelczak <42094017+kpawelczak@users.noreply.github.com> Date: Thu, 6 Jul 2023 11:09:02 +0200 Subject: [PATCH] fix: console error when address fails to update (#17608) --- .../store/effects/user-addresses.effect.ts | 20 ++++++++------ .../address-form/address-form.component.ts | 27 +++++++++++-------- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/projects/core/src/user/store/effects/user-addresses.effect.ts b/projects/core/src/user/store/effects/user-addresses.effect.ts index 88edf4c795d..265081c052f 100644 --- a/projects/core/src/user/store/effects/user-addresses.effect.ts +++ b/projects/core/src/user/store/effects/user-addresses.effect.ts @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -import { Injectable, inject } from '@angular/core'; +import { inject, Injectable } from '@angular/core'; import { Actions, createEffect, ofType } from '@ngrx/effects'; import { Observable, of } from 'rxjs'; import { catchError, map, mergeMap, switchMap, tap } from 'rxjs/operators'; @@ -81,13 +81,17 @@ export class UserAddressesEffects { map(() => { return new UserActions.UpdateUserAddressSuccess(payload); }), - catchError((error) => - of( + catchError((error) => { + this.showGlobalMessage( + 'addressForm.invalidAddress', + GlobalMessageType.MSG_TYPE_ERROR + ); + return of( new UserActions.UpdateUserAddressFail( normalizeHttpError(error, this.logger) ) - ) - ) + ); + }) ); }) ) @@ -182,10 +186,10 @@ export class UserAddressesEffects { /** * Show global confirmation message with provided text */ - private showGlobalMessage(text: string) { + private showGlobalMessage(key: string, type?: GlobalMessageType) { this.messageService.add( - { key: text }, - GlobalMessageType.MSG_TYPE_CONFIRMATION + { key }, + type ?? GlobalMessageType.MSG_TYPE_CONFIRMATION ); } diff --git a/projects/storefrontlib/cms-components/myaccount/address-book/address-form/address-form.component.ts b/projects/storefrontlib/cms-components/myaccount/address-book/address-form/address-form.component.ts index fdcb4b25d3f..4d79a48c0c5 100644 --- a/projects/storefrontlib/cms-components/myaccount/address-book/address-form/address-form.component.ts +++ b/projects/storefrontlib/cms-components/myaccount/address-book/address-form/address-form.component.ts @@ -35,7 +35,7 @@ import { } from '@spartacus/core'; import { BehaviorSubject, combineLatest, Observable, Subscription } from 'rxjs'; import { filter, map, switchMap, take, tap } from 'rxjs/operators'; -import { LaunchDialogService, LAUNCH_CALLER } from '../../../../layout'; +import { LAUNCH_CALLER, LaunchDialogService } from '../../../../layout'; import { sortTitles } from '../../../../shared/utils/forms/title-utils'; @Component({ @@ -203,16 +203,21 @@ export class AddressFormComponent implements OnInit, OnDestroy { verifyAddress(): void { if (this.addressForm.valid) { - if (this.addressForm.get('region')?.value.isocode) { - this.regions$.pipe(take(1)).subscribe((regions) => { - const obj = regions.find( - (region) => - region.isocode === - this.addressForm.controls['region'].value.isocode - ); - Object.assign(this.addressForm.value.region, { - isocodeShort: obj?.isocodeShort, - }); + const regionControl = this.addressForm.get('region'); + const isocode = regionControl?.value?.isocode; + + if (isocode) { + this.regions$.pipe(take(1)).subscribe((regions: Region[]) => { + if (regions.length) { + const selectedRegion = regions.find( + (region: Region) => region.isocode === isocode + ); + regionControl?.patchValue({ + isocodeShort: selectedRegion?.isocodeShort, + }); + } else { + regionControl?.reset(); + } }); }