feat: started popular titles on main page
This commit is contained in:
@@ -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();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user