-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
156 lines (134 loc) · 4.93 KB
/
script.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
fetch("https://fake-api-vq1l.onrender.com/posts", {
method: "GET",
headers: {
"Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOjE1LCJlbWFpbCI6InN0ZXZlbi5sb3BlejJAdXRwLmVkdS5jbyIsImlhdCI6MTcyNjY4MTkzMiwiZXhwIjoxNzQzOTYxOTMyfQ.B4COfQ30uuvJjfW-t7nRCm6uSoFq_sVpEip5iy5S6rQ"
}
})
.then(response => response.json())
.then(data => {
const list = document.getElementById("list");
data.forEach( product => {
const il = document.createElement("li");
const images = JSON.parse(product.images)
const myhtml = `
<div class="card " style="width: 18rem; border: 2px solid #F5E7B2;background-color: #F5E7B2;
box-shadow: 0 12px 16px 0 #d1ab7c, 0 17px 50px 0 #F9D689;">
<img src="${images[0]}" class="card-img-top image" alt="error en imagen">
<div class="card-body card" style="border: 2px solid #E0A75E; background-color: #FFEEA9;">
<h5 class="card-title" style="color: #E85C0D" id="titulo">${product.title}</h5>
<p class="card-text">${product.description}</p>
<p class="card-text">$ ${product.value}</p>
<div class="Buttons">
<div class="Button1">
<Button class="bEditar" onclick="editPost(${product.id},${product.value},'${product.title}','${product.description}','${images[0]}')">
<img src="pen.png" class="iEliminar"/>
</Button>
</div>
<div class="Button1">
<Button onclick="deletePost(${product.id})" class="bEliminar">
<img src="delete.png" class="iEliminar"/>
</Button>
</div>
</div>
</div>
</div>
`;
il.innerHTML = myhtml;
list.appendChild(il);
})
});
function sendForm(){
const title = document.getElementById("title");
const description = document.getElementById("description");
const value = document.getElementById("value");
const image = document.getElementById("image");
const body ={
title: title.value,
description: description.value,
value: value.value,
images: [image.value]
}
fetch("https://fake-api-vq1l.onrender.com/posts", {
method: "POST",
headers: {
"Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOjE1LCJlbWFpbCI6InN0ZXZlbi5sb3BlejJAdXRwLmVkdS5jbyIsImlhdCI6MTcyNjY4MTkzMiwiZXhwIjoxNzQzOTYxOTMyfQ.B4COfQ30uuvJjfW-t7nRCm6uSoFq_sVpEip5iy5S6rQ",
"Content-type": "application/json"
},
body: JSON.stringify(body)
})
.then( res => res.json())
.then( res => {
console.log(
"respuesta de la api", res
)
title.value = "";
description.value = "";
value.value = "";
image.value = "";
location.reload();
})
}
function deletePost(id){
fetch(`https://fake-api-vq1l.onrender.com/posts/${id}`, {
method: "DELETE",
headers: {
"Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOjE1LCJlbWFpbCI6InN0ZXZlbi5sb3BlejJAdXRwLmVkdS5jbyIsImlhdCI6MTcyNjY4MTkzMiwiZXhwIjoxNzQzOTYxOTMyfQ.B4COfQ30uuvJjfW-t7nRCm6uSoFq_sVpEip5iy5S6rQ",
},
})
.then( res => res.json())
.then( res => {
console.log(
"respuesta de la api", res
)
location.reload();
})
}
function editPost(id,value,title,description,images){
var modal = document.getElementById("myModal");
var span = document.getElementsByClassName("close")[0];
var updateForm = document.getElementById("update");
let titulo = document.getElementById("title1");
let descripcion = document.getElementById("description1");
const precio = document.getElementById("value1");
let imagen = document.getElementById("image1");
titulo.value = `${title}`
descripcion.value = `${description}`
precio.value = `${value}`
imagen.value = `${images}`
modal.style.display= "block";
span.onclick = function() {
modal.style.display = "none";
}
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
updateForm.onclick = function(){
const title = document.getElementById("title1");
const description = document.getElementById("description1");
const value = document.getElementById("value1");
const image = document.getElementById("image1");
const body ={
title: title.value,
description: description.value,
value: value.value,
images: [image.value]
}
fetch(`https://fake-api-vq1l.onrender.com/posts/${id}`, {
method: "PATCH",
headers: {
"Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOjE1LCJlbWFpbCI6InN0ZXZlbi5sb3BlejJAdXRwLmVkdS5jbyIsImlhdCI6MTcyNjY4MTkzMiwiZXhwIjoxNzQzOTYxOTMyfQ.B4COfQ30uuvJjfW-t7nRCm6uSoFq_sVpEip5iy5S6rQ",
"Content-type": "application/json"
},
body: JSON.stringify(body)
})
.then( res => res.json())
.then( res => {
console.log(
"respuesta de la api", res
)
location.reload();
})
}
}