Skip to content

Commit

Permalink
fix: console error when address fails to update (#17608)
Browse files Browse the repository at this point in the history
  • Loading branch information
kpawelczak authored Jul 6, 2023
1 parent 64a7612 commit c4f66e0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
20 changes: 12 additions & 8 deletions projects/core/src/user/store/effects/user-addresses.effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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)
)
)
)
);
})
);
})
)
Expand Down Expand Up @@ -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
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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();
}
});
}

Expand Down

0 comments on commit c4f66e0

Please sign in to comment.