@@ -4,16 +4,22 @@ import { buildPost } from './showPostsView.js';
44//===================================================================================================================
55export async function showPostsController ( container ) { // the container is ".posts-container"
66
7+ const pages = document . getElementById ( 'pages' ) ;
8+
9+ const POSTS_PER_PAGE = 10 ;
10+ let currentPage = 1 ;
11+
712 try {
813 //container.dispatchEvent(new CustomEvent("load-posts-started"))
914 const event = new CustomEvent ( "load-posts-started" ) ;
1015 container . dispatchEvent ( event ) ; // --> This "event" goes to "load-posts-started".
1116
1217 //===================================
13- const posts = await getPosts ( ) ;
18+ const { posts, totalPosts } = await getPosts ( currentPage ) ;
1419 //===================================
1520
16- drawPosts ( posts , container )
21+ drawPosts ( posts , container ) ;
22+ drawPages ( totalPosts ) ;
1723
1824 } catch ( error ) {
1925
@@ -23,6 +29,8 @@ export async function showPostsController(container) { // the container is ".pos
2329 detail : error . message
2430 } )
2531
32+ console . log ( error )
33+
2634 // By sending the following line "main.js" will listen for this event and do what we told it to do,
2735 // in this case "showNotification(errorMesage)" method.
2836 container . dispatchEvent ( event ) // --> This "event" goes to "load-posts-error".
@@ -32,31 +40,72 @@ export async function showPostsController(container) { // the container is ".pos
3240 const event = new CustomEvent ( "load-posts-finished" )
3341 container . dispatchEvent ( event ) // --> This "event" goes to "load-posts-finished".
3442 }
35- }
3643
37- //-------------------------------------------------------------------------------------------------------------------
38- function drawPosts ( posts , container ) {
3944
40- container . innerHTML = '' ; // I clean all displayed posts if any.
45+ //-------------------------------------------------------------------------------------------------------------------
46+ function drawPosts ( posts , container ) {
4147
42- if ( posts . length === 0 ) {
43- //alert("Sorry, no posts available!")
44- } else { // I add this "else" to have more clarity of the code.
48+ container . innerHTML = '' ; // I clean all displayed posts if any.
4549
46- posts . forEach ( ( post ) => { // If "posts" is an empty array, the "forEach" will not be executed.
50+ if ( posts . length === 0 ) {
51+ //alert("Sorry, no posts available!")
52+ } else { // I add this "else" to have more clarity of the code.
4753
48- const postHtml = document . createElement ( "a" ) ;
49- postHtml . setAttribute ( "href" , `./views/post-detail.html?id=${ post . id } ` )
50- // Creates an empty HTML element in memory (postHtml), which is not yet in the DOM.
54+ posts . forEach ( ( post ) => { // If "posts" is an empty array, the "forEach" will not be executed.
5155
52- postHtml . innerHTML = buildPost ( post )
53- // In an already cleaned container,
54- // I assign to "postHtml" whatever the buildPost(post) function returns .
56+ const postHtml = document . createElement ( "a" ) ;
57+ postHtml . setAttribute ( "href" , `./views/post-detail.html?id= ${ post . id } ` )
58+ // Creates an empty HTML element in memory (postHtml), which is not yet in the DOM .
5559
56- container . appendChild ( postHtml )
57- // I insert this new div in the container.
58- // Requires you to pass a node object (postHtml), not plain text or HTML.
59- } )
60+ postHtml . innerHTML = buildPost ( post )
61+ // In an already cleaned container,
62+ // I assign to "postHtml" whatever the buildPost(post) function returns.
63+
64+ container . appendChild ( postHtml )
65+ // I insert this new div in the container.
66+ // Requires you to pass a node object (postHtml), not plain text or HTML.
67+ } )
68+ }
6069 }
61- }
6270
71+ //-------------------------------------------------------------------------------------------------------------------
72+ function drawPages ( totalPosts ) {
73+
74+ pages . innerHTML = '' ;
75+
76+ const pageCount = Math . ceil ( totalPosts / POSTS_PER_PAGE ) ;
77+
78+ for ( let i = 1 ; i <= pageCount ; i ++ ) {
79+
80+ const btn = document . createElement ( 'button' ) ;
81+ btn . textContent = i ;
82+
83+ if ( i === currentPage ) {
84+ btn . classList . add ( 'active' ) ;
85+ }
86+
87+ btn . addEventListener ( 'click' , async ( ) => {
88+
89+ currentPage = i ;
90+
91+ try {
92+ //===================================
93+ const { posts } = await getPosts ( currentPage ) ;
94+ //===================================
95+
96+ drawPosts ( posts , container ) ;
97+ drawPages ( totalPosts ) ;
98+
99+ } catch ( error ) {
100+
101+ const errorEvent = new CustomEvent ( "load-posts-error" , {
102+ detail : error . message
103+ } ) ;
104+ container . dispatchEvent ( errorEvent ) ;
105+ }
106+ } ) ;
107+
108+ pages . appendChild ( btn ) ;
109+ }
110+ }
111+ }
0 commit comments