Skip to content

Commit 8e55384

Browse files
committed
progress is made in showPosts
1 parent 8030f82 commit 8e55384

File tree

5 files changed

+65
-1
lines changed

5 files changed

+65
-1
lines changed

src/main.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
import { showPostsController } from "./modules/show-posts/showPostsController.js";
2+
13

24
// this callback is executed once the DOM is fully loaded.
35
document.addEventListener("DOMContentLoaded", () => {
4-
6+
const container = document.querySelector(".posts-container")
7+
showPostsController(container)
8+
59
})
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
}

views/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
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>

0 commit comments

Comments
 (0)