-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: render view context content (#4)
- introduce View Context service, state management, facade and rendering component - add 'categoryRef' to category data
- Loading branch information
Showing
21 changed files
with
444 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface CallParameters { | ||
[key: string]: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// tslint:disable no-barrel-files | ||
// API to access ngrx viewcontexts state | ||
export * from './viewcontexts.actions'; | ||
export * from './viewcontexts.selectors'; |
26 changes: 26 additions & 0 deletions
26
src/app/core/store/content/viewcontexts/viewcontexts.actions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { createAction } from '@ngrx/store'; | ||
|
||
import { CallParameters } from 'ish-core/models/call-parameters/call-parameters.model'; | ||
import { ContentPageletEntryPoint } from 'ish-core/models/content-pagelet-entry-point/content-pagelet-entry-point.model'; | ||
import { ContentPagelet } from 'ish-core/models/content-pagelet/content-pagelet.model'; | ||
import { httpError, payload } from 'ish-core/utils/ngrx-creators'; | ||
|
||
export const loadViewContextEntrypoint = createAction( | ||
'[Content View Context] Load Entrypoint', | ||
payload<{ viewContextId: string; callParameters: CallParameters }>() | ||
); | ||
|
||
export const loadViewContextEntrypointFail = createAction( | ||
'[Content View Context API] Load Entrypoint Fail', | ||
httpError() | ||
); | ||
|
||
export const loadViewContextEntrypointSuccess = createAction( | ||
'[Content View Context API] Load Entrypoint Success', | ||
payload<{ | ||
entrypoint: ContentPageletEntryPoint; | ||
pagelets: ContentPagelet[]; | ||
viewContextId: string; | ||
callParameters: CallParameters; | ||
}>() | ||
); |
57 changes: 57 additions & 0 deletions
57
src/app/core/store/content/viewcontexts/viewcontexts.effects.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
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, of } from 'rxjs'; | ||
import { anything, instance, mock, when } from 'ts-mockito'; | ||
|
||
import { ContentPageletEntryPoint } from 'ish-core/models/content-pagelet-entry-point/content-pagelet-entry-point.model'; | ||
import { CMSService } from 'ish-core/services/cms/cms.service'; | ||
|
||
import { loadViewContextEntrypoint, loadViewContextEntrypointSuccess } from './viewcontexts.actions'; | ||
import { ViewcontextsEffects } from './viewcontexts.effects'; | ||
|
||
describe('Viewcontexts Effects', () => { | ||
let actions$: Observable<Action>; | ||
let effects: ViewcontextsEffects; | ||
let cmsServiceMock: CMSService; | ||
|
||
beforeEach(() => { | ||
cmsServiceMock = mock(CMSService); | ||
|
||
TestBed.configureTestingModule({ | ||
providers: [ | ||
ViewcontextsEffects, | ||
provideMockActions(() => actions$), | ||
{ provide: CMSService, useFactory: () => instance(cmsServiceMock) }, | ||
], | ||
}); | ||
|
||
effects = TestBed.inject(ViewcontextsEffects); | ||
}); | ||
|
||
describe('loadViewContextEntrypoint$', () => { | ||
it('should dispatch success actions when encountering loadViewcontexts', () => { | ||
when(cmsServiceMock.getViewContextContent(anything(), anything())).thenReturn( | ||
of({ entrypoint: { id: 'test' } as ContentPageletEntryPoint, pagelets: [] }) | ||
); | ||
|
||
actions$ = hot('-a-a-a', { | ||
a: loadViewContextEntrypoint({ | ||
viewContextId: 'test', | ||
callParameters: {}, | ||
}), | ||
}); | ||
const expected$ = cold('-c-c-c', { | ||
c: loadViewContextEntrypointSuccess({ | ||
entrypoint: { id: 'test' } as ContentPageletEntryPoint, | ||
pagelets: [], | ||
viewContextId: 'test', | ||
callParameters: {}, | ||
}), | ||
}); | ||
|
||
expect(effects.loadViewContextEntrypoint$).toBeObservable(expected$); | ||
}); | ||
}); | ||
}); |
32 changes: 32 additions & 0 deletions
32
src/app/core/store/content/viewcontexts/viewcontexts.effects.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { Actions, createEffect, ofType } from '@ngrx/effects'; | ||
import { concatMap, map } from 'rxjs/operators'; | ||
|
||
import { CMSService } from 'ish-core/services/cms/cms.service'; | ||
import { mapErrorToAction, mapToPayload } from 'ish-core/utils/operators'; | ||
|
||
import { | ||
loadViewContextEntrypoint, | ||
loadViewContextEntrypointFail, | ||
loadViewContextEntrypointSuccess, | ||
} from './viewcontexts.actions'; | ||
|
||
@Injectable() | ||
export class ViewcontextsEffects { | ||
constructor(private actions$: Actions, private cmsService: CMSService) {} | ||
|
||
loadViewContextEntrypoint$ = createEffect(() => | ||
this.actions$.pipe( | ||
ofType(loadViewContextEntrypoint), | ||
mapToPayload(), | ||
concatMap(({ viewContextId, callParameters }) => | ||
this.cmsService.getViewContextContent(viewContextId, callParameters).pipe( | ||
map(({ entrypoint, pagelets }) => | ||
loadViewContextEntrypointSuccess({ entrypoint, pagelets, viewContextId, callParameters }) | ||
), | ||
mapErrorToAction(loadViewContextEntrypointFail) | ||
) | ||
) | ||
) | ||
); | ||
} |
30 changes: 30 additions & 0 deletions
30
src/app/core/store/content/viewcontexts/viewcontexts.reducer.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { serializeContextSpecificViewContextId } from './viewcontexts.reducer'; | ||
|
||
describe('Viewcontexts Reducer', () => { | ||
it('should serialize callParameters in a sorted manner if callParameters are given', () => { | ||
const viewContextId = 'the_viewcontext'; | ||
const callParameters = { Product: 'TEST', Category: 'Hello@World', Extra: 'foo', Alternative: 'bar' }; | ||
|
||
expect(serializeContextSpecificViewContextId(viewContextId, callParameters)).toMatchInlineSnapshot( | ||
`"the_viewcontext@@Alternative-bar@@Category-Hello@World@@Extra-foo@@Product-TEST"` | ||
); | ||
}); | ||
|
||
it('should return the viewContextId if empty callParameters are provided', () => { | ||
const viewContextId = 'the_viewcontext'; | ||
const callParameters = {}; | ||
|
||
expect(serializeContextSpecificViewContextId(viewContextId, callParameters)).toMatchInlineSnapshot( | ||
`"the_viewcontext"` | ||
); | ||
}); | ||
|
||
it('should return the viewContextId if if no callParameters are provided', () => { | ||
const viewContextId = 'the_viewcontext'; | ||
const callParameters = undefined; | ||
|
||
expect(serializeContextSpecificViewContextId(viewContextId, callParameters)).toMatchInlineSnapshot( | ||
`"the_viewcontext"` | ||
); | ||
}); | ||
}); |
42 changes: 42 additions & 0 deletions
42
src/app/core/store/content/viewcontexts/viewcontexts.reducer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { EntityState, createEntityAdapter } from '@ngrx/entity'; | ||
import { createReducer, on } from '@ngrx/store'; | ||
|
||
import { CallParameters } from 'ish-core/models/call-parameters/call-parameters.model'; | ||
import { ContentPageletEntryPoint } from 'ish-core/models/content-pagelet-entry-point/content-pagelet-entry-point.model'; | ||
|
||
import { loadViewContextEntrypointSuccess } from './viewcontexts.actions'; | ||
|
||
declare type ContentPageletEntryPointWithContext = ContentPageletEntryPoint & { | ||
viewContextId: string; | ||
callParameters: CallParameters; | ||
}; | ||
|
||
export function serializeContextSpecificViewContextId(viewContextId: string, callParameters: CallParameters) { | ||
const serializedParams = callParameters | ||
? Object.entries(callParameters) | ||
.sort() | ||
.map(([key, value]) => `@@${key}-${value}`) | ||
.join('') | ||
: ''; | ||
return viewContextId + serializedParams; | ||
} | ||
|
||
export const viewcontextsAdapter = createEntityAdapter<ContentPageletEntryPointWithContext>({ | ||
selectId: viewcontext => serializeContextSpecificViewContextId(viewcontext.viewContextId, viewcontext.callParameters), | ||
}); | ||
|
||
export interface ViewcontextsState extends EntityState<ContentPageletEntryPointWithContext> {} | ||
|
||
const initialState: ViewcontextsState = viewcontextsAdapter.getInitialState({}); | ||
|
||
export const viewcontextsReducer = createReducer( | ||
initialState, | ||
|
||
on(loadViewContextEntrypointSuccess, (state: ViewcontextsState, action) => { | ||
const { entrypoint, viewContextId, callParameters } = action.payload; | ||
|
||
return { | ||
...viewcontextsAdapter.upsertOne({ ...entrypoint, viewContextId, callParameters }, state), | ||
}; | ||
}) | ||
); |
Oops, something went wrong.