Migrate global app state to signals library
This commit is contained in:
parent
8e026944aa
commit
f2d67bc56f
6 changed files with 20 additions and 37 deletions
|
@ -1,23 +1,15 @@
|
||||||
import {
|
import {
|
||||||
ApplicationConfig,
|
ApplicationConfig,
|
||||||
importProvidersFrom,
|
|
||||||
isDevMode,
|
isDevMode,
|
||||||
provideBrowserGlobalErrorListeners,
|
provideBrowserGlobalErrorListeners,
|
||||||
provideZonelessChangeDetection,
|
provideZonelessChangeDetection,
|
||||||
} from '@angular/core';
|
} from '@angular/core';
|
||||||
import { StoreModule } from '@ngrx/store';
|
|
||||||
import { provideStoreDevtools } from '@ngrx/store-devtools';
|
import { provideStoreDevtools } from '@ngrx/store-devtools';
|
||||||
import { appStateReducer } from './state/state.reducer';
|
|
||||||
|
|
||||||
export const appConfig: ApplicationConfig = {
|
export const appConfig: ApplicationConfig = {
|
||||||
providers: [
|
providers: [
|
||||||
provideBrowserGlobalErrorListeners(),
|
provideBrowserGlobalErrorListeners(),
|
||||||
provideZonelessChangeDetection(),
|
provideZonelessChangeDetection(),
|
||||||
importProvidersFrom(
|
|
||||||
StoreModule.forRoot({
|
|
||||||
app: appStateReducer,
|
|
||||||
})
|
|
||||||
),
|
|
||||||
provideStoreDevtools({
|
provideStoreDevtools({
|
||||||
maxAge: 25,
|
maxAge: 25,
|
||||||
logOnly: !isDevMode(),
|
logOnly: !isDevMode(),
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
<main>
|
<main>
|
||||||
@switch (currentView$ | async) {
|
@switch (currentView()) {
|
||||||
@default {
|
@default {
|
||||||
<app-starting-point></app-starting-point>
|
<app-starting-point></app-starting-point>
|
||||||
}
|
}
|
||||||
|
|
15
src/app/app.state.ts
Normal file
15
src/app/app.state.ts
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
import { signalStore, withState } from '@ngrx/signals';
|
||||||
|
|
||||||
|
export type AppState = {
|
||||||
|
currentView: CurrentView;
|
||||||
|
};
|
||||||
|
|
||||||
|
export enum CurrentView {
|
||||||
|
StartingPoint = 'startingPoint',
|
||||||
|
}
|
||||||
|
|
||||||
|
const initialState: AppState = {
|
||||||
|
currentView: CurrentView.StartingPoint,
|
||||||
|
};
|
||||||
|
|
||||||
|
export const AppStateStore = signalStore({ providedIn: 'root' }, withState(initialState));
|
|
@ -1,7 +1,5 @@
|
||||||
import { AsyncPipe } from '@angular/common';
|
|
||||||
import { ChangeDetectionStrategy, Component, inject, OnInit } from '@angular/core';
|
import { ChangeDetectionStrategy, Component, inject, OnInit } from '@angular/core';
|
||||||
import { select, Store } from '@ngrx/store';
|
import { AppStateStore } from './app.state';
|
||||||
import { selectCurrentView } from './state/state.selectors';
|
|
||||||
import { StartingPoint } from './views/starting-point/starting-point';
|
import { StartingPoint } from './views/starting-point/starting-point';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
|
@ -9,12 +7,11 @@ import { StartingPoint } from './views/starting-point/starting-point';
|
||||||
templateUrl: './app.html',
|
templateUrl: './app.html',
|
||||||
styleUrl: './app.scss',
|
styleUrl: './app.scss',
|
||||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||||
imports: [AsyncPipe, StartingPoint],
|
imports: [StartingPoint],
|
||||||
})
|
})
|
||||||
export class App implements OnInit {
|
export class App implements OnInit {
|
||||||
private readonly store$ = inject(Store);
|
private readonly state = inject(AppStateStore);
|
||||||
|
readonly currentView = this.state.currentView;
|
||||||
readonly currentView$ = this.store$.pipe(select(selectCurrentView));
|
|
||||||
|
|
||||||
ngOnInit(): void {}
|
ngOnInit(): void {}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,15 +0,0 @@
|
||||||
import { createReducer } from '@ngrx/store';
|
|
||||||
|
|
||||||
export interface AppState {
|
|
||||||
currentView: CurrentView;
|
|
||||||
}
|
|
||||||
|
|
||||||
export enum CurrentView {
|
|
||||||
StartingPoint = 'startingPoint',
|
|
||||||
}
|
|
||||||
|
|
||||||
export const initialState: AppState = {
|
|
||||||
currentView: CurrentView.StartingPoint,
|
|
||||||
};
|
|
||||||
|
|
||||||
export const appStateReducer = createReducer(initialState);
|
|
|
@ -1,6 +0,0 @@
|
||||||
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);
|
|
Loading…
Add table
Reference in a new issue