-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Yannick Liebnau
committed
Aug 4, 2023
1 parent
08e8f49
commit ce305ea
Showing
6 changed files
with
87 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export default class Post { | ||
constructor() { | ||
this.baseURL = 'https://maisonduloup.org/wp-json/wp/v2/posts'; | ||
} | ||
|
||
async fetchPosts() { | ||
const response = await fetch(`${this.baseURL}`); | ||
const posts = await response.json(); | ||
return Boolean(posts) ? posts : new Error('Posts not found'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import Post from "../classes/Post.js"; | ||
|
||
export default class PostController { | ||
constructor() { | ||
this.posts = new Post(); | ||
} | ||
|
||
async getHomepagePosts() { | ||
const posts = await this.posts.fetchPosts(); | ||
const homepagePostsElement = document.createElement('div'); | ||
homepagePostsElement.setAttribute('id', 'home-posts'); | ||
document.querySelector('#home').appendChild(homepagePostsElement); | ||
|
||
const homepagePosts = []; | ||
posts.forEach((post, i) => { | ||
if (post.sticky == true || i <= 2) homepagePosts.push(post); | ||
}) | ||
|
||
homepagePosts.forEach(post => { | ||
let homepagePost = document.createElement("div"); | ||
homepagePost.setAttribute('id', `post-${post.slug}`); | ||
homepagePost.classList.add('home-post'); | ||
|
||
async function getThumbnailImage() { | ||
let mediaURL = `https://maisonduloup.org/wp-json/wp/v2/media/${post.featured_media}`; | ||
let response = await fetch(mediaURL); | ||
let thumbnailImage = await response.json(); | ||
|
||
let postThumbnail = document.createElement('img'); | ||
postThumbnail.classList.add('post-thumbnail'); | ||
postThumbnail.setAttribute('src', thumbnailImage.guid.rendered); | ||
homepagePost.appendChild(postThumbnail); | ||
} | ||
|
||
if (post.featured_media !== 0) { | ||
getThumbnailImage(); | ||
} | ||
|
||
if (post.title.rendered !== "") { | ||
let postTitle = document.createElement('h2'); | ||
postTitle.innerHTML = post.title.rendered; | ||
homepagePost.appendChild(postTitle); | ||
} | ||
if (post.excerpt.rendered !== "") { | ||
let postExcerpt = document.createElement('p'); | ||
postExcerpt.innerHTML = post.excerpt.rendered; | ||
homepagePost.appendChild(postExcerpt); | ||
} | ||
|
||
|
||
|
||
homepagePostsElement.append(homepagePost); | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters