feat: started popular titles on main page

This commit is contained in:
2024-07-20 00:59:00 +03:00
parent 2becf42487
commit 6b209077ef
10 changed files with 180 additions and 11 deletions

View File

@@ -1,4 +1,12 @@
<h1>It's home component</h1>
@if (!search) {
@for (popular of popularItems; track popular[0]) {
<p class="text-5xl">{{ popular[0] }}</p>
@for (popularTitle of popular[1]; track popularTitle.id) {
<p>{{ popularTitle.rus_name }}</p>
}
}
}
<div class="flex flex-col items-center w-full px-3">
@if (loading) {
<h1>Loading...</h1>

View File

@@ -1,7 +1,8 @@
import { CommonModule } from "@angular/common";
import { Component, Input, OnDestroy, OnInit } from "@angular/core";
import { ActivatedRoute, RouterLink } from "@angular/router";
import { Subject, takeUntil } from "rxjs";
import { map, Subject, Subscription, takeUntil } from "rxjs";
import { IRuLIBPopular } from "../../services/parsers/rulib/rulib.popular.dto";
import { Datum } from "../../services/parsers/rulib/rulib.search.dto";
import { SearchService } from "../../services/search.service";
@@ -16,33 +17,73 @@ export class HomeComponent implements OnDestroy, OnInit {
@Input() items: Datum[] = [];
loading = false;
notFound = false;
search: string = "";
popularItemsMap: Map<string, IRuLIBPopular[]> = new Map<string, IRuLIBPopular[]>();
private destroy$ = new Subject<void>();
private popularSubscription = new Subscription();
constructor(
private searchService: SearchService,
private route: ActivatedRoute,
) {}
) {
this.searchService.setMangalibParser();
}
ngOnDestroy(): void {
this.destroy$.next();
this.destroy$.complete();
this.popularSubscription.unsubscribe();
}
private getPopular() {
if (!this.popularSubscription.closed) this.popularSubscription.unsubscribe();
this.popularSubscription = this.searchService
.getPopular()
.pipe(map((data) => data as IRuLIBPopular[]))
.subscribe({
next: (data) => {
this.popularItemsMap.set(this.searchService.getRuLibName(), data);
console.log(this.popularItemsMap);
},
error: (error) => {
console.error(error);
},
});
}
getPopularMangalib() {
this.searchService.setMangalibParser();
this.getPopular();
}
getPopularSlashlib() {
this.searchService.setSlashlibParser();
this.getPopular();
}
get popularItems() {
return this.popularItemsMap.entries();
}
ngOnInit(): void {
this.route.queryParams.subscribe((params) => {
const search = params["search"];
if (search) {
this.searchService.setMangalibParser();
this.route.queryParams.pipe(takeUntil(this.destroy$)).subscribe((params) => {
this.search = params["search"] ?? "";
this.items = [];
if (this.search) {
this.loading = true;
this.searchService.setMangalibParser();
this.searchService
.search(search)
.search(this.search)
.pipe(takeUntil(this.destroy$))
.subscribe((data) => {
this.loading = false;
this.items = data.data;
this.notFound = this.items.length === 0;
});
} else {
this.getPopularMangalib();
this.getPopularSlashlib();
}
});
}