108 lines
2.8 KiB
TypeScript
108 lines
2.8 KiB
TypeScript
import { Component, OnInit } from '@angular/core';
|
|
import { Entry } from '../../../shared/models/entry.model';
|
|
import { TmdbService } from '../../../core/services/tmdb.service';
|
|
import { FormControl } from '@angular/forms';
|
|
import { debounceTime, distinctUntilChanged } from 'rxjs';
|
|
|
|
@Component({
|
|
selector: 'app-search-page',
|
|
templateUrl: './search-page.component.html',
|
|
styleUrls: ['./search-page.component.css'],
|
|
})
|
|
export class SearchPageComponent implements OnInit {
|
|
entryType: boolean = true;
|
|
entries: Entry[] = [];
|
|
isSearchLocked: boolean = false;
|
|
endPoint: string = '';
|
|
|
|
pages: Record<string, Record<number, Entry[]>> = {
|
|
true: {},
|
|
false: {},
|
|
};
|
|
|
|
currentPage: number = 1;
|
|
maxPage: number = 0;
|
|
totalEntries: number = 0;
|
|
searchControl = new FormControl();
|
|
|
|
constructor(private tmdbService: TmdbService) {}
|
|
|
|
ngOnInit() {
|
|
this.searchControl.valueChanges
|
|
.pipe(
|
|
debounceTime(300), // Delay the API call by 300 milliseconds
|
|
distinctUntilChanged() // Only emit when the current value is different from the last
|
|
)
|
|
.subscribe((value) => {
|
|
this.currentPage = 1;
|
|
this.doSearch(value);
|
|
// this.search(value); // Call your API search function here
|
|
});
|
|
}
|
|
|
|
setEntryType(entryType: boolean) {
|
|
this.entryType = entryType;
|
|
this.doSearch(this.searchControl.value);
|
|
}
|
|
|
|
// ? PAGINATION
|
|
goToPage(page: number) {
|
|
this.currentPage = page;
|
|
this.doSearch(this.searchControl.value);
|
|
}
|
|
|
|
get pageNumbers(): number[] {
|
|
this.currentPage = this.currentPage <= 0 ? 1 : this.currentPage; // page <= 0 fix
|
|
let startPage = this.currentPage <= 3 ? 1 : this.currentPage - 2;
|
|
let endPage = startPage + 4;
|
|
|
|
return Array.from(
|
|
{ length: endPage - startPage + 1 },
|
|
(_, i) => startPage + i
|
|
);
|
|
}
|
|
|
|
// ? EVENTS
|
|
// onInputChange(): void {
|
|
// return;
|
|
// const inputValue = this.searchInput;
|
|
// this.currentPage = 1;
|
|
|
|
// if (!this.isSearchLocked) {
|
|
// console.log(this.isSearchLocked);
|
|
// this.isSearchLocked = true;
|
|
|
|
// this.doSearch(inputValue);
|
|
|
|
// setTimeout(() => {
|
|
// this.isSearchLocked = false;
|
|
// }, 3000);
|
|
// }
|
|
// }
|
|
|
|
// onEnterPress() {
|
|
// this.currentPage = 1;
|
|
// this.doSearch(this.searchInput);
|
|
// }
|
|
|
|
// ? EVENT CATCHER
|
|
entryUpdated(event: Event) {
|
|
|
|
}
|
|
|
|
// ? FUNCTIONS
|
|
doSearch(keyword: string) {
|
|
keyword = keyword.toLowerCase().trim();
|
|
if (keyword.length <= 0) return;
|
|
|
|
let type = this.entryType ? 'movie' : 'tv';
|
|
this.tmdbService
|
|
.getByKeyword(keyword, type, this.currentPage)
|
|
.subscribe((result) => {
|
|
this.entries = result.entries as Entry[];
|
|
this.maxPage = result.total_pages;
|
|
this.totalEntries = result.total_results;
|
|
});
|
|
}
|
|
}
|