-
-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(home): replace component-store with signal store
- Loading branch information
1 parent
56ee53a
commit 558562b
Showing
4 changed files
with
36 additions
and
40 deletions.
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
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 |
---|---|---|
@@ -1,42 +1,38 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { ComponentStore, OnStateInit } from '@ngrx/component-store'; | ||
import { pipe } from 'rxjs'; | ||
import { switchMap } from 'rxjs/operators'; | ||
import { HomeService } from './home.service'; | ||
import { signalStore, withState, withMethods, patchState, withHooks } from '@ngrx/signals'; | ||
import { rxMethod } from '@ngrx/signals/rxjs-interop'; | ||
import { inject } from '@angular/core'; | ||
import { pipe, switchMap } from 'rxjs'; | ||
import { tapResponse } from '@ngrx/operators'; | ||
import { HomeService } from './home.service'; | ||
|
||
export interface HomeState { | ||
tags: string[]; | ||
} | ||
|
||
@Injectable() | ||
export class HomeStoreService extends ComponentStore<HomeState> implements OnStateInit { | ||
constructor(private readonly homeService: HomeService) { | ||
super({ tags: [] }); | ||
} | ||
|
||
ngrxOnStateInit() { | ||
this.getTags(); | ||
} | ||
|
||
// SELECTORS | ||
tags$ = this.select((store) => store.tags); | ||
|
||
// EFFECTS | ||
readonly getTags = this.effect<void>( | ||
pipe( | ||
switchMap(() => | ||
this.homeService.getTags().pipe( | ||
tapResponse( | ||
(response) => { | ||
this.patchState({ tags: response.tags }); | ||
}, | ||
(error) => { | ||
console.error('error getting tags: ', error); | ||
}, | ||
export const HomeStore = signalStore( | ||
{ providedIn: 'root' }, | ||
withState<HomeState>({ tags: [] }), | ||
withMethods((store, homeService = inject(HomeService)) => ({ | ||
getTags: rxMethod<void>( | ||
pipe( | ||
switchMap(() => | ||
homeService.getTags().pipe( | ||
tapResponse( | ||
(response) => { | ||
patchState(store, { tags: response.tags }); | ||
}, | ||
(error) => { | ||
console.error('error getting tags: ', error); | ||
}, | ||
), | ||
), | ||
), | ||
), | ||
), | ||
); | ||
} | ||
})), | ||
withHooks({ | ||
onInit({ getTags }) { | ||
getTags(); | ||
}, | ||
}), | ||
); |
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