Skip to content

Commit

Permalink
fix: handle failing basket fetching for anonymous ICM apiToken withou…
Browse files Browse the repository at this point in the history
…t an assigned basket

* issue that occurs with the Hybrid Approach
* failing basket request needs to reset the loading state
  • Loading branch information
shauke committed Jun 16, 2023
1 parent a16501f commit a55751f
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 14 deletions.
6 changes: 0 additions & 6 deletions src/app/core/services/basket/basket.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,6 @@ describe('Basket Service', () => {
});
});

it('should not throw errors when getting a basket by token is unsuccessful', done => {
when(apiService.get(anything(), anything())).thenReturn(throwError(() => new Error()));

basketService.getBasketByToken('dummy').subscribe({ next: fail, error: fail, complete: done });
});

it("should create a basket data when 'createBasket' is called", done => {
when(apiService.post(anything(), anything(), anything())).thenReturn(of(basketMockData));

Expand Down
8 changes: 2 additions & 6 deletions src/app/core/services/basket/basket.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { HttpHeaders, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Store, select } from '@ngrx/store';
import { EMPTY, Observable, forkJoin, iif, of, throwError } from 'rxjs';
import { Observable, forkJoin, iif, of, throwError } from 'rxjs';
import { catchError, concatMap, first, map, take } from 'rxjs/operators';

import { AddressMapper } from 'ish-core/models/address/address.mapper';
Expand Down Expand Up @@ -213,12 +213,8 @@ export class BasketService {
.get<BasketData>(`baskets/current`, {
headers: this.basketHeaders.set(ApiService.TOKEN_HEADER_KEY, apiToken),
params,
skipApiErrorHandling: true,
})
.pipe(
map(BasketMapper.fromData),
catchError(() => EMPTY)
);
.pipe(map(BasketMapper.fromData));
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/app/core/store/customer/basket/basket.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export const loadBasketByAPIToken = createAction(
payload<{ apiToken: string }>()
);

export const loadBasketByAPITokenFail = createAction('[Basket API] Load Basket by API Token Fail', httpError());

export const loadBasketFail = createAction('[Basket API] Load Basket Fail', httpError());

export const loadBasketSuccess = createAction('[Basket API] Load Basket Success', payload<{ basket: Basket }>());
Expand Down
6 changes: 5 additions & 1 deletion src/app/core/store/customer/basket/basket.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
deleteBasketAttributeSuccess,
loadBasket,
loadBasketByAPIToken,
loadBasketByAPITokenFail,
loadBasketEligibleShippingMethods,
loadBasketEligibleShippingMethodsFail,
loadBasketEligibleShippingMethodsSuccess,
Expand Down Expand Up @@ -116,7 +117,10 @@ export class BasketEffects {
ofType(loadBasketByAPIToken),
mapToPayloadProperty('apiToken'),
concatMap(apiToken =>
this.basketService.getBasketByToken(apiToken).pipe(map(basket => loadBasketSuccess({ basket })))
this.basketService.getBasketByToken(apiToken).pipe(
map(basket => loadBasketSuccess({ basket })),
mapErrorToAction(loadBasketByAPITokenFail)
)
)
)
);
Expand Down
8 changes: 7 additions & 1 deletion src/app/core/store/customer/basket/basket.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
deleteBasketPaymentSuccess,
loadBasket,
loadBasketByAPIToken,
loadBasketByAPITokenFail,
loadBasketEligiblePaymentMethods,
loadBasketEligiblePaymentMethodsFail,
loadBasketEligiblePaymentMethodsSuccess,
Expand Down Expand Up @@ -149,7 +150,12 @@ export const basketReducer = createReducer(
mergeBasketInProgress,
setBasketDesiredDeliveryDate
),
unsetLoadingOn(addPromotionCodeToBasketSuccess, addPromotionCodeToBasketFail, loadBasketSuccess),
unsetLoadingOn(
addPromotionCodeToBasketSuccess,
addPromotionCodeToBasketFail,
loadBasketSuccess,
loadBasketByAPITokenFail
),
unsetLoadingAndErrorOn(
mergeBasketSuccess,
updateBasketItemSuccess,
Expand Down

0 comments on commit a55751f

Please sign in to comment.