-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin.js
58 lines (52 loc) · 1.98 KB
/
admin.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
// Navbar part starts from here
let username = document.getElementById("userid");
let usernameinp = localStorage.getItem("username");
if (usernameinp == null) username.innerText = "Sign in";
else username.innerText = usernameinp;
username.addEventListener("click", function () {
if (usernameinp == null) window.location.href = "./signup.html";
else window.location.href = "./logout.html";
})
let icon = document.getElementById("icon");
icon.addEventListener("click", function () {
window.location.href = "./index.html";
})
// Storing data to localstorage part starts from here
let fetcheddata = [];
async function fetchdata() {
try {
let data = await fetch("https://dbioz2ek0e.execute-api.ap-south-1.amazonaws.com/mockapi/get-tech-products")
data = await data.json();
fetcheddata = data.data;
data.data.forEach((e) => {
fetcheddata.push(e);
})
}
catch (arr) {
console.log("error");
}
}
fetchdata();
let form = document.querySelector('form');
let brand = document.getElementById('brand');
let image = document.getElementById('image');
let price = document.getElementById('price');
let details = document.getElementById('details');
let category = document.getElementById('category');
form.addEventListener('submit', (event) => {
event.preventDefault();
let lsdata = JSON.parse(localStorage.getItem("productdata"));
if (lsdata == null) lsdata = fetcheddata;
let product = {
brand: brand.value,
img: image.value,
price: price.value,
details: details.value,
category: category.value,
id: lsdata.length + 1,
}
lsdata.push(product);
localStorage.setItem("productdata", JSON.stringify(lsdata));
form.reset();
alert("Product added successfully");
})