improved search component
This commit is contained in:
1 parent
60faa34363
commit
ff76e3900b
6 files changed
+64
-43
No files matched your search
@@ -0,0 +1,3 @@
|
||||
.ms-auto.d-flex .btn-group button {
|
||||
min-width: 100px;
|
||||
}
|
||||
@@ -12,25 +12,26 @@
|
||||
</ol>
|
||||
</nav>
|
||||
</div>
|
||||
<div class="ms-auto">
|
||||
<div class="input-group custom-group">
|
||||
<input type="text" placeholder="Name..." [(ngModel)]="searchInput" (ngModelChange)="onInputChange()"
|
||||
(keydown.enter)="onEnterPress()">
|
||||
|
||||
<div class="btn-group" role="group">
|
||||
<button type="button" class="btn btn-outline-dark left-btn" [class.active]="entryType === true"
|
||||
(click)="setEntryType(true)">Movies</button>
|
||||
|
||||
<button type="button" class="btn btn-outline-dark right-btn" [class.active]="entryType === false"
|
||||
(click)="setEntryType(false)">TV-Series</button>
|
||||
</div>
|
||||
<div class="ms-auto d-flex align-items-center">
|
||||
<div class="input-group me-3">
|
||||
<span class="input-group-text">Search</span>
|
||||
<input class="form-control" type="search" placeholder="Title..." [formControl]="searchControl">
|
||||
</div>
|
||||
|
||||
<div class="btn-group" role="group" style="align-self: center;">
|
||||
<button type="button" class="btn btn-outline-dark left-btn" [class.active]="entryType === true"
|
||||
(click)="setEntryType(true)">Movies</button>
|
||||
<button type="button" class="btn btn-outline-dark right-btn w-100" [class.active]="entryType === false"
|
||||
(click)="setEntryType(false)">TV-Series</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<!--end breadcrumb-->
|
||||
|
||||
<!-- Pagination -->
|
||||
<div class="pagination-container d-flex justify-content-end">
|
||||
<div class="pagination-container d-flex justify-content-end" *ngIf="maxPage > 1">
|
||||
<ul class="pagination pagination-sm">
|
||||
<li class="page-item" [class.disabled]="currentPage === 1">
|
||||
<a class="page-link" href="javascript:;" (click)="goToPage(currentPage - 1)">Previous</a>
|
||||
@@ -48,7 +49,7 @@
|
||||
<app-dynamic-entry-list [entries]="entries"></app-dynamic-entry-list>
|
||||
|
||||
<!-- Pagination -->
|
||||
<div class="pagination-container d-flex justify-content-end">
|
||||
<div class="pagination-container d-flex justify-content-end" *ngIf="maxPage > 1">
|
||||
<ul class="pagination pagination-sm">
|
||||
<li class="page-item" [class.disabled]="currentPage === 1">
|
||||
<a class="page-link" href="javascript:;" (click)="goToPage(currentPage - 1)">Previous</a>
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
import { Component } from '@angular/core';
|
||||
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',
|
||||
templateUrl: './search.component.html',
|
||||
styleUrls: ['./search.component.css'],
|
||||
})
|
||||
export class SearchComponent {
|
||||
export class SearchComponent implements OnInit {
|
||||
entryType: boolean = true;
|
||||
entries: Entry[] = [];
|
||||
isSearchLocked: boolean = false;
|
||||
@@ -17,23 +19,36 @@ export class SearchComponent {
|
||||
true: {},
|
||||
false: {},
|
||||
};
|
||||
currentPage: number = 1;
|
||||
|
||||
searchInput: string = '';
|
||||
currentPage: number = 1;
|
||||
maxPage: number = 0;
|
||||
totalEntries: number = 0;
|
||||
searchControl = new FormControl();
|
||||
|
||||
constructor(private tmdbService: TmdbService) {}
|
||||
|
||||
ngOnInit() {}
|
||||
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.searchInput);
|
||||
this.doSearch(this.searchControl.value);
|
||||
}
|
||||
|
||||
// ? PAGINATION
|
||||
goToPage(page: number) {
|
||||
this.currentPage = page;
|
||||
this.doSearch(this.searchInput);
|
||||
this.doSearch(this.searchControl.value);
|
||||
}
|
||||
|
||||
get pageNumbers(): number[] {
|
||||
@@ -48,26 +63,27 @@ export class SearchComponent {
|
||||
}
|
||||
|
||||
// ? EVENTS
|
||||
onInputChange(): void {
|
||||
const inputValue = this.searchInput;
|
||||
this.currentPage = 1;
|
||||
// onInputChange(): void {
|
||||
// return;
|
||||
// const inputValue = this.searchInput;
|
||||
// this.currentPage = 1;
|
||||
|
||||
if (!this.isSearchLocked) {
|
||||
console.log(this.isSearchLocked);
|
||||
this.isSearchLocked = true;
|
||||
// if (!this.isSearchLocked) {
|
||||
// console.log(this.isSearchLocked);
|
||||
// this.isSearchLocked = true;
|
||||
|
||||
this.doSearch(inputValue);
|
||||
// this.doSearch(inputValue);
|
||||
|
||||
setTimeout(() => {
|
||||
this.isSearchLocked = false;
|
||||
}, 3000);
|
||||
}
|
||||
}
|
||||
// setTimeout(() => {
|
||||
// this.isSearchLocked = false;
|
||||
// }, 3000);
|
||||
// }
|
||||
// }
|
||||
|
||||
onEnterPress() {
|
||||
this.currentPage = 1;
|
||||
this.doSearch(this.searchInput);
|
||||
}
|
||||
// onEnterPress() {
|
||||
// this.currentPage = 1;
|
||||
// this.doSearch(this.searchInput);
|
||||
// }
|
||||
|
||||
// ? FUNCTIONS
|
||||
doSearch(keyword: string) {
|
||||
@@ -77,8 +93,10 @@ export class SearchComponent {
|
||||
let type = this.entryType ? 'movie' : 'tv';
|
||||
this.tmdbService
|
||||
.getByKeyword(keyword, type, this.currentPage)
|
||||
.subscribe((myEntries) => {
|
||||
this.entries = myEntries;
|
||||
.subscribe((result) => {
|
||||
this.entries = result.entries as Entry[];
|
||||
this.maxPage = result.total_pages;
|
||||
this.totalEntries = result.total_results;
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user