diff --git a/frontend/src/app/core/services/library.service.ts b/frontend/src/app/core/services/library.service.ts index 26b2804..1aaff56 100644 --- a/frontend/src/app/core/services/library.service.ts +++ b/frontend/src/app/core/services/library.service.ts @@ -79,25 +79,24 @@ export class LibraryService implements OnInit { const existingEntry = this.entriesSubject .getValue() .find((entry) => entry.entryId?.tmdb_id == tmdb_id); - if (existingEntry) { - return of(existingEntry); - } else { - return this.http - .post(`${this.backend_apiUrl}/${this.apiAction}`, { - tmdb_id: tmdb_id, - is_tvserie: is_tvserie, + + if (existingEntry) return of(existingEntry); + + return this.http + .post(`${this.backend_apiUrl}/${this.apiAction}`, { + tmdb_id: tmdb_id, + is_tvserie: is_tvserie, + }) + .pipe( + tap((newEntry) => { + newEntry.isInLibrary = true; + + const currentEntries = this.entriesSubject.getValue(); + const updatedEntries = [...currentEntries, newEntry]; + + this.entriesSubject.next(updatedEntries); }) - .pipe( - tap((newEntry) => { - newEntry.isInLibrary = true; - - const currentEntries = this.entriesSubject.getValue(); - const updatedEntries = [...currentEntries, newEntry]; - - this.entriesSubject.next(updatedEntries); - }) // Add new entry to the list - ); - } + ); } public deleteEntry(id: number): Observable { @@ -115,11 +114,13 @@ export class LibraryService implements OnInit { ); } - public setEntry(entry: Entry) { + public updateEntry(entry: Entry) { const currentEntries = this.entriesSubject.getValue(); - let updatedEntries = currentEntries.filter((e) => e.id != entry.id); - updatedEntries.push(entry); + const updatedEntries = [ + ...currentEntries.filter((e) => e.id !== entry.id), + entry, + ]; this.entriesSubject.next(updatedEntries); } diff --git a/frontend/src/app/features/entry/alternate-entry-page/alternate-entry-page.component.ts b/frontend/src/app/features/entry/alternate-entry-page/alternate-entry-page.component.ts index dd0dcb6..a7aea50 100644 --- a/frontend/src/app/features/entry/alternate-entry-page/alternate-entry-page.component.ts +++ b/frontend/src/app/features/entry/alternate-entry-page/alternate-entry-page.component.ts @@ -146,7 +146,7 @@ export class AlternateEntryPageComponent implements OnInit { if (entryRatings) { // TODO show notification this.entry.entryRating = entryRatings; - this.libraryService.setEntry(this.entry); + this.libraryService.updateEntry(this.entry); } }, error: (error) => { @@ -218,7 +218,7 @@ export class AlternateEntryPageComponent implements OnInit { // ? update the watchlist page if (this.entry.watchlist) { this.entry.watchlist = false; - this.libraryService.setEntry(this.entry); + this.libraryService.updateEntry(this.entry); // this.libraryService.refetchEntries().subscribe(); } @@ -287,7 +287,7 @@ export class AlternateEntryPageComponent implements OnInit { // console.log(`Entry updated: `, entry); this.entry = entry; this.entry.isInLibrary = true; - this.libraryService.setEntry(this.entry); + this.libraryService.updateEntry(this.entry); }, error: (error) => { console.log(`Error updating entry: `, error); @@ -310,7 +310,7 @@ export class AlternateEntryPageComponent implements OnInit { // console.log(`Entry updated: `, entry); this.entry = entry; this.entry.isInLibrary = true; - this.libraryService.setEntry(this.entry); + this.libraryService.updateEntry(this.entry); }, error: (error) => { console.log(`Error updating entry: `, error); @@ -740,7 +740,7 @@ export class AlternateEntryPageComponent implements OnInit { this.entry.showInCalendar = result.entry.showInCalendar; this.entry.autoSync = result.entry.autoSync; - this.libraryService.setEntry(this.entry); + this.libraryService.updateEntry(this.entry); } }); } diff --git a/frontend/src/app/features/entry/entry-page/entry-page.component.ts b/frontend/src/app/features/entry/entry-page/entry-page.component.ts index 91782e9..1b51aad 100644 --- a/frontend/src/app/features/entry/entry-page/entry-page.component.ts +++ b/frontend/src/app/features/entry/entry-page/entry-page.component.ts @@ -79,16 +79,16 @@ export class EntryPageComponent implements OnInit { this.entry.collections = this.entry.collections.map((c) => { c.mediasCount = c.medias.length; c.selectedBackdrop = this.getCollectionBackdropPath(c); - - c.medias = []; return c; }); if (this.entry.is_tvseries) { - // ? sorting seasons + // ! sorting seasons this.entry.seasons = this.entry.seasons.sort((a, b) => { - if (b.season_number == 0) b.season_number = 30; // this will make the special season go last - if (a.season_number == 0) a.season_number = 30; // this will make the special season go last + // ? by changing the season number of 'special' season to 999 + // ? it will show up as the last season. + if (b.season_number == 0) b.season_number = 999; // this will make the special season go last + if (a.season_number == 0) a.season_number = 999; // this will make the special season go last return a.season_number - b.season_number; }); @@ -145,7 +145,7 @@ export class EntryPageComponent implements OnInit { if (entryRatings) { // TODO show notification this.entry.entryRating = entryRatings; - this.libraryService.setEntry(this.entry); + this.libraryService.updateEntry(this.entry); } }, error: (error) => { @@ -179,6 +179,7 @@ export class EntryPageComponent implements OnInit { let watchDate = result.watchDate; let watchedWith = result.watchedWith.split('\n').join(', '); + // * Update if (entrySeen.id) { const es = { id: entrySeen.id, @@ -186,15 +187,25 @@ export class EntryPageComponent implements OnInit { seen_with: watchedWith, } as EntrySeen; - // * Update this.entrySeenService.updateEntrySeen(es).subscribe({ next: (updatedEntrySeen) => { - if (updatedEntrySeen) { - // TODO show confirm notification - // ? replace the entry-seen object with the updated one - for (let i = 0; i < this.entry.entrySeen.length; i++) { - if (this.entry.entrySeen[i].id == updatedEntrySeen.id) - this.entry.entrySeen[i] = updatedEntrySeen; + // TODO show confirm notification + // ? replace the entry-seen object with the updated one + for (let i = 0; i < this.entry.entrySeen.length; i++) { + if (this.entry.entrySeen[i].id == updatedEntrySeen.id) { + this.entry.entrySeen[i] = updatedEntrySeen; + + // ? update enrty last_seen & first_seen dates + if (this.entry.date_last_seen < entrySeen.date_seen) { + this.entry.date_last_seen = entrySeen.date_seen; + } + + if (this.entry.date_first_seen > entrySeen.date_seen) { + this.entry.date_first_seen = entrySeen.date_seen; + } + + this.libraryService.updateEntry(this.entry); + break; } } }, @@ -215,12 +226,25 @@ export class EntryPageComponent implements OnInit { // ? if the entry was in watch list creating and entry-seen would have removed it // ? from the watchlist, so now we need to refrech libraryService in order to // ? update the watchlist page - if (this.entry.watchlist) { - this.entry.watchlist = false; - this.libraryService.setEntry(this.entry); + this.entry.isInLibrary = true; + this.entry.watchlist = false; - // this.libraryService.refetchEntries().subscribe(); + // ? update enrty last_seen & first_seen dates + if ( + !this.entry.date_last_seen || + this.entry.date_last_seen < entrySeen.date_seen + ) { + this.entry.date_last_seen = entrySeen.date_seen; } + + if ( + !this.entry.date_first_seen || + this.entry.date_first_seen > entrySeen.date_seen + ) { + this.entry.date_first_seen = entrySeen.date_seen; + } + + this.libraryService.updateEntry(this.entry); } }, error: (error) => { @@ -286,7 +310,7 @@ export class EntryPageComponent implements OnInit { // console.log(`Entry updated: `, entry); this.entry.favorit = entry.favorit; this.entry.isInLibrary = true; - this.libraryService.setEntry(this.entry); + this.libraryService.updateEntry(this.entry); }, error: (error) => { console.log(`Error updating entry: `, error); @@ -308,7 +332,7 @@ export class EntryPageComponent implements OnInit { next: (entry) => { this.entry.hidden = entry.hidden; this.entry.isInLibrary = true; - this.libraryService.setEntry(this.entry); + this.libraryService.updateEntry(this.entry); }, error: (error) => { console.log(`Error updating entry: `, error); @@ -337,11 +361,10 @@ export class EntryPageComponent implements OnInit { this.entry.collections = result.collections.map((c) => { c.mediasCount = c.medias.length; c.selectedBackdrop = this.getCollectionBackdropPath(c); - - c.medias = []; return c; }); - result.collections; + + this.libraryService.updateEntry(this.entry); }, }); }); @@ -738,7 +761,7 @@ export class EntryPageComponent implements OnInit { this.entry.showInCalendar = result.entry.showInCalendar; this.entry.autoSync = result.entry.autoSync; - this.libraryService.setEntry(this.entry); + this.libraryService.updateEntry(this.entry); } }); } diff --git a/frontend/src/app/features/library/library-page/library-page.component.ts b/frontend/src/app/features/library/library-page/library-page.component.ts index 7db2a1b..36e2684 100644 --- a/frontend/src/app/features/library/library-page/library-page.component.ts +++ b/frontend/src/app/features/library/library-page/library-page.component.ts @@ -92,8 +92,8 @@ export class LibraryPageComponent implements OnInit, AfterViewInit { async ngOnInit() { this.getDefaultViewMode(); - this.pageSize = await this.getDefaultTableSize(); - await this.getEntries(true); + this.pageSize = this.getDefaultTableSize(); + this.getEntries(); // this.router.events.subscribe((event) => { // if (event instanceof NavigationEnd && event.url === '/library') { // this.pageSize = this.getDefaultPageSize(); @@ -139,7 +139,7 @@ export class LibraryPageComponent implements OnInit, AfterViewInit { } // ? uses libraryService to get library all entries - getEntries(forceFetch: boolean = false) { + getEntries() { this.libraryService.newGetAllEntries().subscribe({ next: (entries) => { this.entries = entries.filter((e) => !e.watchlist);