Skip to content

Commit

Permalink
fix: reimplement tracking of anonymous basket for merge basket logic (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
dhhyi committed Apr 19, 2022
1 parent 59238dc commit b9ff6cb
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions src/app/core/store/customer/basket/basket.effects.ts
Expand Up @@ -4,17 +4,27 @@ import { Router } from '@angular/router';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { routerNavigatedAction } from '@ngrx/router-store';
import { Store, select } from '@ngrx/store';
import { EMPTY, combineLatest, from, iif, of } from 'rxjs';
import { concatMap, filter, map, mergeMap, sample, startWith, switchMap, take, withLatestFrom } from 'rxjs/operators';
import { EMPTY, from, iif, of } from 'rxjs';
import {
concatMap,
filter,
map,
mergeMap,
shareReplay,
startWith,
switchMap,
take,
withLatestFrom,
} from 'rxjs/operators';

import { Basket } from 'ish-core/models/basket/basket.model';
import { BasketService } from 'ish-core/services/basket/basket.service';
import { getCurrentCurrency } from 'ish-core/store/core/configuration';
import { mapToRouterState } from 'ish-core/store/core/router';
import { resetOrderErrors } from 'ish-core/store/customer/orders';
import { createUser, loadUserByAPIToken, loginUser, loginUserSuccess } from 'ish-core/store/customer/user';
import { getLoggedInCustomer, loginUserSuccess } from 'ish-core/store/customer/user';
import { ApiTokenService } from 'ish-core/utils/api-token/api-token.service';
import { mapErrorToAction, mapToPayloadProperty } from 'ish-core/utils/operators';
import { mapErrorToAction, mapToPayloadProperty, mapToProperty } from 'ish-core/utils/operators';

import {
createBasket,
Expand Down Expand Up @@ -232,9 +242,16 @@ export class BasketEffects {
*/
private anonymousBasket$ = createEffect(
() =>
combineLatest([this.store.pipe(select(getCurrentBasketId)), this.apiTokenService.apiToken$]).pipe(
sample(this.actions$.pipe(ofType(loginUser, createUser, loadUserByAPIToken))),
startWith([undefined, undefined])
this.store.pipe(
// track basket changes
select(getCurrentBasket),
mapToProperty('id'),
// append corresponding apiToken and customer
withLatestFrom(this.apiTokenService.apiToken$, this.store.pipe(select(getLoggedInCustomer))),
// don't emit when there is a customer
filter(([, , customer]) => !customer),
startWith([]),
shareReplay(1)
),
{ dispatch: false }
);
Expand Down

0 comments on commit b9ff6cb

Please sign in to comment.