feat: search pages

This commit is contained in:
2024-07-22 19:23:25 +03:00
parent 3522df4066
commit 8de238028e
2 changed files with 63 additions and 9 deletions

View File

@@ -47,7 +47,7 @@
<div <div
class="flex md:flex-row flex-col flex-nowrap md:flex-wrap gap-2 justify-start items-center w-full px-3 mt-3 md:mx-12" class="flex md:flex-row flex-col flex-nowrap md:flex-wrap gap-2 justify-start items-center w-full px-3 mt-3 md:mx-12"
> >
@for (item of items; track $index) { @for (item of currentPageItems; track $index) {
<a <a
routerLink="/detail" routerLink="/detail"
[queryParams]="{ url: item.slug_url }" [queryParams]="{ url: item.slug_url }"
@@ -71,9 +71,31 @@
</a> </a>
} }
</div> </div>
<div class="flex flex-row items-center justify-between"> @if (items.length) {
<button *ngIf="currentPage > 0">Назад</button> <div class="flex flex-row items-center justify-between w-full px-3">
<button *ngIf="currentPage < items.length / 8 - 1">Вперед</button> <button
</div> [ngClass]="{
'bg-slate-600 text-white p-3 rounded-md hover:scale-105': 1,
'opacity-15': currentPage === 0,
}"
[disabled]="currentPage === 0"
(click)="previousSearchPage()"
>
Назад
</button>
<p>Страница {{ currentPage + 1 }} из {{ maxPages + 1 }}</p>
<button
[ngClass]="{
'bg-slate-600 text-white p-3 rounded-md hover:scale-105': 1,
'opacity-15': currentPage === maxPages,
}"
[disabled]="currentPage === maxPages"
*ngIf="currentPage < items.length / 8"
(click)="nextSearchPage()"
>
Вперед
</button>
</div>
}
</div> </div>
} }

View File

@@ -1,7 +1,7 @@
import { CommonModule } from "@angular/common"; import { CommonModule } from "@angular/common";
import { Component, Input, OnDestroy, OnInit } from "@angular/core"; import { Component, Input, OnDestroy, OnInit } from "@angular/core";
import { ActivatedRoute, RouterLink } from "@angular/router"; import { ActivatedRoute, Router, RouterLink } from "@angular/router";
import { map, Subject, Subscription, takeUntil } from "rxjs"; import { Subject, Subscription, map, takeUntil } from "rxjs";
import { RulibAuthService } from "../../services/parsers/rulib/rulib.auth.service"; import { RulibAuthService } from "../../services/parsers/rulib/rulib.auth.service";
import { IRuLIBPopular } from "../../services/parsers/rulib/rulib.popular.dto"; import { IRuLIBPopular } from "../../services/parsers/rulib/rulib.popular.dto";
import { Datum } from "../../services/parsers/rulib/rulib.search.dto"; import { Datum } from "../../services/parsers/rulib/rulib.search.dto";
@@ -29,6 +29,7 @@ export class HomeComponent implements OnDestroy, OnInit {
private searchService: SearchService, private searchService: SearchService,
private route: ActivatedRoute, private route: ActivatedRoute,
private ruLibAuthService: RulibAuthService, private ruLibAuthService: RulibAuthService,
private router: Router,
) { ) {
this.searchService.setMangalibParser(); this.searchService.setMangalibParser();
} }
@@ -77,11 +78,41 @@ export class HomeComponent implements OnDestroy, OnInit {
return Array.from(this.popularItemsMap.entries()).sort((a, b) => (a[0] > b[0] ? 1 : -1)); return Array.from(this.popularItemsMap.entries()).sort((a, b) => (a[0] > b[0] ? 1 : -1));
} }
get currentPageItems() {
return this.items.slice(
8 * this.currentPage,
Math.min(this.items.length, 8 * (this.currentPage + 1)),
);
}
get maxPages() {
return Math.ceil(this.items.length / 8) - 1;
}
nextSearchPage() {
this.currentPage = Math.min(this.currentPage + 1, Math.ceil(this.items.length / 8) - 1);
this.router.navigate([], {
queryParams: { page: this.currentPage },
relativeTo: this.route,
queryParamsHandling: "merge",
});
}
previousSearchPage() {
this.currentPage = Math.max(this.currentPage - 1, 0);
this.router.navigate([], {
queryParams: { page: this.currentPage },
relativeTo: this.route,
queryParamsHandling: "merge",
});
}
ngOnInit(): void { ngOnInit(): void {
this.route.queryParams.pipe(takeUntil(this.destroy$)).subscribe((params) => { this.route.queryParams.pipe(takeUntil(this.destroy$)).subscribe((params) => {
if (this.search && params["search"] && this.search === params["search"]) return;
this.search = params["search"] ?? ""; this.search = params["search"] ?? "";
try { try {
this.currentPage = +params["page"] ?? 0; this.currentPage = Math.max(0, +params["page"] || 0);
} catch { } catch {
this.currentPage = 0; this.currentPage = 0;
} }
@@ -95,7 +126,8 @@ export class HomeComponent implements OnDestroy, OnInit {
.subscribe((data) => { .subscribe((data) => {
this.loading = false; this.loading = false;
this.items = data.data; this.items = data.data;
const maxPage = Math.floor(this.items.length / 8) - 1; const maxPage = Math.ceil(this.items.length / 8) - 1;
console.log(maxPage);
this.currentPage = Math.min(this.currentPage, maxPage); this.currentPage = Math.min(this.currentPage, maxPage);
this.notFound = this.items.length === 0; this.notFound = this.items.length === 0;
}); });