-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsingle-product-page.js
195 lines (164 loc) · 7.48 KB
/
single-product-page.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// let data = [
// {
// "id": 1,
// "sub-category": "iPhone 14",
// "title": "Nimbus Phone Case Cover For iPhone 14",
// "price": 1999,
// "discountPercentage": 12.96,
// "description":"A minimally quilted laptop sleeve with light padding, Siesta is safe to carry by hand for short, comfortable excursions or can be slipped inside your backpacks or tote bags for longer day-outs. It features a one of a kind built with unconventional and eye-catching artworks, exhibiting a bold expression of individuality.",
// "stock": 94,
// "brand": "Apple",
// "category": "phone cases",
// "thumbnail": "https://images.dailyobjects.com/marche/product-images/1101/slick-phone-case-cover-for-iphone-14-images/Nimbus-Phone-Case-Cover-For-iPhone-14-2.png?tr=cm-pad_resize,v-2,w-352,h-434,dpr-1",
// "images": [
// "https://images.dailyobjects.com/marche/product-images/1101/numbus-frosted-phone-case-cover-for-samsung-galaxy-z-fold-4-images/Nimbus-Frosted-Phone-Case-Cover-For-Samsung-Galaxy-Z-Fold-4.png?tr=cm-pad_resize,v-2,w-768,h-664,dpr-1",
// "https://images.dailyobjects.com/marche/product-images/1101/numbus-frosted-phone-case-cover-for-samsung-galaxy-z-fold-4-images/Nimbus-Frosted-Phone-Case-Cover-For-Samsung-Galaxy-Z-Fold-4-1.png?tr=cm-pad_resize,v-2,w-768,h-664,dpr-1",
// "https://images.dailyobjects.com/marche/product-images/1101/numbus-frosted-phone-case-cover-for-samsung-galaxy-z-fold-4-images/Nimbus-Frosted-Phone-Case-Cover-For-Samsung-Galaxy-Z-Fold-4-2.png?tr=cm-pad_resize,v-2,w-768,h-664,dpr-1",
// "https://images.dailyobjects.com/marche/product-images/1101/numbus-frosted-phone-case-cover-for-samsung-galaxy-z-fold-4-images/Nimbus-Frosted-Phone-Case-Cover-For-Samsung-Galaxy-Z-Fold-4-3.png?tr=cm-pad_resize,v-2,w-768,h-664,dpr-1",
// "https://images.dailyobjects.com/marche/product-images/1101/numbus-frosted-phone-case-cover-for-samsung-galaxy-z-fold-4-images/Nimbus-Frosted-Phone-Case-Cover-For-Samsung-Galaxy-Z-Fold-4-4.png?tr=cm-pad_resize,v-2,w-768,h-664,dpr-1"
// ]
// }
// ]
let data = JSON.parse(localStorage.getItem("single-data"))||[];
let Data = JSON.parse(localStorage.getItem("recently-viewed"))||[];
let cartData = JSON.parse(localStorage.getItem("cartdata"))||[];
console.log(data)
let slide = document.getElementById("slide");
let btn1 = document.getElementById("btn1");
let btn2 = document.getElementById("btn2");
// slide.style.width = "350px";
// slide.style.height = "250px";
let i = 0;
slide.src = data[0].images[i];
btn2.onclick = () => {
i++;
if (i > data[0].images.length - 1) {
i = 0;
}
slide.src = data[0].images[i];
};
btn1.onclick = () => {
i--;
if (i < 0) {
i = data[0].images.length - 1;
}
slide.src = data[0].images[i];
};
btn1.addEventListener("click", () => {
slide.classList.add("transition");
});
btn1.addEventListener("mouseup", () => {
slide.classList.remove("transition");
});
btn2.addEventListener("click", () => {
slide.classList.add("transition");
});
btn2.addEventListener("mouseup", () => {
slide.classList.remove("transition");
});
let container = document.getElementById("detail-card");
display(data)
function display(data){
data.forEach((element) => {
let card = document.createElement("div");
card.setAttribute("class","card")
let name = document.createElement("p");
name.setAttribute("class","name")
let pricediv = document.createElement("div")
let price = document.createElement("span");
priceRemove = document.createElement("span");
taxline = document.createElement("span");
let AddCart =document.createElement("button")
AddCart.setAttribute("class","cartBtn")
let h3 = document.createElement("h3")
let p = document.createElement("p");
let pincode = document.createElement("input");
let checkbutton = document.createElement("button");
checkbutton.setAttribute("class","checkbutton")
let productDetails = document.createElement("div")
let details = document.createElement("p");
let productSpecifications = document.createElement("div")
let Specifications = document.createElement("p");
let productDelivery = document.createElement("div")
let Delivery = document.createElement("p");
details.textContent = "Product Details";
Specifications.textContent = "Specifications";
Delivery.textContent = "Delivery & Returns";
p.textContent = "*FREE DUFFLE BAG ON ORDERS ABOVE Rs.2999 / FREE KEYCHAIN SET ON ORDERS ABOVE Rs.1999";
h3.textContent = "EXCITING OFFERS";
name.textContent = element.name;
priceRemove.textContent = element.price+850;
taxline.textContent = "Inclusive of all taxes";
price.textContent = `Rs.${element.price}`;
taxline.setAttribute("class","span3")
priceRemove.setAttribute("class","span1")
price.setAttribute("class","span2")
pricediv.setAttribute("class","pricediv")
AddCart.textContent = "ADD TO CART";
pincode.setAttribute("placeholder","Enter Pincode To Check Delivery");
checkbutton.textContent = "CHECK";
AddCart.addEventListener("click",()=>{
if(checkduplicate(element)){
alert("product already added in cart");
}
else{
cartData.push({...element,qty:1});
localStorage.setItem("cartdata",JSON.stringify(cartData));
alert("product successfully added to cart")
window.location.reload()
}
})
productDetails.append(details);
productSpecifications.append(Specifications);
productDelivery.append(Delivery);
pricediv.append(price,priceRemove,taxline)
card.append(name,pricediv,AddCart,h3,p,pincode,checkbutton,productDetails,productSpecifications,productDelivery)
container.append(card)
});
}
let cont1 = document.getElementById("products-1")
displayRecentData(Data)
function displayRecentData(Data){
Data.forEach((element) => {
let card = document.createElement("div");
let img = document.createElement("img");
let name = document.createElement("p");
name.setAttribute("class","hidden")
let price = document.createElement("h4");
img.src = element.img;
name.textContent = element.name;
price.textContent = `Rs.${element.price}`;
card.addEventListener("click",()=>{
let obj = {}
obj.name = element.name;
obj.img = element.img;
obj.price = element.price;
obj.images = element.images;
obj.id = element.id;
let arr =[];
arr.push(obj);
localStorage.setItem("single-data",JSON.stringify(arr))
location.reload()
})
cont1.append(card);
card.append(img,name,price)
});
}
function checkduplicate(element){
for(let i=0;i<cartData.length;i++){
if(cartData[i].name===element.name){
return true;
}
}
return false;
}
let logo = document.getElementById("logo")
logo.addEventListener("click",()=>{
window.location.href="./index.html"
})
let cartcount = document.querySelector(".cartcount");
let cartdata = JSON.parse(localStorage.getItem("cartdata")) || [];
if (cartdata.length === 0) {
cartcount.style.display = "none"
}
cartcount.innerText = cartdata.length;