add to library from search popup

This commit is contained in:
X4Lo committed 2024-09-10 19:03:30 +01:00
1 parent 2b2cc44851
commit 9d89eda74c
3 files changed
+69 -16

No files matched your search

@@ -20,25 +20,36 @@
<hr> <hr>
</ng-container> </ng-container>
<ng-container *ngIf="searchedEntries.length != 0">
<p class="search-title">Movies & Tv-Series</p> <p class="search-title">Movies & Tv-Series</p>
<div class="search-list d-flex flex-column gap-0"> <div class="search-list d-flex flex-column gap-0">
<div class="search-list-item d-flex align-items-center gap-3" *ngFor="let entry of searchedEntries" (click)="lineClick(entry)"> <div class="search-list-item d-flex align-items-center gap-3" *ngFor="let entry of searchedEntries"
(click)="lineClick(entry)">
<div class="memmber-img"> <div class="memmber-img">
<img [src]="'https://image.tmdb.org/t/p/w92/' + entry.poster_path" width="40" height="60" <img [src]="'https://image.tmdb.org/t/p/w92/' + entry.poster_path" width="40" height="60" alt="">
alt="">
</div> </div>
<div class="d-flex flex-column"> <div class="d-flex flex-column">
<div class="d-flex align-items-center"> <div class="d-flex align-items-center">
<h5 class="mb-0 search-list-title">{{ entry.title }}</h5> <h5 class="mb-0 search-list-title">{{ entry.title }}</h5>
<div class="circle ms-2 me-2"></div> <div class="circle ms-2 me-2"></div>
<p class="mb-0 text-secondary" style="font-size: 13px; margin-top: 2px;"> <p class="mb-0 text-secondary" style="font-size: 13px; margin-top: 2px;">
{{ entry.release_date }} {{ entry.release_date | dateFormat }}
</p> </p>
</div> </div>
</div> </div>
<button class="btn btn-primary ms-auto" *ngIf="!entry.isInLibrary"
(click)="addToLibrary(entry.entryId.tmdb_id, entry.is_tvseries); $event.stopPropagation()">
<i class="material-icons-outlined fs-5">add_circle</i>
</button>
</div> </div>
</div>
</ng-container>
<ng-container *ngIf="searchedEntries.length == 0">
<p class="text-muted text-center mt-4">Please search for movies and TV series.</p>
</ng-container>
</div>
<!-- <p class="search-title">Tutorials</p> <!-- <p class="search-title">Tutorials</p>
<div class="search-list d-flex flex-column gap-2"> <div class="search-list d-flex flex-column gap-2">
@@ -72,7 +83,7 @@
<hr> --> <hr> -->
</div> </div>
<div class="card-footer text-center bg-transparent"> <div class="card-footer text-center bg-transparent" *ngIf="searchedEntries.length != 0">
<a href="javascript:;" class="btn w-100">See All Search Results</a> <a href="javascript:;" class="btn w-100" (click)="doShowSearchPage()">See All Search Results</a>
</div> </div>
</div> </div>
@@ -2,7 +2,6 @@ import {
AfterViewInit, AfterViewInit,
Component, Component,
ElementRef, ElementRef,
Input,
OnInit, OnInit,
ViewChild, ViewChild,
} from '@angular/core'; } from '@angular/core';
@@ -26,6 +25,8 @@ export class SearchPopupComponent implements OnInit, AfterViewInit {
searchedEntries: Entry[] = [] as Entry[]; searchedEntries: Entry[] = [] as Entry[];
recentSearches: SearchItem[] = [] as SearchItem[]; recentSearches: SearchItem[] = [] as SearchItem[];
isAddLocked: boolean = false;
@ViewChild('scrollContainer', { static: true }) scrollContainer!: ElementRef; @ViewChild('scrollContainer', { static: true }) scrollContainer!: ElementRef;
private ps!: PerfectScrollbar; private ps!: PerfectScrollbar;
@@ -37,6 +38,14 @@ export class SearchPopupComponent implements OnInit, AfterViewInit {
) {} ) {}
ngOnInit(): void { ngOnInit(): void {
this.getSearchHisotry();
}
ngAfterViewInit(): void {
this.ps = new PerfectScrollbar(this.scrollContainer.nativeElement);
}
getSearchHisotry() {
this.historyService.getSearchHistory().subscribe({ this.historyService.getSearchHistory().subscribe({
next: (result) => { next: (result) => {
this.recentSearches = [ this.recentSearches = [
@@ -56,10 +65,7 @@ export class SearchPopupComponent implements OnInit, AfterViewInit {
}); });
} }
ngAfterViewInit(): void { // ! search value is passed from parent by calling this function
this.ps = new PerfectScrollbar(this.scrollContainer.nativeElement);
}
triggerSearch(searchTerm: string) { triggerSearch(searchTerm: string) {
this.searchTerm = searchTerm; this.searchTerm = searchTerm;
this.searchedEntries = [] as Entry[]; this.searchedEntries = [] as Entry[];
@@ -93,4 +99,37 @@ export class SearchPopupComponent implements OnInit, AfterViewInit {
error: (error) => {}, error: (error) => {},
}); });
} }
addToLibrary(tmdbId: string, isTvSerie: boolean) {
this.isAddLocked = true;
let obs = this.libraryService.addEntry(tmdbId, isTvSerie).subscribe({
next: (entry) => {
if (entry != null) {
entry.isInLibrary = true;
this.libraryService.refreshEntries().subscribe();
this.searchedEntries.forEach((e) => {
if (e.entryId.tmdb_id == entry.entryId.tmdb_id) {
e.isInLibrary = true;
}
});
this.isAddLocked = false;
obs.unsubscribe();
}
},
error: (error) => {
// TODO error notification
console.error('Error adding entry:', error);
this.isAddLocked = false;
obs.unsubscribe();
},
});
}
doShowSearchPage() {
if (this.searchTerm) {
this.router.navigate(['/search'], {
queryParams: { title: this.searchTerm },
});
}
}
} }
@@ -17,12 +17,14 @@ export class LibraryService implements OnInit {
private backend_apiUrl = environment.API_URL; private backend_apiUrl = environment.API_URL;
private apiAction = 'entries'; private apiAction = 'entries';
constructor(private http: HttpClient) {} constructor(private http: HttpClient) {
this.fetchAllEntries().subscribe(() => {
ngOnInit() { console.log(`Library Service: Loaded ${this.entries.length} entry.`);
this.fetchAllEntries(); });
} }
ngOnInit() {}
getAllEntries(forceFetch: boolean = false): Observable<Entry[]> { getAllEntries(forceFetch: boolean = false): Observable<Entry[]> {
if (!forceFetch && this.entriesLoaded) { if (!forceFetch && this.entriesLoaded) {
return of(this.entries); return of(this.entries);
@@ -169,6 +171,7 @@ export class LibraryService implements OnInit {
// ); // );
// } // }
public isInLibrary(tmdb_id: string): Observable<Entry | null> { public isInLibrary(tmdb_id: string): Observable<Entry | null> {
console.log(`is in library called`);
const existingEntry = this.entries.find( const existingEntry = this.entries.find(
(entry) => entry.entryId.tmdb_id == tmdb_id (entry) => entry.entryId.tmdb_id == tmdb_id
); );