From ea23a74ade201433a6671ad8105c47017735efda Mon Sep 17 00:00:00 2001 From: Sheik Althaf Date: Wed, 5 Jun 2024 23:43:33 +0530 Subject: [PATCH 1/2] refactor: migrate to signals for checklist --- .../checklist-cta-bar.component.html | 4 +- .../checklist-cta-bar.component.ts | 3 +- .../checklist-detail-view.component.html | 2 +- .../checklist-detail-view.component.ts | 20 +-- .../checklist-favorites-view.component.html | 4 +- .../checklist-favorites-view.component.ts | 23 +-- .../checklist-list-view.component.html | 6 +- .../checklist-list-view.component.ts | 40 ++--- .../checklist-overview.component.html | 2 +- .../checklist-overview.component.ts | 59 +++---- .../checklist-search.component.html | 4 +- .../checklist-search.component.ts | 46 +++-- src/app/checklist/checklist.component.html | 30 ++-- src/app/checklist/checklist.component.ts | 164 ++++++------------ .../confirmation-dialog.component.ts | 13 +- src/app/checklist/search/search.service.ts | 2 +- src/app/shared/breakpoint.service.ts | 52 ++---- 17 files changed, 186 insertions(+), 288 deletions(-) diff --git a/src/app/checklist/checklist-cta-bar/checklist-cta-bar.component.html b/src/app/checklist/checklist-cta-bar/checklist-cta-bar.component.html index 97c9cca..4927f01 100644 --- a/src/app/checklist/checklist-cta-bar/checklist-cta-bar.component.html +++ b/src/app/checklist/checklist-cta-bar/checklist-cta-bar.component.html @@ -14,7 +14,7 @@
- - + +
diff --git a/src/app/checklist/checklist-cta-bar/checklist-cta-bar.component.ts b/src/app/checklist/checklist-cta-bar/checklist-cta-bar.component.ts index b9545be..62fa1d1 100644 --- a/src/app/checklist/checklist-cta-bar/checklist-cta-bar.component.ts +++ b/src/app/checklist/checklist-cta-bar/checklist-cta-bar.component.ts @@ -3,13 +3,14 @@ import { ChecklistFilter } from '../models/checklist.model'; import { MatIcon } from '@angular/material/icon'; import { MatButton } from '@angular/material/button'; import { NgIf } from '@angular/common'; +import { MatTooltip } from '@angular/material/tooltip'; @Component({ standalone: true, selector: 'ac-checklist-cta-bar', templateUrl: './checklist-cta-bar.component.html', styleUrls: ['./checklist-cta-bar.component.scss'], - imports: [NgIf, MatButton, MatIcon] + imports: [NgIf, MatButton, MatIcon, MatTooltip] }) export class ChecklistCtaBarComponent { @Input() filter: ChecklistFilter; diff --git a/src/app/checklist/checklist-detail-view/checklist-detail-view.component.html b/src/app/checklist/checklist-detail-view/checklist-detail-view.component.html index 9558052..1efcda3 100644 --- a/src/app/checklist/checklist-detail-view/checklist-detail-view.component.html +++ b/src/app/checklist/checklist-detail-view/checklist-detail-view.component.html @@ -1,4 +1,4 @@ - +
Done ; - - constructor(private store: Store) {} - - ngOnInit() { - this.item$ = this.store.pipe(select(ChecklistSelectors.getSelectedItem)); - } +export class ChecklistDetailViewComponent { + private store = inject>(Store); + item = this.store.selectSignal(ChecklistSelectors.getSelectedItem); toggleItem(item: ChecklistItem) { this.store.dispatch(new ToggleItem(item)); diff --git a/src/app/checklist/checklist-favorites-view/checklist-favorites-view.component.html b/src/app/checklist/checklist-favorites-view/checklist-favorites-view.component.html index f2017af..4c052de 100644 --- a/src/app/checklist/checklist-favorites-view/checklist-favorites-view.component.html +++ b/src/app/checklist/checklist-favorites-view/checklist-favorites-view.component.html @@ -1,9 +1,9 @@
Favorites
- + - +

    {{ favorite.category.title }}

    diff --git a/src/app/checklist/checklist-favorites-view/checklist-favorites-view.component.ts b/src/app/checklist/checklist-favorites-view/checklist-favorites-view.component.ts index 7cf6ffe..55ec841 100644 --- a/src/app/checklist/checklist-favorites-view/checklist-favorites-view.component.ts +++ b/src/app/checklist/checklist-favorites-view/checklist-favorites-view.component.ts @@ -1,6 +1,5 @@ -import { Component, OnInit } from '@angular/core'; -import { select, Store } from '@ngrx/store'; -import { Observable } from 'rxjs'; +import { Component, inject } from '@angular/core'; +import { Store } from '@ngrx/store'; import { ToggleFavorite, ToggleItem } from '../../projects/state/projects.actions'; import { ApplicationState } from '../../state/app.state'; import { ChecklistFilter, ChecklistItem, Favorite } from '../models/checklist.model'; @@ -8,7 +7,7 @@ import { SetFavoritesFilter } from '../state/checklist.actions'; import { ChecklistSelectors } from '../state/checklist.selectors'; import { ChecklistListItemComponent } from '../checklist-list/checklist-list-item.component'; import { ChecklistListComponent } from '../checklist-list/checklist-list.component'; -import { NgIf, NgFor, AsyncPipe } from '@angular/common'; +import { NgIf, NgFor } from '@angular/common'; import { ChecklistCtaBarComponent } from '../checklist-cta-bar/checklist-cta-bar.component'; @Component({ @@ -16,18 +15,12 @@ import { ChecklistCtaBarComponent } from '../checklist-cta-bar/checklist-cta-bar selector: 'ac-checklist-favorites-view', templateUrl: './checklist-favorites-view.component.html', styleUrls: ['./checklist-favorites-view.component.scss'], - imports: [ChecklistCtaBarComponent, NgIf, NgFor, ChecklistListComponent, ChecklistListItemComponent, AsyncPipe] + imports: [ChecklistCtaBarComponent, NgIf, NgFor, ChecklistListComponent, ChecklistListItemComponent] }) -export class ChecklistFavoritesViewComponent implements OnInit { - favorites$: Observable>; - filter$: Observable; - - constructor(private store: Store) {} - - ngOnInit() { - this.favorites$ = this.store.pipe(select(ChecklistSelectors.getFilteredFavorites)); - this.filter$ = this.store.pipe(select(ChecklistSelectors.getFavoritesFilter)); - } +export class ChecklistFavoritesViewComponent { + private store = inject>(Store); + favorites = this.store.selectSignal(ChecklistSelectors.getFilteredFavorites); + filter = this.store.selectSignal(ChecklistSelectors.getFavoritesFilter); setFilter(filter: ChecklistFilter) { this.store.dispatch(new SetFavoritesFilter(filter)); diff --git a/src/app/checklist/checklist-list-view/checklist-list-view.component.html b/src/app/checklist/checklist-list-view/checklist-list-view.component.html index c18c5fd..3354863 100644 --- a/src/app/checklist/checklist-list-view/checklist-list-view.component.html +++ b/src/app/checklist/checklist-list-view/checklist-list-view.component.html @@ -1,6 +1,6 @@ ; - filter$: Observable; - showActionButtons$: Observable; - - constructor(private store: Store, private breakpointService: BreakpointService) {} - - ngOnInit() { - this.items$ = this.store.pipe(select(ChecklistSelectors.getItemsFromSelectedCategory)); - this.filter$ = this.store.pipe(select(ChecklistSelectors.getCategoriesFilter)); - - const { medium$, desktop$ } = this.breakpointService.getAllBreakpoints(); - this.showActionButtons$ = combineLatest(medium$, desktop$, (medium, desktop) => medium || desktop); - } +export class ListViewComponent { + private store = inject>(Store); + private breakpointService = inject(BreakpointService); + items = this.store.selectSignal(ChecklistSelectors.getItemsFromSelectedCategory); + filter = this.store.selectSignal(ChecklistSelectors.getCategoriesFilter); + showActionButtons = computed(() => this.breakpointService.medium() || this.breakpointService.desktop()); toggleItem(item: ChecklistItem) { this.store.dispatch(new ToggleItem(item)); @@ -44,11 +34,13 @@ export class ListViewComponent implements OnInit { } checkAllItems() { - this.getSelectedCategory().subscribe(category => this.store.dispatch(new CheckAll(category))); + const categories = this.getSelectedCategory(); + this.store.dispatch(new CheckAll(categories)); } uncheckAllItems() { - this.getSelectedCategory().subscribe(category => this.store.dispatch(new UncheckAll(category))); + const categories = this.getSelectedCategory(); + this.store.dispatch(new UncheckAll(categories)); } toggleFavorite(item: ChecklistItem) { @@ -59,7 +51,7 @@ export class ListViewComponent implements OnInit { return item.id; } - private getSelectedCategory(): Observable { - return this.store.pipe(selectOnce(ChecklistSelectors.getSelectedCategory)); + private get getSelectedCategory(): Signal { + return this.store.selectSignal(ChecklistSelectors.getSelectedCategory); } } diff --git a/src/app/checklist/checklist-overview/checklist-overview.component.html b/src/app/checklist/checklist-overview/checklist-overview.component.html index b7b9f1a..abb1122 100644 --- a/src/app/checklist/checklist-overview/checklist-overview.component.html +++ b/src/app/checklist/checklist-overview/checklist-overview.component.html @@ -1,4 +1,4 @@ -