-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
41 lines (35 loc) · 1.3 KB
/
scripts.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
34
35
36
37
38
39
40
41
const storyList = document.getElementById('story-list');
const storyForm = document.getElementById('story-form');
// Fetch Stories
const fetchStories = async () => {
const response = await fetch('http://localhost:5000/api/stories');
const stories = await response.json();
renderStories(stories);
}
// Render Stories
const renderStories = (stories) => {
storyList.innerHTML = '';
stories.forEach(story => {
const storyElement = document.createElement('div');
storyElement.innerHTML = `<h2>${story.title}</h2><p>${story.content}</p>`;
storyList.appendChild(storyElement);
});
}
// Handle Story Submission
storyForm.addEventListener('submit', async (e) => {
e.preventDefault();
const title = document.getElementById('title').value;
const content = document.getElementById('content').value;
await fetch('http://localhost:5000/api/stories', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ title, content })
});
document.getElementById('title').value = '';
document.getElementById('content').value = '';
fetchStories();
});
// Initial Fetch
fetchStories();