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

fix(posts): load more posts with tag

parent 916739f9
No related branches found
No related tags found
2 merge requests!229feat(data-consent): add data sharing consent when creating and editing a...,!225fix(posts): load more posts with tag
......@@ -56,11 +56,14 @@ export class PostListComponent implements OnInit {
...this.selectedLocationTagSlug,
...this.selectedPublicTagsSlug,
];
// Reset posts
this.resetPosts();
// Apply search
this.getPosts(this.filters);
this.getPosts(1, this.filters);
} else {
// Init default news list
this.allPosts = [];
this.filters = [];
this.postService.getPosts(1).subscribe((news) => {
this.fillArticles(news);
});
......@@ -84,7 +87,7 @@ export class PostListComponent implements OnInit {
this.allPosts = [...headLineTag, ..._.difference(this.allPosts, headLineTag)];
}
public getPosts(filters?: Tag[]): void {
public getPosts(page: number, filters?: Tag[]): void {
// Parse filter
let parsedFilters = null;
if (filters) {
......@@ -97,11 +100,8 @@ export class PostListComponent implements OnInit {
}
}
// Reset posts
this.resetPosts();
this.isLoading = true;
this.postService.getPosts(1, parsedFilters).subscribe((news) => {
this.postService.getPosts(page, parsedFilters).subscribe((news) => {
this.fillArticles(news);
});
}
......@@ -135,9 +135,13 @@ export class PostListComponent implements OnInit {
private loadMore(): void {
if (this.pagination && this.pagination.page < this.pagination.pages) {
this.isLoading = true;
this.postService.getPosts(this.pagination.next).subscribe((news) => {
this.fillArticles(news);
});
if (this.filters) {
this.getPosts(this.pagination.next, this.filters);
} else {
this.postService.getPosts(this.pagination.next).subscribe((news) => {
this.fillArticles(news);
});
}
}
}
......
......@@ -23,22 +23,21 @@ export class PostService {
}
public getPosts(page: number, tags?: string[]): Observable<PostWithMeta> {
if (!tags) {
return this.http
.get<PostWithMeta>(`${this.baseUrl}?page=${page}&include=tags,authors`)
.pipe(map((item: PostWithMeta) => new PostWithMeta(item)));
let tagsFilter = '';
if (tags) {
let tagsString = '';
// Transform tab filters to string filters
tags.forEach((tag, index) => {
tagsString += tag;
if (index != tags.length - 1) {
tagsString += '+tags:';
}
});
tagsFilter = `&filter=tags:${encodeURIComponent(tagsString)}`;
}
let tagsString = '';
// Transform tab filters to string filters
tags.forEach((tag, index) => {
tagsString += tag;
if (index != tags.length - 1) {
tagsString += '+tags:';
}
});
return this.http
.get<PostWithMeta>(`${this.baseUrl}?include=tags,authors&filter=tags:${encodeURIComponent(tagsString)}`)
.pipe(map((item: PostWithMeta) => new PostWithMeta(item)));
return this.http.get<PostWithMeta>(`${this.baseUrl}?page=${page}&include=tags,authors${tagsFilter}`);
}
public getTags(): Observable<TagWithMeta> {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment