File tree Expand file tree Collapse file tree 5 files changed +65
-1
lines changed Expand file tree Collapse file tree 5 files changed +65
-1
lines changed Original file line number Diff line number Diff line change 1+ import { showPostsController } from "./modules/show-posts/showPostsController.js" ;
2+
13
24// this callback is executed once the DOM is fully loaded.
35document . addEventListener ( "DOMContentLoaded" , ( ) => {
4-
6+ const container = document . querySelector ( ".posts-container" )
7+ showPostsController ( container )
8+
59} )
Original file line number Diff line number Diff line change 1+ import { getPosts } from "./showPostsModel.js"
2+ import { buildPost } from './showPostsView.js' ;
3+
4+ export async function showPostsController ( container ) { // the container is ".posts-container"
5+
6+ try {
7+ const posts = await getPosts ( ) ;
8+ drawPosts ( posts , container )
9+ } catch ( error ) {
10+ /* ... */
11+ } finally {
12+ /* ... */
13+ }
14+
15+ }
16+
17+ function drawPosts ( posts , container ) { // the container is ".posts-container"
18+
19+ container . innerHTML = '' ;
20+
21+
22+ posts . forEach ( ( post ) => {
23+
24+ const postHtml = document . createElement ( "div" ) ;
25+
26+ postHtml . innerHTML = buildPost ( post )
27+
28+ container . appendChild ( postHtml )
29+ } )
30+ }
Original file line number Diff line number Diff line change 1+ export async function getPosts ( ) {
2+
3+ let posts = [ ] ;
4+
5+ try {
6+ const response = await fetch ( 'http://localhost:8000/api/posts' )
7+ posts = await response . json ( ) ;
8+ } catch ( error ) {
9+ throw new Error ( "It was not possible to get the posts. Please try again later." )
10+ }
11+
12+ return posts ;
13+ }
Original file line number Diff line number Diff line change 1+ export const buildPost = ( post ) => {
2+ let postView = `
3+ <p>${ post . userId } </p>
4+ <p>${ post . name } </p>
5+ <p>${ post . description } </p>
6+ <p>${ post . price } </p>
7+ <p>${ post . photo } </p>
8+ <p>${ post . sale_purchase } </p>
9+ ` ;
10+
11+ return postView
12+ }
13+
14+ export const buildNoTweetsAdvice = ( ) => {
15+ return '<h3>Sorry, no posts available!</h3>'
16+ }
Original file line number Diff line number Diff line change 66 < title > Document</ title >
77</ head >
88< body >
9+ < div class ="posts-container "> </ div >
910
1011 < script type ="module " src ="../src/main.js "> </ script >
1112</ body >
You can’t perform that action at this time.
0 commit comments