Create app state selector

This commit is contained in:
Z. Charles Dziura 2025-06-30 21:14:20 -04:00
parent c452fbc5ef
commit 850b99d871
4 changed files with 29 additions and 6 deletions

View file

@ -1,3 +1,4 @@
<main>
<h1>Hello, world!</h1>
<p>Current View: {{ currentView$ | async }}</p>
</main>

View file

@ -1,10 +1,18 @@
import { Component } from '@angular/core';
import { StoreModule } from '@ngrx/store';
import { appStateReducer } from './state/state.reducer';
import { AsyncPipe } from '@angular/common';
import { Component, inject, OnInit } from '@angular/core';
import { select, Store } from '@ngrx/store';
import { selectCurrentView } from './state/state.selectors';
@Component({
selector: 'app-root',
templateUrl: './app.html',
styleUrl: './app.scss',
imports: [AsyncPipe],
})
export class App {}
export class App implements OnInit {
private readonly store$ = inject(Store);
readonly currentView$ = this.store$.pipe(select(selectCurrentView));
ngOnInit(): void {}
}

View file

@ -1,7 +1,15 @@
import { createReducer } from '@ngrx/store';
export interface AppState {}
export interface AppState {
currentView: CurrentView;
}
export const initialState: AppState = {};
export enum CurrentView {
StartingPoint = 0,
}
export const initialState: AppState = {
currentView: CurrentView.StartingPoint,
};
export const appStateReducer = createReducer(initialState);

View file

@ -0,0 +1,6 @@
import { createFeatureSelector, createSelector } from '@ngrx/store';
import { AppState } from './state.reducer';
export const selectAppState = createFeatureSelector<AppState>('app');
export const selectCurrentView = createSelector(selectAppState, (state: AppState) => state.currentView);