1- import { Component , State , h } from '@stencil/core' ;
1+ import { Component , State , h , Build } from '@stencil/core' ;
22import { BlogPost } from '../../model/blog-post.model' ;
33import { BlogMeta } from '../../model/blog-meta.model' ;
44import { BlogCategory } from '../../model/blog-category.model' ;
@@ -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' ,
@@ -61,6 +63,9 @@ export class AppBlog {
6163 }
6264
6365 componentDidLoad ( ) {
66+ if ( Build . isBrowser ) {
67+ window . scrollTo ( 0 , 0 ) ;
68+ }
6469 const input = document . getElementById ( 'blog-search' ) ;
6570 input . addEventListener ( 'search' , ( ) => this . handleSearch ( input . innerText ) ) ;
6671
@@ -80,8 +85,19 @@ export class AppBlog {
8085 this . allBlogPosts = resp . data ;
8186 this . blogMeta = resp . meta ;
8287 this . blogNumberOfPages = Math . ceil ( resp . meta . count / this . pageSize ) ;
83- this . getBlogPosts ( 1 ) ;
88+
8489 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 ) ;
85101 }
86102 this . blogIsLoading = false ;
87103 }
@@ -96,12 +112,19 @@ export class AppBlog {
96112
97113 getSearchPosts ( page ) {
98114 this . searchIsLoading = true ;
99- const pageSize = 3 ;
100- Fetch . fetchSearchPosts ( this . searchQuery , page , pageSize ) . then ( resp => {
115+ Fetch . fetchSearchPosts ( this . searchQuery , page , this . pageSize ) . then ( resp => {
101116 if ( resp . data ) {
102117 this . searchPostsData = resp . data ;
103118 this . searchMeta = resp . meta ;
104- 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 ) ;
105128 this . searchCurrentPage = page ;
106129 } else {
107130 this . searchIsError = true ;
@@ -114,15 +137,29 @@ export class AppBlog {
114137 this . blogIsLoading = true ;
115138 if ( this . blogFilter ) {
116139 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+
117148 this . blogNumberOfPages = Math . ceil ( this . blogPostsData . length / this . pageSize ) ;
118149 this . blogCurrentPage = 1 ;
119150 } else {
120151 this . blogNumberOfPages = Math . ceil ( this . allBlogPosts . length / this . pageSize ) ;
121152 this . blogPostsData = [ ] ;
122153 let index = ( page - 1 ) * this . pageSize ;
123- 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+
124160 for ( index ; index < endPoint ; index ++ ) {
125- 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 ] ) ;
126163 }
127164 }
128165 this . blogIsLoading = false ;
@@ -149,8 +186,6 @@ export class AppBlog {
149186 if ( ! this . searchQuery && this . blogFilter !== filterName ) {
150187 this . blogFilter = filterName ;
151188 this . getBlogPosts ( 1 ) ;
152- // if (this.blogFilter) this.getBlogPosts(1);
153- // else this.getAllBlogPosts();
154189 }
155190 }
156191
0 commit comments