-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript_jap.js
33 lines (28 loc) · 1.2 KB
/
script_jap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
document.addEventListener('DOMContentLoaded', function() {
const postsContainer = document.getElementById('posts-container');
// Fetch all posts from the API
fetch('https://api.stein.shn.hk/api/posts')
.then(response => response.json())
.then(data => {
// Filter posts by type "jap"
const japPosts = data.filter(post => post.type === 'jap');
// Sort posts from new to old
japPosts.sort((a, b) => new Date(b.date) - new Date(a.date));
// Display each post on the page
japPosts.forEach(post => {
const postElement = document.createElement('div');
postElement.classList.add('post');
const postDate = new Date(post.date).toLocaleString();
postElement.innerHTML = `
<h2>${post.text}</h2>
<p><strong>User:</strong> ${post.user}</p>
<p><strong>Date:</strong> ${postDate}</p>
<hr>
`;
postsContainer.appendChild(postElement);
});
})
.catch(error => {
console.error('Error fetching posts:', error);
});
});