Skip to content

Commit

Permalink
fix: remove personalized content from store
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian-Haehnlein committed Mar 19, 2020
1 parent c4ae7c7 commit 8da665e
Show file tree
Hide file tree
Showing 14 changed files with 159 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/app/core/store/content/content-store.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ActionReducerMap, StoreModule } from '@ngrx/store';
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 @@ -15,7 +16,7 @@ export const contentReducers: ActionReducerMap<ContentState> = {
pages: pagesReducer,
};

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

@NgModule({
imports: [EffectsModule.forFeature(contentEffects), StoreModule.forFeature('content', contentReducers)],
Expand Down
11 changes: 10 additions & 1 deletion src/app/core/store/content/includes/includes.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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 @@ -25,4 +26,12 @@ export class LoadContentIncludeSuccess implements Action {
constructor(public payload: { include: ContentPageletEntryPoint; pagelets: ContentPagelet[] }) {}
}

export type IncludesAction = LoadContentInclude | LoadContentIncludeFail | LoadContentIncludeSuccess;
export class ResetContentIncludes implements Action {
readonly type = IncludesActionTypes.ResetContentIncludes;
}

export type IncludesAction =
| LoadContentInclude
| LoadContentIncludeFail
| LoadContentIncludeSuccess
| ResetContentIncludes;
19 changes: 18 additions & 1 deletion src/app/core/store/content/includes/includes.effects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ 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/user';

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

describe('Includes Effects', () => {
Expand Down Expand Up @@ -74,4 +80,15 @@ 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: 8 additions & 1 deletion src/app/core/store/content/includes/includes.effects.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Injectable } from '@angular/core';
import { Actions, Effect, ofType } from '@ngrx/effects';
import { identity } from 'rxjs';
import { groupBy, map, mergeMap, switchMap } from 'rxjs/operators';
import { groupBy, map, mapTo, mergeMap, switchMap } from 'rxjs/operators';

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

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

@Effect()
resetContentIncludesAfterLogout$ = this.actions$.pipe(
ofType(UserActionTypes.LogoutUser),
mapTo(new includesActions.ResetContentIncludes())
);
}
7 changes: 7 additions & 0 deletions src/app/core/store/content/includes/includes.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ 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: 1 addition & 0 deletions src/app/core/store/content/pagelets/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// tslint:disable no-barrel-files
// API to access ngrx pagelets state
export * from './pagelets.actions';
export * from './pagelets.selectors';
11 changes: 11 additions & 0 deletions src/app/core/store/content/pagelets/pagelets.actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Action } from '@ngrx/store';

export enum PageletsActionTypes {
ResetPagelets = '[Pagelets] Reset Pagelets',
}

export class ResetPagelets implements Action {
readonly type = PageletsActionTypes.ResetPagelets;
}

export type PageletsAction = ResetPagelets;
42 changes: 42 additions & 0 deletions src/app/core/store/content/pagelets/pagelets.effects.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { TestBed } from '@angular/core/testing';
import { provideMockActions } from '@ngrx/effects/testing';
import { Action } from '@ngrx/store';
import { cold, hot } from 'jest-marbles';
import { Observable } from 'rxjs';
import { instance, mock } from 'ts-mockito';

import { CMSService } from 'ish-core/services/cms/cms.service';
import { LogoutUser } from 'ish-core/store/user';

import { ResetPagelets } from './pagelets.actions';
import { PageletsEffects } from './pagelets.effects';

describe('Pagelets Effects', () => {
let actions$: Observable<Action>;
let effects: PageletsEffects;
let cmsServiceMock: CMSService;

beforeEach(() => {
cmsServiceMock = mock(CMSService);

TestBed.configureTestingModule({
providers: [
PageletsEffects,
provideMockActions(() => actions$),
{ provide: CMSService, useFactory: () => instance(cmsServiceMock) },
],
});
effects = TestBed.get(PageletsEffects);
});

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

expect(effects.resetPageletsAfterLogout$).toBeObservable(expected$);
});
});
});
18 changes: 18 additions & 0 deletions src/app/core/store/content/pagelets/pagelets.effects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Injectable } from '@angular/core';
import { Actions, Effect, ofType } from '@ngrx/effects';
import { mapTo } from 'rxjs/operators';

import { UserActionTypes } from 'ish-core/store/user';

import * as pageletsActions from './pagelets.actions';

@Injectable()
export class PageletsEffects {
constructor(private actions$: Actions) {}

@Effect()
resetPageletsAfterLogout$ = this.actions$.pipe(
ofType(UserActionTypes.LogoutUser),
mapTo(new pageletsActions.ResetPagelets())
);
}
3 changes: 3 additions & 0 deletions src/app/core/store/content/pagelets/pagelets.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export function pageletsReducer(state = initialState, action: IncludesAction | P
case PagesActionTypes.LoadContentPageSuccess: {
return pageletsAdapter.upsertMany(action.payload.pagelets, state);
}
case IncludesActionTypes.ResetContentIncludes: {
return pageletsAdapter.removeAll(state);
}
}

return state;
Expand Down
12 changes: 11 additions & 1 deletion src/app/core/store/content/pages/pages.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ 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 SelectContentPage implements Action {
Expand All @@ -30,4 +31,13 @@ export class LoadContentPageSuccess implements Action {
constructor(public payload: { page: ContentPageletEntryPoint; pagelets: ContentPagelet[] }) {}
}

export type PageAction = SelectContentPage | LoadContentPage | LoadContentPageFail | LoadContentPageSuccess;
export class ResetContentPages implements Action {
readonly type = PagesActionTypes.ResetContentPages;
}

export type PageAction =
| SelectContentPage
| LoadContentPage
| LoadContentPageFail
| LoadContentPageSuccess
| ResetContentPages;
14 changes: 13 additions & 1 deletion src/app/core/store/content/pages/pages.effects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ 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 { LogoutUser } from 'ish-core/store/user';
import { ngrxTesting } from 'ish-core/utils/dev/ngrx-testing';

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

describe('Pages Effects', () => {
Expand Down Expand Up @@ -58,4 +59,15 @@ 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$);
});
});
});
9 changes: 8 additions & 1 deletion src/app/core/store/content/pages/pages.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { Injectable } from '@angular/core';
import { Actions, Effect, ofType } from '@ngrx/effects';
import { Store, select } from '@ngrx/store';
import { mapToParam, ofRoute } from 'ngrx-router';
import { filter, map, mergeMap, withLatestFrom } from 'rxjs/operators';
import { filter, map, mapTo, mergeMap, withLatestFrom } from 'rxjs/operators';

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

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

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

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

return state;
Expand Down

0 comments on commit 8da665e

Please sign in to comment.