forked from realworld-apps/angular-realworld-example-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharticle-list.component.ts
More file actions
98 lines (88 loc) · 2.79 KB
/
article-list.component.ts
File metadata and controls
98 lines (88 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { Component, DestroyRef, inject, Input } from "@angular/core";
import { ArticlesService } from "../services/articles.service";
import { ArticleListConfig } from "../models/article-list-config.model";
import { Article } from "../models/article.model";
import { ArticlePreviewComponent } from "./article-preview.component";
import { NgClass } from "@angular/common";
import { LoadingState } from "../../../core/models/loading-state.model";
import { takeUntilDestroyed } from "@angular/core/rxjs-interop";
@Component({
selector: "app-article-list",
template: `
@if (loading === LoadingState.LOADING) {
<div class="article-preview">Loading articles...</div>
}
@if (loading === LoadingState.LOADED) {
@for (article of results; track article.slug) {
<app-article-preview [article]="article" />
} @empty {
<div class="article-preview">No articles are here... yet.</div>
}
<nav>
<ul class="pagination">
@for (pageNumber of totalPages; track pageNumber) {
<li
class="page-item"
[ngClass]="{ active: pageNumber === currentPage }"
>
<button class="page-link" (click)="setPageTo(pageNumber)">
{{ pageNumber }}
</button>
</li>
}
</ul>
</nav>
}
`,
imports: [ArticlePreviewComponent, NgClass],
styles: `
.page-link {
cursor: pointer;
}
`,
})
export class ArticleListComponent {
query!: ArticleListConfig;
results: Article[] = [];
currentPage = 1;
totalPages: Array<number> = [];
loading = LoadingState.NOT_LOADED;
LoadingState = LoadingState;
destroyRef = inject(DestroyRef);
@Input() limit!: number;
@Input()
set config(config: ArticleListConfig) {
if (config) {
this.query = config;
this.currentPage = 1;
this.runQuery();
}
}
constructor(private articlesService: ArticlesService) {}
setPageTo(pageNumber: number) {
this.currentPage = pageNumber;
this.runQuery();
}
runQuery() {
this.loading = LoadingState.LOADING;
this.results = [];
// Create limit and offset filter (if necessary)
if (this.limit) {
this.query.filters.limit = this.limit;
this.query.filters.offset = this.limit * (this.currentPage - 1);
}
this.articlesService
.query(this.query)
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe((data) => {
this.loading = LoadingState.LOADED;
this.results = data.articles;
console.log("adding a console log here");
// Used from http://www.jstips.co/en/create-range-0...n-easily-using-one-line/
this.totalPages = Array.from(
new Array(Math.ceil(data.articlesCount / this.limit)),
(val, index) => index + 1,
);
});
}
}