-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproducts.js
159 lines (108 loc) · 3.11 KB
/
products.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
"use strict";
let baseURL = "https://api.escuelajs.co/api/v1/products";
let token = localStorage.getItem("token");
// console.log(baseURL);
async function getData() {
const response = await fetch(`${baseURL}`, {
method: "GET",
headers: {
"Content-Type": "application/json",
// "Authorization": `Bearer ${token}`
}
})
const result = await response.json();
console.log(result);
dataRender(result)
}
// =========== render all data =====================
getData()
function dataRender(data) {
data.forEach(el => {
const card = createElement("div", "item", `
<img class="elImage" src="${el.category.image}">
<h4>Title:${el.title} </h4>
<h4>Price:${el.price}</h4>
<button data-bs-toggle="modal" data-bs-target="#staticBackdrop" class="btn btn-primary bg-primary" data-edit=${el.id}>EDIT</button>
<button class="btn btn-danger bg-danger" data-del=${el.id}>DELETE</button>
`);
$("#data").appendChild(card);
});
}
// ============== add data functions ================
function addData() {
const productName = $('#product-name').value.trim();
const productPrice = $('#product-price').value.trim();
const params = {
title: productName,
price: productPrice
}
console.log(params);
if (productName.length === 0 || productPrice.length === 0) {
alert("Please fill...")
} else {
fetch('https://api.escuelajs.co/api/v1/products/', {
method: "POST",
headers: {
"Content-Type": "application/json",
body: JSON.stringify(params)
}
})
}
}
$('#send').addEventListener('submit', (e) => {
e.preventDefault()
addData()
})
// ============DELETE==========
$('#data').addEventListener('click', (e) => {
if (e.target.classList.contains('btn-danger')) {
let id = e.target.getAttribute('data-del');
fetch(`${baseURL}/${id}`, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${token}`
},
body: JSON.stringify({})
})
setTimeout(() => {
window.location.reload()
}, 1000)
}
})
// ======= edit data =============================
$('#data').addEventListener('click', (e) => {
if (e.target.classList.contains('btn-primary')) {
let id = e.target.getAttribute('data-edit');
localStorage.setItem('editId', id);
sendModal()
}
})
async function sendModal() {
let id = localStorage.getItem('editId');
// console.log(id);
const data = await fetch(`https://api.escuelajs.co/api/v1/products/${id}`);
const result = await data.json();
// console.log(result.price);
$('#editname').value=result.title;
$('#editprice').value = result.price;
}
$('#editsend').addEventListener('submit', (e)=> {
e.preventDefault()
let id = localStorage.getItem('editId');
let title = $('#editname').value;
let price = $('#editprice').value;
console.log(price);
let params = {
title: title,
price: price
}
console.log(params);
fetch(`https://api.escuelajs.co/api/v1/products/${id}`, {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(params)
})
})