From afc94e2afc296d2ba67e97f185a60f52a3ba2d88 Mon Sep 17 00:00:00 2001 From: Milad Raeisi Date: Fri, 13 Dec 2024 15:56:22 +0400 Subject: [PATCH] Refactor BookmarkComponent to use signals for loading state and project details; update template bindings accordingly --- .../bookmark/bookmark.component.html | 6 +-- .../components/bookmark/bookmark.component.ts | 48 ++++++++----------- 2 files changed, 24 insertions(+), 30 deletions(-) diff --git a/src/app/components/bookmark/bookmark.component.html b/src/app/components/bookmark/bookmark.component.html index 06daf3f..dbb43be 100644 --- a/src/app/components/bookmark/bookmark.component.html +++ b/src/app/components/bookmark/bookmark.component.html @@ -27,7 +27,7 @@
- +
Card cover image
- +
Loading projects...
- +
diff --git a/src/app/components/bookmark/bookmark.component.ts b/src/app/components/bookmark/bookmark.component.ts index 2745e86..425fe34 100644 --- a/src/app/components/bookmark/bookmark.component.ts +++ b/src/app/components/bookmark/bookmark.component.ts @@ -1,6 +1,6 @@ import { AngorCardComponent } from '@angor/components/card'; import { CommonModule, NgClass } from '@angular/common'; -import { Component, OnDestroy, OnInit } from '@angular/core'; +import { Component, OnDestroy, OnInit, inject, signal } from '@angular/core'; import { MatButtonModule } from '@angular/material/button'; import { MatOptionModule } from '@angular/material/core'; import { MatFormFieldModule } from '@angular/material/form-field'; @@ -40,69 +40,63 @@ import { catchError, Observable, of, Subject, takeUntil, tap } from 'rxjs'; styleUrls: ['./bookmark.component.scss'], }) export class BookmarkComponent implements OnInit, OnDestroy { - savedProjects: Project[] = []; - savedProjectDetailes: ProjectDetails[] = []; - bookmarks$: Observable; - isLoading = true; + bookmarkService = inject(BookmarkService); + savedProjectDetailes = signal([]); + isLoading = signal(false); + bookmarks$ = this.bookmarkService.bookmarks$; private _unsubscribeAll = new Subject(); constructor( - private _bookmarkService: BookmarkService, private _storageService: StorageService, private _router: Router, private _projectsService: ProjectsService, - ) { - this.bookmarks$ = this._bookmarkService.bookmarks$; - } + ) {} async ngOnInit(): Promise { try { - await this._bookmarkService.initializeForCurrentUser(); + await this.bookmarkService.initializeForCurrentUser(); await this.loadBookmarkedProjects(); this.subscribeToBookmarkChanges(); - this.isLoading = false; + this.isLoading.set(false); } catch (error) { console.error('Error during initialization:', error); - this.isLoading = false; + this.isLoading.set(false); } } - trackByFn(index: number, item: ProjectDetails): string | number { return item.nostrPubKey || index; } private async loadBookmarkedProjects(): Promise { - this.isLoading = true; + this.isLoading.set(true); try { - const bookmarkIds = await this._bookmarkService.getBookmarks(); + const bookmarkIds = await this.bookmarkService.getBookmarks(); const projects = await this._storageService.getProjectsByNostrPubKeys(bookmarkIds); - this.savedProjectDetailes = projects; - this.isLoading = false; + this.savedProjectDetailes.set(projects); + this.isLoading.set(false); } catch (error) { console.error('Error loading bookmarked projects:', error); - this.isLoading = false; + this.isLoading.set(false); } } - private subscribeToBookmarkChanges(): void { this.bookmarks$ .pipe(takeUntil(this._unsubscribeAll)) .subscribe(async (bookmarkIds) => { try { const projects = await this._storageService.getProjectsByNostrPubKeys(bookmarkIds); - this.savedProjectDetailes = projects; - this.fetchMetadataForProjects(this.savedProjectDetailes); - this.isLoading = false; + this.savedProjectDetailes.set(projects); + this.fetchMetadataForProjects(this.savedProjectDetailes()); + this.isLoading.set(false); } catch (error) { console.error('Error updating bookmarks:', error); - this.isLoading = false; + this.isLoading.set(false); } }); } - private fetchMetadataForProjects(projects: ProjectDetails[]): void { projects.forEach((project) => { this._storageService @@ -124,11 +118,11 @@ export class BookmarkComponent implements OnInit, OnDestroy { async toggleBookmark(projectNpub: string): Promise { const isBookmarked = - await this._bookmarkService.isBookmarked(projectNpub); + await this.bookmarkService.isBookmarked(projectNpub); if (isBookmarked) { - await this._bookmarkService.removeBookmark(projectNpub); + await this.bookmarkService.removeBookmark(projectNpub); } else { - await this._bookmarkService.addBookmark(projectNpub); + await this.bookmarkService.addBookmark(projectNpub); } }