Skip to content
Snippets Groups Projects
Commit 2721c9e9 authored by Etienne LOUPIAS's avatar Etienne LOUPIAS
Browse files

fix(posts): load more posts with tag (cf. MR !225 )

parent 3a4b521c
No related branches found
No related tags found
2 merge requests!244Dev,!243feat/US17-posts-pagination
...@@ -43,11 +43,14 @@ export class PostListComponent implements OnInit { ...@@ -43,11 +43,14 @@ export class PostListComponent implements OnInit {
...this.selectedLocationTagSlug, ...this.selectedLocationTagSlug,
...this.selectedPublicTagsSlug, ...this.selectedPublicTagsSlug,
]; ];
// Reset posts
this.resetPosts();
// Apply search // Apply search
this.getPosts(this.filters); this.getPosts(1, this.filters);
} else { } else {
// Init default news list // Init default news list
this.allPosts = []; this.allPosts = [];
this.filters = [];
this.postService.getPosts(1).subscribe((news) => { this.postService.getPosts(1).subscribe((news) => {
this.fillArticles(news); this.fillArticles(news);
}); });
...@@ -74,7 +77,7 @@ export class PostListComponent implements OnInit { ...@@ -74,7 +77,7 @@ export class PostListComponent implements OnInit {
this.allPosts = [...headLineTag, ..._.difference(this.allPosts, headLineTag)]; this.allPosts = [...headLineTag, ..._.difference(this.allPosts, headLineTag)];
} }
public getPosts(filters?: Tag[]): void { public getPosts(page: number, filters?: Tag[]): void {
// Parse filter // Parse filter
let parsedFilters = null; let parsedFilters = null;
if (filters) { if (filters) {
...@@ -87,11 +90,8 @@ export class PostListComponent implements OnInit { ...@@ -87,11 +90,8 @@ export class PostListComponent implements OnInit {
} }
} }
// Reset posts
this.resetPosts();
this.isLoading = true; this.isLoading = true;
this.postService.getPosts(1, parsedFilters).subscribe((news) => { this.postService.getPosts(page, parsedFilters).subscribe((news) => {
this.fillArticles(news); this.fillArticles(news);
}); });
} }
...@@ -125,11 +125,15 @@ export class PostListComponent implements OnInit { ...@@ -125,11 +125,15 @@ export class PostListComponent implements OnInit {
public loadMore(): void { public loadMore(): void {
if (this.pagination && this.pagination.page < this.pagination.pages) { if (this.pagination && this.pagination.page < this.pagination.pages) {
this.isLoading = true; this.isLoading = true;
if (this.filters) {
this.getPosts(this.pagination.next, this.filters);
} else {
this.postService.getPosts(this.pagination.next).subscribe((news) => { this.postService.getPosts(this.pagination.next).subscribe((news) => {
this.fillArticles(news); this.fillArticles(news);
}); });
} }
} }
}
// Split news on two columns on desktop mode or one column in mobile mode. // Split news on two columns on desktop mode or one column in mobile mode.
private setNews(news: PostWithMeta): void { private setNews(news: PostWithMeta): void {
... ...
......
...@@ -27,11 +27,9 @@ export class PostService { ...@@ -27,11 +27,9 @@ export class PostService {
// 12 posts to fit new design (1 + 3 + 2 + 1 + 3 + 2) // 12 posts to fit new design (1 + 3 + 2 + 1 + 3 + 2)
limit = '12'; limit = '12';
} }
if (!tags) {
return this.http let tagsFilter = '';
.get<PostWithMeta>(`${this.baseUrl}?page=${page}&include=tags,authors&limit=${limit}`) if (tags) {
.pipe(map((item: PostWithMeta) => new PostWithMeta(item)));
}
let tagsString = ''; let tagsString = '';
// Transform tab filters to string filters // Transform tab filters to string filters
tags.forEach((tag, index) => { tags.forEach((tag, index) => {
...@@ -40,11 +38,10 @@ export class PostService { ...@@ -40,11 +38,10 @@ export class PostService {
tagsString += '+tags:'; tagsString += '+tags:';
} }
}); });
return this.http tagsFilter = `&filter=tags:${encodeURIComponent(tagsString)}`;
.get<PostWithMeta>( }
`${this.baseUrl}?include=tags,authors&filter=tags:${encodeURIComponent(tagsString)}&limit=${limit}`
) return this.http.get<PostWithMeta>(`${this.baseUrl}?page=${page}&include=tags,authors${tagsFilter}&limit=${limit}`);
.pipe(map((item: PostWithMeta) => new PostWithMeta(item)));
} }
public getTags(): Observable<TagWithMeta> { public getTags(): Observable<TagWithMeta> {
... ...
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment