-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
150 lines (128 loc) · 4.72 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
147
148
149
150
const api = axios.create({
baseURL: 'https://api.thecatapi.com/v1'
});
api.defaults.headers.common['X-API-KEY'] = 'live_QBSMUgyr2Pn7axbQld7rXSU4z9QPXU3a4LifJw1Uhwn6INt4riU3PQQJAbS9XWbh';
const API_URL_RANDOM = 'https://api.thecatapi.com/v1/images/search?limit=3';
const API_URL_FAVORITES = 'https://api.thecatapi.com/v1/favourites';
const API_URL_FAVORITES_DELETE = (id) => `https://api.thecatapi.com/v1/favourites/${id}?api_key=live_QBSMUgyr2Pn7axbQld7rXSU4z9QPXU3a4LifJw1Uhwn6INt4riU3PQQJAbS9XWbh`;
const API_URL_UPLOAD = 'https://api.thecatapi.com/v1/images/upload';
const spanError = document.getElementById('error')
async function loadRandonMichis() {
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 img3 = document.getElementById('img3');
const btn1 = document.getElementById('btn1');
const btn2 = document.getElementById('btn2');
const btn3 = document.getElementById('btn3');
img1.src = data[0].url;
img2.src = data[1].url;
img3.src = data[2].url;
btn1.onclick = () => saveFavouriteMichis(data[0].id);
btn2.onclick = () => saveFavouriteMichis(data[1].id);
btn3.onclick = () => saveFavouriteMichis(data[2].id);
}
}
async function loadFavouriteMichis() {
const res = await fetch(API_URL_FAVORITES, {
method: 'GET',
headers: {
'X-API-KEY': 'live_QBSMUgyr2Pn7axbQld7rXSU4z9QPXU3a4LifJw1Uhwn6INt4riU3PQQJAbS9XWbh',
},
});
const data = await res.json();
console.log('Favoritos')
console.log(data)
if (res.status !== 200) {
spanError.innerHTML = "Hubo un errpr: " + res.status + data.messange;
} 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 saveFavouriteMichis(id) {
const {data, status } = await api.post('/favourites', {
image_id: id,
});
/*const res = await fetch(API_URL_FAVORITES, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-API-KEY': 'live_QBSMUgyr2Pn7axbQld7rXSU4z9QPXU3a4LifJw1Uhwn6INt4riU3PQQJAbS9XWbh',
},
body: JSON.stringify({
image_id: id
}),
});
const data = await res.json();*/
console.log('Seve')
/*console.log(res)*/
if (status !== 200) {
spanError.innerHTML = "Hubo un errpr: " + status + data.messange;
}else {
console.log('Michi guardado en favoritos')
loadFavouriteMichis();
}
}
async function deleteFavouriteMichi(id) {
const res = await fetch(API_URL_FAVORITES_DELETE(id), {
method: 'DELETE',
headers: {
'x-API-KEY': 'live_QBSMUgyr2Pn7axbQld7rXSU4z9QPXU3a4LifJw1Uhwn6INt4riU3PQQJAbS9XWbh',
}
});
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: {
'X-API-KEY': 'live_QBSMUgyr2Pn7axbQld7rXSU4z9QPXU3a4LifJw1Uhwn6INt4riU3PQQJAbS9XWbh',
},
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)
saveFavouriteMichis(data.id);
}
}
loadRandonMichis();
loadFavouriteMichis();