Skip to content

Commit

Permalink
feat: added permissions store connector service (#661)
Browse files Browse the repository at this point in the history
* feat: added permissions store connector service

* misc: renamed action

* misc: added export for permissions connector
  • Loading branch information
BenjaminPabst authored Jan 31, 2025
1 parent 9180db9 commit 7e9f6dd
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions libs/ngrx-accelerator/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export * from './lib/utils/local-storage/create-nested-key-configuration'

// Store Connector
export * from './lib/store-connector/navigated-event-store-connector-service'
export * from './lib/store-connector/permissions-store-connector-service'
export * from './lib/store-connector/onecx-actions'
export * from './lib/store-connector/onecx-reducer'
export * from './lib/store-connector/onecx-selectors'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ export const OneCxActions = createActionGroup({
navigated: props<{
event: undefined | unknown
}>(),
permissionsChanged: props<{
permissions: string[]
}>(),
},
})
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,12 @@ export const oneCxReducer = createReducer<OneCxState>(
...state,
location: action.event as NavigatedEventPayload,
})
),
on(
OneCxActions.permissionsChanged,
(state: OneCxState, action): OneCxState => ({
...state,
permissions: action.permissions,
})
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ export function createOneCxSelector<State extends Record<string, any>>(): Memoiz
export type OneCxSelectors<V> = {
selectLocation: MemoizedSelector<V, LocationState | undefined>
selectBackNavigationPossible: MemoizedSelector<V, boolean>
selectPermissions: MemoizedSelector<V, string[] | undefined>
}

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)
const selectPermissions = createSelector(selectState, (state) => state.permissions)
return {
selectLocation,
selectBackNavigationPossible,
selectPermissions,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ import { NavigatedEventPayload } from '@onecx/integration-interface'
export type LocationState = NavigatedEventPayload
export interface OneCxState {
location?: LocationState | undefined
permissions?: string[] | undefined
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { ENVIRONMENT_INITIALIZER, Injectable, OnDestroy, inject } from '@angular/core'
import { Store } from '@ngrx/store'
import { PermissionsTopic } from '@onecx/integration-interface'
import { OneCxActions } from './onecx-actions'

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

@Injectable()
export class PermissionsStoreConnectorService implements OnDestroy {
permissionsTopic$ = new PermissionsTopic()
constructor(store: Store) {
this.permissionsTopic$.subscribe((permissions) => {
store.dispatch(OneCxActions.permissionsChanged({ permissions }))
})
}
ngOnDestroy(): void {
this.permissionsTopic$.destroy()
}
}

0 comments on commit 7e9f6dd

Please sign in to comment.