-
Notifications
You must be signed in to change notification settings - Fork 0
/
main-axios.js
179 lines (130 loc) · 5.6 KB
/
main-axios.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
const api = axios.create ({
baseURL: 'https://api.thecatapi.com/v1'
});
api.defaults.headers.common['x-api-key'] = '94509196-4821-4bbd-a278-78b60d6188a6';
const btnReloadCats = document.getElementById("btnReloadCats");
const img1 = document.getElementById("img1");
const img2 = document.getElementById("img2");
const img3 = document.getElementById("img3");
const error = document.getElementById("error");
error.style.display = "none";
var myModal = document.getElementById('exampleModal')
btnReloadCats.addEventListener("click", getRandomCats);
async function getRandomCats(){
const { data, status } = await api.get('/images/search', {
params: {
limit: 3
}
});
if (status != 200){
error.innerHTML = "Se generó un error " + status + " " + data.message;
error.style.display = "block";
} else {
img1.src = data[0].url;
img2.src = data[1].url;
img3.src = data[2].url;
const btnFavorito1 = document.getElementById("btnFavorito1");
const btnFavorito2 = document.getElementById("btnFavorito2");
const btnFavorito3 = document.getElementById("btnFavorito3");
btnFavorito1.onclick = () => saveFavouriteCat(data[0].id);
btnFavorito2.onclick = () => saveFavouriteCat(data[1].id);
btnFavorito3.onclick = () => saveFavouriteCat(data[2].id);
}
}
async function getFavouritesCats(){
const { data, status } = await api.get('/favourites');
if (status != 200){
error.innerHTML = "Se generó un error " + status + " " + data.message;
error.style.display = "block";
} else {
const subContenedor = document.getElementById("subContenedor");
subContenedor.innerHTML = "";
subContenedor.classList.add("row", "row-cols-1", "row-cols-sm-2", "row-cols-md-3", "g-3");
data.forEach(gato => {
const tarjetaGato = document.createElement("div");
tarjetaGato.classList.add("col");
const divCard = document.createElement("div");
divCard.classList.add("card","shadow-sm");
const divCardBody = document.createElement("div");
divCardBody.classList.add("card-body");
const p = document.createElement("p");
p.classList.add("card-text");
const divButtonsElements = document.createElement("div");
divButtonsElements.classList.add("d-flex", "justify-content-between", "align-items-center");
const divButtonsGroup = document.createElement("div");
divButtonsGroup.classList.add("btn-group");
const smallText = document.createElement("small");
smallText.classList.add("text-muted");
const img = document.createElement("img");
const btn = document.createElement("button");
btn.classList.add("btn", "btn-sm", "btn-outline-secondary");
const btnText = document.createTextNode("Quitar Fav.");
btn.appendChild(btnText);
btn.onclick = () => deleteFavouriteCat(gato.id);
const btnBorrarPicSubida = document.createElement("button");
btnBorrarPicSubida.classList.add("btn", "btn-sm", "btn-outline-secondary");
const btnTextBorrar = document.createTextNode("Eliminar");
btnBorrarPicSubida.appendChild(btnTextBorrar);
btnBorrarPicSubida.onclick = () => deleteUploadCat(gato.image.id, gato.id);
img.src = gato.image.url;
img.height = '200';
img.style.width = '100%';
img.alt = 'Gato favorito';
divCard.appendChild(img);
divCard.appendChild(divCardBody);
divCardBody.appendChild(p);
divCardBody.appendChild(divButtonsElements);
divButtonsGroup.appendChild(btn);
divButtonsGroup.appendChild(btnBorrarPicSubida);
divButtonsElements.appendChild(divButtonsGroup);
divButtonsElements.appendChild(divButtonsGroup);
divButtonsElements.appendChild(smallText);
tarjetaGato.appendChild(divCard);
subContenedor.appendChild(tarjetaGato);
});
}
}
async function saveFavouriteCat(id){
const { data, status } = await api.post('/favourites', {
image_id: id
});
if (status != 200){
error.innerHTML = "Se generó un error " + status + " " + data.message;
error.style.display = "block";
} else {
getFavouritesCats();
}
}
async function deleteFavouriteCat(id){
const { data, status } = await api.delete('/favourites/' + id);
if (status != 200){
error.innerHTML = "Se generó un error " + status + " " + data.message;
error.style.display = "block";
} else {
getFavouritesCats();
}
}
async function UploadPicCat() {
const form = document.getElementById("frmFoto");
const formData = new FormData(form);
const { data, status } = await api.post('/images/upload', formData, {
headers: {
'Content-Type': 'multipart/form-data'
}
});
if (status != 201){
error.innerHTML = "Se generó un error " + status + " " + data.message;
error.style.display = "block";
} else {
saveFavouriteCat(data.id);
$("#exampleModal").modal('hide');
}
}
async function deleteUploadCat(imageId, id){
const { data, status } = await api.delete('/images/' + imageId);
console.log(status);
deleteFavouriteCat(id);
getFavouritesCats();
}
getRandomCats();
getFavouritesCats();