From 561f27b065913ff2342214a72ad0a4db66001176 Mon Sep 17 00:00:00 2001 From: Mohammed Abad Date: Sun, 28 Apr 2024 00:49:17 +0100 Subject: [PATCH] fix library not refreching when add new entry --- .../resolvers/library-resolver.service.ts | 15 +++++++++++ .../features/library/library.component.css | 2 -- .../features/library/library.component.html | 4 +-- .../app/features/library/library.component.ts | 25 +++++++++++++++---- 4 files changed, 37 insertions(+), 9 deletions(-) create mode 100644 frontend/src/app/core/resolvers/library-resolver.service.ts diff --git a/frontend/src/app/core/resolvers/library-resolver.service.ts b/frontend/src/app/core/resolvers/library-resolver.service.ts new file mode 100644 index 0000000..5e89928 --- /dev/null +++ b/frontend/src/app/core/resolvers/library-resolver.service.ts @@ -0,0 +1,15 @@ +import { Injectable } from '@angular/core'; +import { Resolve, ActivatedRouteSnapshot } from '@angular/router'; +import { Observable } from 'rxjs'; +import { LibraryService } from '../services/library.service'; + +@Injectable({ + providedIn: 'root', +}) +export class LibraryEntriesResolver implements Resolve { + constructor(private libraryService: LibraryService) {} + + resolve(route: ActivatedRouteSnapshot): Observable { + return this.libraryService.getAllEntries(); + } +} diff --git a/frontend/src/app/features/library/library.component.css b/frontend/src/app/features/library/library.component.css index 00945fa..2709cf5 100644 --- a/frontend/src/app/features/library/library.component.css +++ b/frontend/src/app/features/library/library.component.css @@ -1,4 +1,3 @@ -/* Custom CSS to visually connect the elements */ .custom-group .form-select { border-top-right-radius: 0; border-bottom-right-radius: 0; @@ -32,7 +31,6 @@ margin-right: -1px; } -/* Adjust the select width as needed */ #single-select-field { max-width: 120px; } \ No newline at end of file diff --git a/frontend/src/app/features/library/library.component.html b/frontend/src/app/features/library/library.component.html index 6646cff..33d2bfe 100644 --- a/frontend/src/app/features/library/library.component.html +++ b/frontend/src/app/features/library/library.component.html @@ -39,7 +39,7 @@ -
+
  • Previous @@ -58,7 +58,7 @@ -
    +
    • Previous diff --git a/frontend/src/app/features/library/library.component.ts b/frontend/src/app/features/library/library.component.ts index 327b54c..254a7eb 100644 --- a/frontend/src/app/features/library/library.component.ts +++ b/frontend/src/app/features/library/library.component.ts @@ -1,6 +1,7 @@ import { Component } from '@angular/core'; import { Entry } from '../../shared/models/entry.model'; import { LibraryService } from '../../core/services/library.service'; +import { NavigationEnd, Router } from '@angular/router'; @Component({ selector: 'app-library', @@ -18,22 +19,30 @@ export class LibraryComponent { currentPage: number = 1; pageSize: number = 10; - constructor(private libraryService: LibraryService) {} + constructor(private router: Router, private libraryService: LibraryService) {} ngOnInit() { this.pageSize = this.getDefaultPageSize(); - this.fetchEntries(); + this.getEntries(); + + this.router.events.subscribe((event) => { + if (event instanceof NavigationEnd && event.url === '/library') { + this.pageSize = this.getDefaultPageSize(); + this.getEntries(true); + } + }); } // uses libraryService to get library all content - fetchEntries() { - this.libraryService.getAllEntries().subscribe({ + getEntries(forceFetch: boolean = false) { + this.libraryService.getAllEntries(forceFetch).subscribe({ next: (entries) => { this.entries = entries; this.filterEntries(); }, error: (error) => { - // Handle any errors, possibly informing the user. + console.log(`Couldn't get entries.`, error); + // TODO show notification }, }); } @@ -51,6 +60,12 @@ export class LibraryComponent { temp = this.entries; } + // ? sort filter + temp = temp.sort( + (a, b) => + new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime() + ); + const startIndex = (this.currentPage - 1) * this.pageSize; const endIndex = startIndex + this.pageSize;