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 15, 2020
1 parent 861a99b commit b2ec14a
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 { getCurrentLocale, getICMServerURL, getRestEndpoint } from 'ish-core/store/core/configuration';
import { getAPIToken, getPGID } from 'ish-core/store/customer/user';
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('_customer'), select(getAPIToken)).subscribe(token => (this.apiToken = token));
store.pipe(waitForFeatureStore('_customer'), select(getPGID)).subscribe(pgid => (this.pgid = pgid));
}

/**
Expand Down
14 changes: 10 additions & 4 deletions src/app/core/store/content/content-store.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ 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 { ContentState } from './content-store';
import { IncludesEffects } from './includes/includes.effects';
import { includesReducer } from './includes/includes.reducer';
import { PageletsEffects } from './pagelets/pagelets.effects';
import { pageletsReducer } from './pagelets/pagelets.reducer';
import { PagesEffects } from './pages/pages.effects';
import { pagesReducer } from './pages/pages.reducer';
Expand All @@ -19,13 +20,18 @@ export const contentReducers: ActionReducerMap<ContentState> = {
};
// tslint:disable: deprecation

const contentEffects = [IncludesEffects, PageletsEffects, PagesEffects];
const contentEffects = [IncludesEffects, PagesEffects];

const metaReducers = [resetOnLogoutMeta];

@NgModule({
imports: [EffectsModule.forFeature(contentEffects), StoreModule.forFeature('content', contentReducers)],
imports: [
EffectsModule.forFeature(contentEffects),
StoreModule.forFeature('content', contentReducers, { metaReducers }),
],
})
export class ContentStoreModule {
static forTesting(...reducers: (keyof ActionReducerMap<ContentState>)[]) {
return StoreModule.forFeature('content', pick(contentReducers, reducers));
return StoreModule.forFeature('content', pick(contentReducers, reducers), { metaReducers });
}
}
11 changes: 1 addition & 10 deletions src/app/core/store/content/includes/includes.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export enum IncludesActionTypes {
LoadContentInclude = '[Content Include] Load Content Include',
LoadContentIncludeFail = '[Content Include API] Load Content Include Fail',
LoadContentIncludeSuccess = '[Content Include API] Load Content Include Success',
ResetContentIncludes = '[Content Include] Reset Content Includes',
}

export class LoadContentInclude implements Action {
Expand All @@ -26,12 +25,4 @@ export class LoadContentIncludeSuccess implements Action {
constructor(public payload: { include: ContentPageletEntryPoint; pagelets: ContentPagelet[] }) {}
}

export class ResetContentIncludes implements Action {
readonly type = IncludesActionTypes.ResetContentIncludes;
}

export type IncludesAction =
| LoadContentInclude
| LoadContentIncludeFail
| LoadContentIncludeSuccess
| ResetContentIncludes;
export type IncludesAction = LoadContentInclude | LoadContentIncludeFail | LoadContentIncludeSuccess;
19 changes: 1 addition & 18 deletions src/app/core/store/content/includes/includes.effects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,8 @@ import { instance, mock, verify, when } from 'ts-mockito';
import { ContentPageletEntryPoint } from 'ish-core/models/content-pagelet-entry-point/content-pagelet-entry-point.model';
import { HttpError } from 'ish-core/models/http-error/http-error.model';
import { CMSService } from 'ish-core/services/cms/cms.service';
import { LogoutUser } from 'ish-core/store/customer/user';

import {
LoadContentInclude,
LoadContentIncludeFail,
LoadContentIncludeSuccess,
ResetContentIncludes,
} from './includes.actions';
import { LoadContentInclude, LoadContentIncludeFail, LoadContentIncludeSuccess } from './includes.actions';
import { IncludesEffects } from './includes.effects';

describe('Includes Effects', () => {
Expand Down Expand Up @@ -80,15 +74,4 @@ describe('Includes Effects', () => {
);
});
});

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

expect(effects.resetContentIncludesAfterLogout$).toBeObservable(expected$);
});
});
});
9 changes: 1 addition & 8 deletions src/app/core/store/content/includes/includes.effects.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Injectable } from '@angular/core';
import { Actions, Effect, ofType } from '@ngrx/effects';
import { identity } from 'rxjs';
import { groupBy, map, mapTo, mergeMap, switchMap } from 'rxjs/operators';
import { groupBy, map, mergeMap, switchMap } from 'rxjs/operators';

import { CMSService } from 'ish-core/services/cms/cms.service';
import { UserActionTypes } from 'ish-core/store/customer/user';
import { mapErrorToAction, mapToPayloadProperty } from 'ish-core/utils/operators';

import * as includesActions from './includes.actions';
Expand All @@ -29,10 +28,4 @@ export class IncludesEffects {
)
)
);

@Effect()
resetContentIncludesAfterLogout$ = this.actions$.pipe(
ofType(UserActionTypes.LogoutUser),
mapTo(new includesActions.ResetContentIncludes())
);
}
7 changes: 0 additions & 7 deletions src/app/core/store/content/includes/includes.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,6 @@ export function includesReducer(state = initialState, action: IncludesAction): I
loading: false,
};
}

case IncludesActionTypes.ResetContentIncludes: {
return {
...includesAdapter.removeAll(state),
loading: false,
};
}
}

return state;
Expand Down
1 change: 0 additions & 1 deletion src/app/core/store/content/pagelets/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// tslint:disable no-barrel-files
// API to access ngrx pagelets state
export * from './pagelets.actions';
export * from './pagelets.selectors';
11 changes: 0 additions & 11 deletions src/app/core/store/content/pagelets/pagelets.actions.ts

This file was deleted.

42 changes: 0 additions & 42 deletions src/app/core/store/content/pagelets/pagelets.effects.spec.ts

This file was deleted.

18 changes: 0 additions & 18 deletions src/app/core/store/content/pagelets/pagelets.effects.ts

This file was deleted.

7 changes: 1 addition & 6 deletions src/app/core/store/content/pagelets/pagelets.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,20 @@ import { ContentPagelet } from 'ish-core/models/content-pagelet/content-pagelet.
import { IncludesAction, IncludesActionTypes } from 'ish-core/store/content/includes/includes.actions';
import { PageAction, PagesActionTypes } from 'ish-core/store/content/pages/pages.actions';

import { PageletsAction, PageletsActionTypes } from './pagelets.actions';

export interface PageletsState extends EntityState<ContentPagelet> {}

export const pageletsAdapter = createEntityAdapter<ContentPagelet>();

const initialState = pageletsAdapter.getInitialState();

export function pageletsReducer(state = initialState, action: IncludesAction | PageAction | PageletsAction) {
export function pageletsReducer(state = initialState, action: IncludesAction | PageAction) {
switch (action.type) {
case IncludesActionTypes.LoadContentIncludeSuccess: {
return pageletsAdapter.upsertMany(action.payload.pagelets, state);
}
case PagesActionTypes.LoadContentPageSuccess: {
return pageletsAdapter.upsertMany(action.payload.pagelets, state);
}
case PageletsActionTypes.ResetPagelets: {
return pageletsAdapter.removeAll(state);
}
}

return state;
Expand Down
7 changes: 1 addition & 6 deletions src/app/core/store/content/pages/pages.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export enum PagesActionTypes {
LoadContentPage = '[Content Page] Load Content Page',
LoadContentPageFail = '[Content Page API] Load Content Page Fail',
LoadContentPageSuccess = '[Content Page API] Load Content Page Success',
ResetContentPages = '[Content Page] Reset Content Pages',
}

export class LoadContentPage implements Action {
Expand All @@ -26,8 +25,4 @@ export class LoadContentPageSuccess implements Action {
constructor(public payload: { page: ContentPageletEntryPoint; pagelets: ContentPagelet[] }) {}
}

export class ResetContentPages implements Action {
readonly type = PagesActionTypes.ResetContentPages;
}

export type PageAction = LoadContentPage | LoadContentPageFail | LoadContentPageSuccess | ResetContentPages;
export type PageAction = LoadContentPage | LoadContentPageFail | LoadContentPageSuccess;
14 changes: 1 addition & 13 deletions src/app/core/store/content/pages/pages.effects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ import { instance, mock, verify, when } from 'ts-mockito';
import { HttpError } from 'ish-core/models/http-error/http-error.model';
import { CMSService } from 'ish-core/services/cms/cms.service';
import { CoreStoreModule } from 'ish-core/store/core/core-store.module';
import { LogoutUser } from 'ish-core/store/customer/user';

import { LoadContentPage, LoadContentPageFail, ResetContentPages } from './pages.actions';
import { LoadContentPage, LoadContentPageFail } from './pages.actions';
import { PagesEffects } from './pages.effects';

describe('Pages Effects', () => {
Expand Down Expand Up @@ -71,17 +70,6 @@ describe('Pages Effects', () => {
});
});

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

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

describe('selectedContentPage$', () => {
it('should select the route when url parameter is available', done => {
effects.selectedContentPage$.subscribe(action => {
Expand Down
9 changes: 1 addition & 8 deletions src/app/core/store/content/pages/pages.effects.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { Injectable } from '@angular/core';
import { Actions, Effect, ofType } from '@ngrx/effects';
import { Store, select } from '@ngrx/store';
import { map, mapTo, mergeMap } from 'rxjs/operators';
import { map, mergeMap } from 'rxjs/operators';

import { CMSService } from 'ish-core/services/cms/cms.service';
import { selectRouteParam } from 'ish-core/store/core/router';
import { UserActionTypes } from 'ish-core/store/customer/user';
import { mapErrorToAction, mapToPayloadProperty, whenTruthy } from 'ish-core/utils/operators';

import * as pagesActions from './pages.actions';
Expand All @@ -32,10 +31,4 @@ export class PagesEffects {
whenTruthy(),
map(contentPageId => new pagesActions.LoadContentPage({ contentPageId }))
);

@Effect()
resetContentPagesAfterLogout$ = this.actions$.pipe(
ofType(UserActionTypes.LogoutUser),
mapTo(new pagesActions.ResetContentPages())
);
}
7 changes: 0 additions & 7 deletions src/app/core/store/content/pages/pages.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,6 @@ export function pagesReducer(state = initialState, action: PageAction): PagesSta
loading: false,
};
}

case PagesActionTypes.ResetContentPages: {
return {
...pagesAdapter.removeAll(state),
loading: false,
};
}
}

return state;
Expand Down
7 changes: 1 addition & 6 deletions src/app/core/store/customer/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/customer/addresses/addresses.effects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { AddressService } from 'ish-core/services/address/address.service';
import { CoreStoreModule } from 'ish-core/store/core/core-store.module';
import { SuccessMessage } from 'ish-core/store/core/messages';
import { CustomerStoreModule } from 'ish-core/store/customer/customer-store.module';
import { LoginUserSuccess, LogoutUser } from 'ish-core/store/customer/user';
import { LoginUserSuccess } from 'ish-core/store/customer/user';

import * as addressesActions from './addresses.actions';
import { AddressesEffects } from './addresses.effects';
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$);
});
});
});
Loading

0 comments on commit b2ec14a

Please sign in to comment.