Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add navigation history #532

Merged
merged 10 commits into from
Oct 15, 2024
1 change: 1 addition & 0 deletions libs/integration-interface/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ export * from './lib/topics/permissions-rpc/v1/permissions-rpc.model'

export * from './lib/topics/events/v1/events-topic'
export * from './lib/topics/events/v1/topic-event-type'
export * from './lib/topics/events/v1/payload-navigated-event'
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type PayloadNavigatedEvent = {
url: string | undefined
isFirst: boolean
history: string[]
}
9 changes: 8 additions & 1 deletion libs/ngrx-accelerator/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,11 @@ export * from './lib/utils/effects/filter-for-query-params-changed'
export * from './lib/utils/selectors/create-child-selectors'

// Local Storage
export * from './lib/utils/local-storage/lazy-loading-merge-reducer'
export * from './lib/utils/local-storage/lazy-loading-merge-reducer'

// Store Connector
export * from './lib/store-connector/navigated-event-store-connector-service'
export * from './lib/store-connector/onecx-actions'
export * from './lib/store-connector/onecx-reducer'
export * from './lib/store-connector/onecx-selectors'
export * from './lib/store-connector/onecx-state'
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { EventsTopic } from '@onecx/integration-interface'
import { ENVIRONMENT_INITIALIZER, Injectable, OnDestroy, inject } from '@angular/core'
import { Store } from '@ngrx/store'
import { filter } from 'rxjs'
import { OneCxActions } from './onecx-actions'

export function provideNavigatedEventStoreConnector() {
return [
{
provide: ENVIRONMENT_INITIALIZER,
multi: true,
useFactory() {
return () => inject(NavigatedEventStoreConnectorService)
},
},
NavigatedEventStoreConnectorService,
]
}

@Injectable()
export class NavigatedEventStoreConnectorService implements OnDestroy {
eventsTopic$ = new EventsTopic()
constructor(store: Store) {
this.eventsTopic$.pipe(filter((e) => e.type === 'navigated')).subscribe((eventType) => {
store.dispatch(OneCxActions.navigated({ event: eventType.payload }))
anninowak marked this conversation as resolved.
Show resolved Hide resolved
})
}
ngOnDestroy(): void {
this.eventsTopic$.destroy()
}
}
10 changes: 10 additions & 0 deletions libs/ngrx-accelerator/src/lib/store-connector/onecx-actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { createActionGroup, props } from '@ngrx/store'

export const OneCxActions = createActionGroup({
source: 'OneCX',
events: {
navigated: props<{
event: undefined | unknown
anninowak marked this conversation as resolved.
Show resolved Hide resolved
}>(),
},
})
15 changes: 15 additions & 0 deletions libs/ngrx-accelerator/src/lib/store-connector/onecx-reducer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { createReducer, on } from '@ngrx/store'
import { OneCxActions } from './onecx-actions'
import { OneCxState } from './onecx-state'
import { PayloadNavigatedEvent } from '@onecx/integration-interface'

export const oneCxReducer = createReducer<OneCxState>(
{},
on(
OneCxActions.navigated,
(state: OneCxState, action): OneCxState => ({
...state,
location: action.event as PayloadNavigatedEvent,
})
)
)
22 changes: 22 additions & 0 deletions libs/ngrx-accelerator/src/lib/store-connector/onecx-selectors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { MemoizedSelector, createFeatureSelector, createSelector } from '@ngrx/store'
import { OneCxState } from './onecx-state'
import { LocationState } from './onecx-state'
export function createOneCxSelector<State extends Record<string, any>>(): MemoizedSelector<State, OneCxState> {
return createFeatureSelector('onecx')
}

export type OneCxSelectors<V> = {
selectLocation: MemoizedSelector<V, LocationState | undefined>
selectBackNavigationPossible: MemoizedSelector<V, boolean>
}

export function getOneCxSelectors<V extends Record<string, any>>(
selectState: (state: V) => OneCxState = createOneCxSelector<V>()
): OneCxSelectors<V> {
const selectLocation = createSelector(selectState, (state) => state.location)
const selectBackNavigationPossible = createSelector(selectLocation, (location) => !!location && !location?.isFirst)
return {
selectLocation,
selectBackNavigationPossible,
}
}
6 changes: 6 additions & 0 deletions libs/ngrx-accelerator/src/lib/store-connector/onecx-state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { PayloadNavigatedEvent } from '@onecx/integration-interface'

export type LocationState = PayloadNavigatedEvent
anninowak marked this conversation as resolved.
Show resolved Hide resolved
export interface OneCxState {
location?: LocationState | undefined
}
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.