-
Notifications
You must be signed in to change notification settings - Fork 0
/
product-detail.js
89 lines (74 loc) · 2.05 KB
/
product-detail.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
var productId=location.search.split("=")[1];
var currProd;
console.log("Product Id : "+productId);
// $("#count").html(
// JSON.parse(localStorage.getItem("cartItem")).length
// );
$.get("https://5d76bf96515d1a0014085cf9.mockapi.io/product/"+productId,function(data,status){
currProd=data;
console.log(data);
setValue(data);
for(var i=0;i<data.photos.length;i++)
{
$("#prvwContainer").append(preview(data.photos[i]))
$(".prvwImg")[0].classList.add("activeClass")
$(".prvwImg").click(function(){
})
}
})
function setValue(response)
{
$("#previewImage").attr("src",response.preview);
$("#descName").html(response.name);
$("#descBrand").html(response.brand);
$("#descPrice").html("Price : RS "+response.price);
$("#descText").html(response.description);
}
function preview(photo,pos)
{
var imageHolder=document.createElement("div");
imageHolder.className="imageHolder";
var image=document.createElement("img");
image.src=photo;
image.className="prvwImg";
imageHolder.appendChild(image);
image.onclick=function(){
$(".prvwImg").removeClass("activeClass");
image.classList.add("activeClass");
$("#previewImage").attr("src",photo);
}
return imageHolder;
}
$("#btnAdd").click(function(){
var productList=localStorage.getItem("cartItem");
var foundAtpos=-1;
productList=productList!=null?JSON.parse(productList):[];
// $("#count").html(productList.length);
console.log(productList);
for(var i=0;i<productList.length;i++)
{
if(productList[i].id == currProd.id)
{
foundAtpos=i;
}
}
if(foundAtpos>-1)
{
productList[foundAtpos].count=productList[foundAtpos].count+1;
localStorage.setItem('cartItem',JSON.stringify(productList));
console.log("FOUND")
}
else
{
currProd.count=1;
productList.push(currProd);
localStorage.setItem('cartItem',JSON.stringify(productList));
}
var totalItems=0;
for(var i=0;i<productList.length;i++)
{
totalItems+=productList[i].count;
}
console.log(currProd);
$("#count").html(totalItems);
})