forked from diurivj/restfulcrud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
45 lines (40 loc) · 1.35 KB
/
main.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
const buttonFetchAll = document.getElementById('fetch-all')
const buttonFetchOne = document.getElementById('fetch-one')
const buttonDelete = document.getElementById('delete-one')
const buttonEdit = document.getElementById('edit-one')
const endpoint = 'https://ih-crud-api.herokuapp.com/characters/'
buttonFetchAll.onclick = () => {
axios
.get(endpoint)
.then(({ data }) => {
console.log(data)
})
.catch(err => console.log(err))
}
buttonFetchOne.onclick = () => {
id = document.getElementById('fetch-one-input').value
axios
.get(`${endpoint}${id}`)
.then(({ data }) => console.log(data))
.catch(err => console.log(err))
}
buttonEdit.onclick = () => {
id = document.getElementById('edit-one-input').value
name = document.querySelector('input[name="name"]').value
occupation = document.querySelector('input[name="occupation"]').value
debt = document.querySelector('input[name="debt"]').value
weapon = document.querySelector('input[name="weapon"]').value
axios
.patch(`${endpoint}${id}`, { name, occupation, debt, weapon })
.then(({ data }) => {
console.log(data)
})
.catch(err => console.log(err))
}
buttonDelete.onclick = () => {
id = document.getElementById('delete-one-input').value
axios
.delete(`${endpoint}${id}`)
.then(({ data }) => console.log(data))
.catch(err => console.log(err))
}