-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
145 lines (129 loc) · 3.97 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
const API_URL_RANDOM = "https://api.thedogapi.com/v1/images/search";
const API_URL_FAVORITES = "https://api.thedogapi.com/v1/favourites";
const API_URL_FAVORITES_DELETE = (id) =>
`https://api.thedogapi.com/v1/favourites/${id}`;
const API_URL_UPLOAD = "https://api.thedogapi.com/v1/images/upload";
const spanError = document.getElementById("error");
async function getNewDogImages() {
try {
const response = await fetch(API_URL_RANDOM);
if (response.status !== 200) {
throw new Error("Hubo un error: " + response.status);
}
const data = await response.json();
console.log("random");
console.log(data);
const imageElement1 = document.getElementById("dog-image1");
const addBtn1 = document.getElementById("add-fav1");
imageElement1.src = data[0].url;
addBtn1.onclick = () => saveFavDog(data[0].id);
} catch (error) {
spanError.innerText = error.message;
}
}
getNewDogImages();
async function getFavDogImages() {
try {
const response = await fetch(API_URL_FAVORITES, {
method: "GET",
headers: {
"X-API-KEY":
"live_fc77FRUbgDCRMt1cN33CEqsbldvBtA72B9FsrqaZBjZ1G33Hn8lP0wAaXHXlUbTZ",
},
});
const data = await response.json();
console.log("favorites");
console.log(data);
if (response.status !== 200) {
throw new Error("Hubo un error: " + response.status);
}
const favoriteSection = data.map(
(dog) =>
`<article>
<div id="img-container">
<img id="${dog.image.id}" alt="foto perrito aleatorio" src="${dog.image.url}"/>
<button class="btn-delete" onclick="deleteFavDog('${dog.id}')">X</button>
</div>
</article>`
);
const favContainer = document.getElementById("favorites-container");
favContainer.innerHTML = favoriteSection.toString().replaceAll(",", "");
} catch (error) {
console.log(error);
spanError.innerText = error;
}
}
getFavDogImages();
async function saveFavDog(id) {
const body = JSON.stringify({
image_id: id,
});
const res = await fetch(API_URL_FAVORITES, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-API-KEY":
"live_fc77FRUbgDCRMt1cN33CEqsbldvBtA72B9FsrqaZBjZ1G33Hn8lP0wAaXHXlUbTZ",
},
body: body,
});
console.log("saved");
const data = await res.json();
if (res.status !== 200) {
spanError.innerText = "Hubo un error: " + res.status + data.message;
} else {
getFavDogImages();
}
}
async function deleteFavDog(id) {
const res = await fetch(API_URL_FAVORITES_DELETE(id), {
method: "DELETE",
headers: {
"Content-Type": "application/json",
"X-API-KEY":
"live_fc77FRUbgDCRMt1cN33CEqsbldvBtA72B9FsrqaZBjZ1G33Hn8lP0wAaXHXlUbTZ",
},
});
const data = await res.json();
if (res.status !== 200) {
spanError.innerText = "Hubo un error: " + res.status + data.message;
} else {
console.log("deleted");
getFavDogImages();
}
}
async function uploadDoggyPhoto() {
const form = document.getElementById("uploadingForm");
const formData = new FormData(form);
const res = await fetch(API_URL_UPLOAD, {
method: "POST",
headers: {
"X-API-KEY":
"live_fc77FRUbgDCRMt1cN33CEqsbldvBtA72B9FsrqaZBjZ1G33Hn8lP0wAaXHXlUbTZ",
},
body: formData,
});
const data = await res.json();
if (res.status !== 201) {
spanError.innerText = "Hubo un error: " + res.status + data.message;
} else {
console.log("photo uploaded");
console.log({ data });
}
}
const input = document.getElementById("file");
const previewPhoto = () => {
const file = input.files;
if (file) {
const fileReader = new FileReader();
const preview = document.getElementById("file-preview");
fileReader.onload = function (event) {
preview.setAttribute("src", event.target.result);
preview.className = "display";
};
fileReader.readAsDataURL(file[0]);
} else {
preview.className = "display-none";
}
};
input.addEventListener("change", previewPhoto);