Skip to content

Commit

Permalink
refactor: use meta reducer to reset user related states when user log…
Browse files Browse the repository at this point in the history
…s out
  • Loading branch information
dhhyi committed Jun 8, 2020
1 parent eaf7c8d commit 25ace4f
Show file tree
Hide file tree
Showing 52 changed files with 152 additions and 407 deletions.
5 changes: 3 additions & 2 deletions src/app/core/services/api/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Link } from 'ish-core/models/link/link.model';
import { Locale } from 'ish-core/models/locale/locale.model';
import { getAPIToken, getPGID } from 'ish-core/store/account/user';
import { getCurrentLocale, getICMServerURL, getRestEndpoint } from 'ish-core/store/core/configuration';
import { waitForFeatureStore } from 'ish-core/utils/operators';

import { ApiServiceErrorHandler } from './api.service.errorhandler';

Expand Down Expand Up @@ -126,8 +127,8 @@ export class ApiService {
store.pipe(select(getCurrentLocale)).subscribe(locale => (this.currentLocale = locale));
store.pipe(select(getICMServerURL)).subscribe(url => (this.icmServerURL = url));
store.pipe(select(getRestEndpoint)).subscribe(url => (this.restEndpoint = url));
store.pipe(select(getAPIToken)).subscribe(token => (this.apiToken = token));
store.pipe(select(getPGID)).subscribe(pgid => (this.pgid = pgid));
store.pipe(waitForFeatureStore('_account'), select(getAPIToken)).subscribe(token => (this.apiToken = token));
store.pipe(waitForFeatureStore('_account'), select(getPGID)).subscribe(pgid => (this.pgid = pgid));
}

/**
Expand Down
11 changes: 9 additions & 2 deletions src/app/core/store/account/account-store.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { EffectsModule } from '@ngrx/effects';
import { ActionReducerMap, StoreModule } from '@ngrx/store';
import { pick } from 'lodash-es';

import { resetOnLogoutMeta } from 'ish-core/utils/meta-reducers';

import { AccountState } from './account-store';
import { AddressesEffects } from './addresses/addresses.effects';
import { addressesReducer } from './addresses/addresses.reducer';
Expand Down Expand Up @@ -39,11 +41,16 @@ const accountEffects = [
UserEffects,
];

const metaReducers = [resetOnLogoutMeta];

@NgModule({
imports: [EffectsModule.forFeature(accountEffects), StoreModule.forFeature('_account', accountReducers)],
imports: [
EffectsModule.forFeature(accountEffects),
StoreModule.forFeature('_account', accountReducers, { metaReducers }),
],
})
export class AccountStoreModule {
static forTesting(...reducers: (keyof ActionReducerMap<AccountState>)[]) {
return StoreModule.forFeature('_account', pick(accountReducers, reducers));
return StoreModule.forFeature('_account', pick(accountReducers, reducers), { metaReducers });
}
}
7 changes: 1 addition & 6 deletions src/app/core/store/account/addresses/addresses.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export enum AddressActionTypes {
DeleteCustomerAddress = '[Address] Delete Customer Address',
DeleteCustomerAddressFail = '[Address API] Delete Customer Address Fail',
DeleteCustomerAddressSuccess = '[Address API] Delete Customer Address Success',
ResetAddresses = '[Address Internal] Reset Addresses',
}

export class LoadAddresses implements Action {
Expand Down Expand Up @@ -71,9 +70,6 @@ export class DeleteCustomerAddressSuccess implements Action {
readonly type = AddressActionTypes.DeleteCustomerAddressSuccess;
constructor(public payload: { addressId: string }) {}
}
export class ResetAddresses implements Action {
readonly type = AddressActionTypes.ResetAddresses;
}

export type AddressAction =
| LoadAddresses
Expand All @@ -86,5 +82,4 @@ export type AddressAction =
| UpdateCustomerAddressSuccess
| DeleteCustomerAddress
| DeleteCustomerAddressFail
| DeleteCustomerAddressSuccess
| ResetAddresses;
| DeleteCustomerAddressSuccess;
13 changes: 1 addition & 12 deletions src/app/core/store/account/addresses/addresses.effects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Customer } from 'ish-core/models/customer/customer.model';
import { HttpError } from 'ish-core/models/http-error/http-error.model';
import { AddressService } from 'ish-core/services/address/address.service';
import { AccountStoreModule } from 'ish-core/store/account/account-store.module';
import { LoginUserSuccess, LogoutUser } from 'ish-core/store/account/user';
import { LoginUserSuccess } from 'ish-core/store/account/user';
import { CoreStoreModule } from 'ish-core/store/core/core-store.module';
import { SuccessMessage } from 'ish-core/store/core/messages';

Expand Down Expand Up @@ -146,15 +146,4 @@ describe('Addresses Effects', () => {
expect(effects.deleteCustomerAddress$).toBeObservable(expected$);
});
});

describe('resetAddressesAfterLogout$', () => {
it('should map to action of type ResetAddresses if LogoutUser action triggered', () => {
const action = new LogoutUser();
const completion = new addressesActions.ResetAddresses();
actions$ = hot('-a-a-a', { a: action });
const expected$ = cold('-c-c-c', { c: completion });

expect(effects.resetAddressesAfterLogout$).toBeObservable(expected$);
});
});
});
13 changes: 2 additions & 11 deletions src/app/core/store/account/addresses/addresses.effects.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Injectable } from '@angular/core';
import { Actions, Effect, ofType } from '@ngrx/effects';
import { Store, select } from '@ngrx/store';
import { concatMap, filter, map, mapTo, mergeMap, switchMap, withLatestFrom } from 'rxjs/operators';
import { concatMap, filter, map, mergeMap, switchMap, withLatestFrom } from 'rxjs/operators';

import { AddressService } from 'ish-core/services/address/address.service';
import { UserActionTypes, getLoggedInCustomer } from 'ish-core/store/account/user';
import { getLoggedInCustomer } from 'ish-core/store/account/user';
import { SuccessMessage } from 'ish-core/store/core/messages';
import { mapErrorToAction, mapToPayloadProperty } from 'ish-core/utils/operators';

Expand Down Expand Up @@ -67,13 +67,4 @@ export class AddressesEffects {
)
)
);

/**
* Trigger ResetAddresses action after LogoutUser.
*/
@Effect()
resetAddressesAfterLogout$ = this.actions$.pipe(
ofType(UserActionTypes.LogoutUser),
mapTo(new addressActions.ResetAddresses())
);
}
14 changes: 0 additions & 14 deletions src/app/core/store/account/addresses/addresses.reducer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,18 +201,4 @@ describe('Addresses Reducer', () => {
});
});
});

describe('ResetAddresses action', () => {
it('should reset to initial state', () => {
const oldState = {
...initialState,
loading: true,
addresses: [{ ids: ['test'] }],
};
const action = new fromActions.ResetAddresses();
const state = addressesReducer(oldState, action);

expect(state).toEqual(initialState);
});
});
});
4 changes: 0 additions & 4 deletions src/app/core/store/account/addresses/addresses.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,6 @@ export function addressesReducer(state = initialState, action: AddressAction | B
error: undefined,
};
}

case AddressActionTypes.ResetAddresses: {
return initialState;
}
}

return state;
Expand Down
6 changes: 0 additions & 6 deletions src/app/core/store/account/basket/basket.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ export enum BasketActionTypes {
DeleteBasketPayment = '[Basket] Delete Basket Payment',
DeleteBasketPaymentFail = '[Basket API] Delete Basket Payment Fail',
DeleteBasketPaymentSuccess = '[Basket API] Delete Basket Payment Success',
ResetBasket = '[Basket Internal] Reset Basket',
ResetBasketErrors = '[Basket Internal] Reset Basket and Basket Promotion Errors',
}

Expand Down Expand Up @@ -332,10 +331,6 @@ export class DeleteBasketPaymentSuccess implements Action {
readonly type = BasketActionTypes.DeleteBasketPaymentSuccess;
}

export class ResetBasket implements Action {
readonly type = BasketActionTypes.ResetBasket;
}

export class ResetBasketErrors implements Action {
readonly type = BasketActionTypes.ResetBasketErrors;
}
Expand Down Expand Up @@ -395,5 +390,4 @@ export type BasketAction =
| DeleteBasketPayment
| DeleteBasketPaymentFail
| DeleteBasketPaymentSuccess
| ResetBasket
| ResetBasketErrors;
13 changes: 1 addition & 12 deletions src/app/core/store/account/basket/basket.effects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { LineItem } from 'ish-core/models/line-item/line-item.model';
import { Product, ProductCompletenessLevel } from 'ish-core/models/product/product.model';
import { BasketService } from 'ish-core/services/basket/basket.service';
import { AccountStoreModule } from 'ish-core/store/account/account-store.module';
import { LoginUser, LoginUserSuccess, LogoutUser, SetAPIToken } from 'ish-core/store/account/user';
import { LoginUser, LoginUserSuccess, SetAPIToken } from 'ish-core/store/account/user';
import { CoreStoreModule } from 'ish-core/store/core/core-store.module';
import { LoadProductIfNotLoaded, LoadProductSuccess } from 'ish-core/store/shopping/products';
import { ShoppingStoreModule } from 'ish-core/store/shopping/shopping-store.module';
Expand Down Expand Up @@ -410,17 +410,6 @@ describe('Basket Effects', () => {
});
});

describe('resetBasketAfterLogout$', () => {
it('should map to action of type ResetBasket if LogoutUser action triggered', () => {
const action = new LogoutUser();
const completion = new basketActions.ResetBasket();
actions$ = hot('-a-a-a', { a: action });
const expected$ = cold('-c-c-c', { c: completion });

expect(effects.resetBasketAfterLogout$).toBeObservable(expected$);
});
});

describe('routeListenerForResettingBasketErrors$', () => {
it('should fire ResetBasketErrors when route basket or checkout/* is navigated', done => {
router.navigateByUrl('/checkout/payment');
Expand Down
10 changes: 0 additions & 10 deletions src/app/core/store/account/basket/basket.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,6 @@ export class BasketEffects {
mapTo(new basketActions.LoadBasket())
);

/**
* Trigger ResetBasket action after LogoutUser.
*/
@Effect()
resetBasketAfterLogout$ = this.actions$.pipe(
ofType(UserActionTypes.LogoutUser),

mapTo(new basketActions.ResetBasket())
);

/**
* Trigger ResetBasketErrors after the user navigated to another basket/checkout route
* Add queryParam error=true to the route to prevent resetting errors.
Expand Down
14 changes: 0 additions & 14 deletions src/app/core/store/account/basket/basket.reducer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,20 +341,6 @@ describe('Basket Reducer', () => {
});
});
});

describe('ResetBasket action', () => {
it('should reset to initial state', () => {
const oldState = {
...initialState,
loading: true,
lineItems: [{ id: 'test' } as LineItem],
};
const action = new fromActions.ResetBasket();
const state = basketReducer(oldState, action);

expect(state).toEqual(initialState);
});
});
});

describe('LoadBasketEligibleShippingMethods actions', () => {
Expand Down
1 change: 0 additions & 1 deletion src/app/core/store/account/basket/basket.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ export function basketReducer(state = initialState, action: BasketAction | Order
};
}

case BasketActionTypes.ResetBasket:
case OrdersActionTypes.CreateOrderSuccess: {
return initialState;
}
Expand Down
4 changes: 1 addition & 3 deletions src/app/core/store/account/basket/basket.selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import { getLoggedInCustomer } from 'ish-core/store/account/user';
import { getCategoryTree } from 'ish-core/store/shopping/categories';
import { getProductEntities } from 'ish-core/store/shopping/products';

import { initialState } from './basket.reducer';

const getBasketState = createSelector(getAccountState, state => (state && state.basket) || initialState);
const getBasketState = createSelector(getAccountState, state => state.basket);

export const getBasketValidationResults = createSelector(
getBasketState,
Expand Down
8 changes: 1 addition & 7 deletions src/app/core/store/account/orders/orders.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export enum OrdersActionTypes {
LoadOrderSuccess = '[Order API] Load Order Success',
SelectOrderAfterRedirect = '[Order Internal] Select Order After Checkout Redirect',
SelectOrderAfterRedirectFail = '[Order API] Select Order Fail After Checkout Redirect',
ResetOrders = '[Order API] Reset Orders',
}

export class CreateOrder implements Action {
Expand Down Expand Up @@ -85,10 +84,6 @@ export class SelectOrderAfterRedirectFail implements Action {
constructor(public payload: { error: HttpError }) {}
}

export class ResetOrders implements Action {
readonly type = OrdersActionTypes.ResetOrders;
}

export type OrdersAction =
| CreateOrder
| CreateOrderFail
Expand All @@ -102,5 +97,4 @@ export type OrdersAction =
| LoadOrderSuccess
| SelectOrder
| SelectOrderAfterRedirect
| SelectOrderAfterRedirectFail
| ResetOrders;
| SelectOrderAfterRedirectFail;
13 changes: 1 addition & 12 deletions src/app/core/store/account/orders/orders.effects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { User } from 'ish-core/models/user/user.model';
import { OrderService } from 'ish-core/services/order/order.service';
import { AccountStoreModule } from 'ish-core/store/account/account-store.module';
import { ContinueCheckoutWithIssues, LoadBasket } from 'ish-core/store/account/basket';
import { LoginUserSuccess, LogoutUser } from 'ish-core/store/account/user';
import { LoginUserSuccess } from 'ish-core/store/account/user';
import { CoreStoreModule } from 'ish-core/store/core/core-store.module';
import { ShoppingStoreModule } from 'ish-core/store/shopping/shopping-store.module';
import { BasketMockData } from 'ish-core/utils/dev/basket-mock-data';
Expand Down Expand Up @@ -431,17 +431,6 @@ describe('Orders Effects', () => {
});
});

describe('resetOrdersAfterLogout$', () => {
it('should map to action of type ResetOrders if LogoutUser action triggered', () => {
const action = new LogoutUser();
const completion = new orderActions.ResetOrders();
actions$ = hot('-a-a-a', { a: action });
const expected$ = cold('-c-c-c', { c: completion });

expect(effects.resetOrdersAfterLogout$).toBeObservable(expected$);
});
});

describe('setOrderBreadcrumb$', () => {
beforeEach(() => {
store$.dispatch(new orderActions.LoadOrdersSuccess({ orders }));
Expand Down
11 changes: 1 addition & 10 deletions src/app/core/store/account/orders/orders.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
import { ProductCompletenessLevel } from 'ish-core/models/product/product.model';
import { OrderService } from 'ish-core/services/order/order.service';
import { ContinueCheckoutWithIssues, LoadBasket } from 'ish-core/store/account/basket';
import { UserActionTypes, getLoggedInUser } from 'ish-core/store/account/user';
import { getLoggedInUser } from 'ish-core/store/account/user';
import { ofUrl, selectQueryParams, selectRouteParam } from 'ish-core/store/core/router';
import { SetBreadcrumbData } from 'ish-core/store/core/viewconf';
import { LoadProductIfNotLoaded } from 'ish-core/store/shopping/products';
Expand Down Expand Up @@ -225,15 +225,6 @@ export class OrdersEffects {
mapTo(new LoadBasket())
);

/**
* Trigger ResetOrders action after LogoutUser.
*/
@Effect()
resetOrdersAfterLogout$ = this.actions$.pipe(
ofType(UserActionTypes.LogoutUser),
mapTo(new ordersActions.ResetOrders())
);

@Effect()
setOrderBreadcrumb$ = this.store.pipe(
select(getSelectedOrder),
Expand Down
14 changes: 0 additions & 14 deletions src/app/core/store/account/orders/orders.reducer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,4 @@ describe('Orders Reducer', () => {
expect(state.selected).toEqual(order.id);
});
});

describe('ResetOrders action', () => {
it('should reset to initial state', () => {
const oldState = {
...initialState,
loading: true,
orders: [{ ids: ['test'] }],
};
const action = new fromActions.ResetOrders();
const state = ordersReducer(oldState, action);

expect(state).toEqual(initialState);
});
});
});
4 changes: 0 additions & 4 deletions src/app/core/store/account/orders/orders.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,6 @@ export function ordersReducer(state = initialState, action: OrdersAction): Order
loading: false,
};
}

case OrdersActionTypes.ResetOrders: {
return initialState;
}
}

return state;
Expand Down
6 changes: 3 additions & 3 deletions src/app/core/store/account/restore/restore.effects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Order } from 'ish-core/models/order/order.model';
import { User } from 'ish-core/models/user/user.model';
import { CookiesService } from 'ish-core/services/cookies/cookies.service';
import { AccountStoreModule } from 'ish-core/store/account/account-store.module';
import { BasketActionTypes, LoadBasketSuccess, ResetBasket } from 'ish-core/store/account/basket';
import { BasketActionTypes, LoadBasketSuccess } from 'ish-core/store/account/basket';
import { LoadOrderSuccess, OrdersActionTypes } from 'ish-core/store/account/orders';
import { LoginUserSuccess, LogoutUser, SetAPIToken, UserActionTypes } from 'ish-core/store/account/user';
import { CoreStoreModule } from 'ish-core/store/core/core-store.module';
Expand Down Expand Up @@ -201,7 +201,7 @@ describe('Restore Effects', () => {

restoreEffects.removeAnonymousBasketIfTokenVanishes$.subscribe({
next: action => {
expect(action.type).toEqual(BasketActionTypes.ResetBasket);
expect(action.type).toEqual(UserActionTypes.LogoutUser);
done();
},
complete: fail,
Expand Down Expand Up @@ -242,7 +242,7 @@ describe('Restore Effects', () => {

jest.advanceTimersByTime(RestoreEffects.SESSION_KEEP_ALIVE / 2);

store$.dispatch(new ResetBasket());
store$.dispatch(new LogoutUser());

jest.advanceTimersByTime(RestoreEffects.SESSION_KEEP_ALIVE + 100);

Expand Down
Loading

0 comments on commit 25ace4f

Please sign in to comment.