Skip to content
This repository was archived by the owner on Feb 5, 2022. It is now read-only.

Commit 4f1eef1

Browse files
author
“Young
committed
Bug(): Blogs don't display the featured Post again on the page.
1 parent 38ba1ae commit 4f1eef1

File tree

1 file changed

+40
-8
lines changed

1 file changed

+40
-8
lines changed

src/pages/app-blog/app-blog.tsx

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ export class AppBlog {
3333
@State() searchIsLoading: boolean = false;
3434

3535
pageSize = 3;
36+
indexOfFeaturedPost = -1;
37+
pageOfFeaturedPost = 0;
3638
private filters: BlogCategory[] = [
3739
{
3840
name: 'All',
@@ -83,8 +85,19 @@ export class AppBlog {
8385
this.allBlogPosts = resp.data;
8486
this.blogMeta = resp.meta;
8587
this.blogNumberOfPages = Math.ceil(resp.meta.count / this.pageSize);
86-
this.getBlogPosts(1);
88+
8789
this.getFeaturedPost();
90+
91+
// Find the index of the featuredPost.
92+
this.indexOfFeaturedPost = this.allBlogPosts.findIndex(post => {
93+
return (post.title == this.featuredPost.title && post.published == this.featuredPost.published);
94+
});
95+
// Find the page where the featuredPost is if found the featuredPost
96+
if (this.indexOfFeaturedPost > -1) {
97+
this.pageOfFeaturedPost = Math.floor(this.indexOfFeaturedPost / this.pageSize) + 1;
98+
}
99+
100+
this.getBlogPosts(1);
88101
}
89102
this.blogIsLoading = false;
90103
}
@@ -99,12 +112,19 @@ export class AppBlog {
99112

100113
getSearchPosts(page) {
101114
this.searchIsLoading = true;
102-
const pageSize = 3;
103-
Fetch.fetchSearchPosts(this.searchQuery, page, pageSize).then(resp => {
115+
Fetch.fetchSearchPosts(this.searchQuery, page, this.pageSize).then(resp => {
104116
if (resp.data) {
105117
this.searchPostsData = resp.data;
106118
this.searchMeta = resp.meta;
107-
this.searchNumberOfPages = Math.ceil(resp.meta.count / pageSize);
119+
120+
// Find the index of the featuredPost from the searhchPostsData
121+
const index = this.searchPostsData.findIndex(post => {
122+
return (post.title == this.featuredPost.title && post.published == this.featuredPost.published);
123+
});
124+
// If found it, remove it from the searchPostsData to avoid display again.
125+
if (index >= 0) this.searchPostsData.splice(index, 1);
126+
127+
this.searchNumberOfPages = Math.ceil(this.searchPostsData.length / this.pageSize);
108128
this.searchCurrentPage = page;
109129
} else {
110130
this.searchIsError = true;
@@ -117,15 +137,29 @@ export class AppBlog {
117137
this.blogIsLoading = true;
118138
if (this.blogFilter) {
119139
this.blogPostsData = await Fetch.fetchFilteredPosts(this.blogFilter, 1, this.pageSize, true);
140+
141+
// Find the index of the featuredPost from the blogPostsData
142+
const index = this.blogPostsData.findIndex(post => {
143+
return (post.title == this.featuredPost.title && post.published == this.featuredPost.published);
144+
});
145+
// If found it, remove it from the blogPostsData to avoid display again.
146+
if (index >= 0) this.blogPostsData.splice(index, 1);
147+
120148
this.blogNumberOfPages = Math.ceil(this.blogPostsData.length / this.pageSize);
121149
this.blogCurrentPage = 1;
122150
} else {
123151
this.blogNumberOfPages = Math.ceil(this.allBlogPosts.length / this.pageSize);
124152
this.blogPostsData = [];
125153
let index = (page - 1) * this.pageSize;
126-
const endPoint = Math.min(this.allBlogPosts.length, page * this.pageSize);
154+
let endPoint = Math.min(this.allBlogPosts.length, page * this.pageSize);
155+
156+
// Adjust the index and the endPoint by the index of the featuredPost
157+
if (page > this.pageOfFeaturedPost) index++;
158+
if (page == this.pageOfFeaturedPost) endPoint++;
159+
127160
for (index; index < endPoint; index++) {
128-
this.blogPostsData.push(this.allBlogPosts[index]);
161+
// Don't push to the blogPostsData if it is the featuredPost.
162+
if (index != this.indexOfFeaturedPost) this.blogPostsData.push(this.allBlogPosts[index]);
129163
}
130164
}
131165
this.blogIsLoading = false;
@@ -152,8 +186,6 @@ export class AppBlog {
152186
if (!this.searchQuery && this.blogFilter !== filterName) {
153187
this.blogFilter = filterName;
154188
this.getBlogPosts(1);
155-
// if (this.blogFilter) this.getBlogPosts(1);
156-
// else this.getAllBlogPosts();
157189
}
158190
}
159191

0 commit comments

Comments
 (0)