Skip to content

Commit

Permalink
refactor(home): replace component-store with signal store
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanoslig committed Aug 10, 2024
1 parent 56ee53a commit 558562b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 40 deletions.
4 changes: 2 additions & 2 deletions libs/home/src/lib/home.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ <h1 class="logo-font">conduit</h1>

<div class="col-md-3">
<div class="sidebar">
@defer (when !!(tags$ | async)?.length) {
<cdt-tags-list (setListTag)="setListTag($event)" [tags]="(tags$ | async) ?? []"></cdt-tags-list>
@defer (when !!$tags().length) {
<cdt-tags-list (setListTag)="setListTag($event)" [tags]="$tags()"></cdt-tags-list>
} @placeholder {
loading...
}
Expand Down
10 changes: 5 additions & 5 deletions libs/home/src/lib/home.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { ArticlesListStore, ListType, articlesListInitialState } from '@realworl
import { AsyncPipe, NgClass } from '@angular/common';
import { TagsListComponent } from './tags-list/tags-list.component';
import { ArticleListComponent } from '@realworld/articles/feature-articles-list/src';
import { HomeStoreService } from './home.store';
import { provideComponentStore } from '@ngrx/component-store';
import { HomeStore } from './home.store';

import { AuthStore } from '@realworld/auth/data-access';

@Component({
Expand All @@ -13,16 +13,16 @@ import { AuthStore } from '@realworld/auth/data-access';
templateUrl: './home.component.html',
styleUrls: ['./home.component.css'],
imports: [AsyncPipe, NgClass, TagsListComponent, ArticleListComponent],
providers: [provideComponentStore(HomeStoreService)],
providers: [HomeStore],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class HomeComponent {
private readonly articlesListStore = inject(ArticlesListStore);
private readonly authStore = inject(AuthStore);
private readonly homeStore = inject(HomeStoreService);
private readonly homeStore = inject(HomeStore);

$listConfig = this.articlesListStore.listConfig;
tags$ = this.homeStore.tags$;
$tags = this.homeStore.tags;

readonly loadArticlesOnLogin = effect(() => {
const isLoggedIn = this.authStore.loggedIn();
Expand Down
60 changes: 28 additions & 32 deletions libs/home/src/lib/home.store.ts
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();
},
}),
);
2 changes: 1 addition & 1 deletion libs/home/src/lib/tags-list/tags-list.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ import { Component, Input, Output, EventEmitter, ChangeDetectionStrategy, output
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class TagsListComponent {
tags = input([]);
tags = input<string[]>([]);
setListTag = output<string>();
}

0 comments on commit 558562b

Please sign in to comment.