-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
57 lines (48 loc) · 1.49 KB
/
script.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Make an application that allows a users to:
// View poems
// View one particular poem
// Post a poem
// Delete a poem.
// A poem object should have an author, a body (the poem itself) and the date added.
// Fetching Poems
function fetchData(){
fetch('http://localhost:3000/poems')
.then(response => response.json())
.then(data => viewPoems(data))
.catch(error => console.log(error))
}
fetchData()
// To view poems
function viewPoems(poems){
for(poem of poems){
// Append the poems to the DOM
let poemDiv = document.querySelector('#poem')
let newPoem = document.createElement('div')
newPoem.innerHTML = `
<h3>Author: ${poem['author']}</h3>
<p>${poem['body']}</p>
`
poemDiv.appendChild(newPoem)
}
}
let form = document.querySelector('form')
form.addEventListener('submit', fetchOnePoem)
function fetchOnePoem(e){
e.preventDefault();
// Grab id, Return specific poem
let poemID = document.querySelector('#poemID')
fetch(`http://localhost:3000/poems/${poemID.value}`)
.then(response => response.json())
.then(data =>displayPoem(data))
.catch(error => console.log(error))
}
function displayPoem(data){
let newDiv = document.querySelector('#newPoem')
let displayedPoem = document.createElement('div')
displayedPoem.innerHTML = `
<h4>Author: ${data.author}</h4>
<p>${data.body}</p>
<p>Date added: ${data.dateAdded}</p>
`
newDiv.appendChild(displayedPoem)
}