forked from platzi/consumo-api-rest-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
146 lines (124 loc) · 4.04 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const api = axios.create({
baseURL: 'https://api.thecatapi.com/v1'
});
api.defaults.headers.common['X-API-KEY'] = 'c08d415f-dea7-4a38-bb28-7b2188202e46';
const API_URL_RANDOM = 'https://api.thecatapi.com/v1/images/search?limit=2';
const API_URL_FAVOTITES = 'https://api.thecatapi.com/v1/favourites';
const API_URL_FAVOTITES_DELETE = (id) => `https://api.thecatapi.com/v1/favourites/${id}`;
const API_URL_UPLOAD = 'https://api.thecatapi.com/v1/images/upload';
const spanError = document.getElementById('error')
async function loadRandomMichis() {
const res = await fetch(API_URL_RANDOM);
const data = await res.json();
console.log('Random')
console.log(data)
if (res.status !== 200) {
spanError.innerHTML = "Hubo un error: " + res.status;
} else {
const img1 = document.getElementById('img1');
const img2 = document.getElementById('img2');
const btn1 = document.getElementById('btn1');
const btn2 = document.getElementById('btn2');
img1.src = data[0].url;
img2.src = data[1].url;
btn1.onclick = () => saveFavouriteMichi(data[0].id);
btn2.onclick = () => saveFavouriteMichi(data[1].id);
}
}
async function loadFavouriteMichis() {
const res = await fetch(API_URL_FAVOTITES, {
method: 'GET',
headers: {
'X-API-KEY': 'c08d415f-dea7-4a38-bb28-7b2188202e46',
},
});
const data = await res.json();
console.log('Favoritos')
console.log(data)
if (res.status !== 200) {
spanError.innerHTML = "Hubo un error: " + res.status + data.message;
} else {
const section = document.getElementById('favoriteMichis')
section.innerHTML = "";
const h2 = document.createElement('h2');
const h2Text = document.createTextNode('Michis favoritos');
h2.appendChild(h2Text);
section.appendChild(h2);
data.forEach(michi => {
const article = document.createElement('article');
const img = document.createElement('img');
const btn = document.createElement('button');
const btnText = document.createTextNode('Sacar al michi de favoritos');
img.src = michi.image.url;
img.width = 150;
btn.appendChild(btnText);
btn.onclick = () => deleteFavouriteMichi(michi.id);
article.appendChild(img);
article.appendChild(btn);
section.appendChild(article);
});
}
}
async function saveFavouriteMichi(id) {
const { data, status } = await api.post('/favourites', {
image_id: id,
});
// const res = await fetch(API_URL_FAVOTITES, {
// method: 'POST',
// headers: {
// 'Content-Type': 'application/json',
// 'X-API-KEY': 'c08d415f-dea7-4a38-bb28-7b2188202e46',
// },
// body: JSON.stringify({
// image_id: id
// }),
// });
// const data = await res.json();
console.log('Save')
if (status !== 200) {
spanError.innerHTML = "Hubo un error: " + status + data.message;
} else {
console.log('Michi guardado en favoritos')
loadFavouriteMichis();
}
}
async function deleteFavouriteMichi(id) {
const res = await fetch(API_URL_FAVOTITES_DELETE(id), {
method: 'DELETE',
headers: {
'X-API-KEY': 'c08d415f-dea7-4a38-bb28-7b2188202e46',
}
});
const data = await res.json();
if (res.status !== 200) {
spanError.innerHTML = "Hubo un error: " + res.status + data.message;
} else {
console.log('Michi eliminado de favoritos')
loadFavouriteMichis();
}
}
async function uploadMichiPhoto() {
const form = document.getElementById('uploadingForm')
const formData = new FormData(form);
console.log(formData.get('file'))
const res = await fetch(API_URL_UPLOAD, {
method: 'POST',
headers: {
// 'Content-Type': 'multipart/form-data',
'X-API-KEY': 'c08d415f-dea7-4a38-bb28-7b2188202e46',
},
body: formData,
})
const data = await res.json();
if (res.status !== 201) {
spanError.innerHTML = "Hubo un error: " + res.status + data.message;
console.log({data})
} else {
console.log('Foto de michi subida :)')
console.log({data})
console.log(data.url)
saveFavouriteMichi(data.id);
}
}
loadRandomMichis();
loadFavouriteMichis();