forked from lassiecoder/E-CommerceWebsite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contentDetails.js
147 lines (115 loc) · 4.31 KB
/
contentDetails.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
console.clear()
let id = location.search.split('?')[1]
console.log(id)
if(document.cookie.indexOf(',counter=')>=0)
{
let counter = document.cookie.split(',')[1].split('=')[1]
document.getElementById("badge").innerHTML = counter
}
function dynamicContentDetails(ob)
{
let mainContainer = document.createElement('div')
mainContainer.id = 'containerD'
document.getElementById('containerProduct').appendChild(mainContainer);
let imageSectionDiv = document.createElement('div')
imageSectionDiv.id = 'imageSection'
let imgTag = document.createElement('img')
imgTag.id = 'imgDetails'
//imgTag.id = ob.photos
imgTag.src = ob.preview
imageSectionDiv.appendChild(imgTag)
let productDetailsDiv = document.createElement('div')
productDetailsDiv.id = 'productDetails'
// console.log(productDetailsDiv);
let h1 = document.createElement('h1')
let h1Text = document.createTextNode(ob.name)
h1.appendChild(h1Text)
let h4 = document.createElement('h4')
let h4Text = document.createTextNode(ob.brand)
h4.appendChild(h4Text)
console.log(h4);
let detailsDiv = document.createElement('div')
detailsDiv.id = 'details'
let h3DetailsDiv = document.createElement('h3')
let h3DetailsText = document.createTextNode('Rs ' + ob.price)
h3DetailsDiv.appendChild(h3DetailsText)
let h3 = document.createElement('h3')
let h3Text = document.createTextNode('Description')
h3.appendChild(h3Text)
let para = document.createElement('p')
let paraText = document.createTextNode(ob.description)
para.appendChild(paraText)
let productPreviewDiv = document.createElement('div')
productPreviewDiv.id = 'productPreview'
let h3ProductPreviewDiv = document.createElement('h3')
let h3ProductPreviewText = document.createTextNode('Product Preview')
h3ProductPreviewDiv.appendChild(h3ProductPreviewText)
productPreviewDiv.appendChild(h3ProductPreviewDiv)
let i;
for(i=0; i<ob.photos.length; i++)
{
let imgTagProductPreviewDiv = document.createElement('img')
imgTagProductPreviewDiv.id = 'previewImg'
imgTagProductPreviewDiv.src = ob.photos[i]
imgTagProductPreviewDiv.onclick = function(event)
{
console.log("clicked" + this.src)
imgTag.src = ob.photos[i]
document.getElementById("imgDetails").src = this.src
}
productPreviewDiv.appendChild(imgTagProductPreviewDiv)
}
let buttonDiv = document.createElement('div')
buttonDiv.id = 'button'
let buttonTag = document.createElement('button')
buttonDiv.appendChild(buttonTag)
buttonText = document.createTextNode('Add to Cart')
buttonTag.onclick = function()
{
let order = id+" "
let counter = 1
if(document.cookie.indexOf(',counter=')>=0)
{
order = id + " " + document.cookie.split(',')[0].split('=')[1]
counter = Number(document.cookie.split(',')[1].split('=')[1]) + 1
}
document.cookie = "orderId=" + order + ",counter=" + counter
document.getElementById("badge").innerHTML = counter
console.log(document.cookie)
}
buttonTag.appendChild(buttonText)
console.log(mainContainer.appendChild(imageSectionDiv));
mainContainer.appendChild(imageSectionDiv)
mainContainer.appendChild(productDetailsDiv)
productDetailsDiv.appendChild(h1)
productDetailsDiv.appendChild(h4)
productDetailsDiv.appendChild(detailsDiv)
detailsDiv.appendChild(h3DetailsDiv)
detailsDiv.appendChild(h3)
detailsDiv.appendChild(para)
productDetailsDiv.appendChild(productPreviewDiv)
productDetailsDiv.appendChild(buttonDiv)
return mainContainer
}
// BACKEND CALLING
let httpRequest = new XMLHttpRequest()
{
httpRequest.onreadystatechange = function()
{
if(this.readyState === 4 && this.status == 200)
{
console.log('connected!!');
let contentDetails = JSON.parse(this.responseText)
{
console.log(contentDetails);
dynamicContentDetails(contentDetails)
}
}
else
{
console.log('not connected!');
}
}
}
httpRequest.open('GET', 'https://5d76bf96515d1a0014085cf9.mockapi.io/product/'+id, true)
httpRequest.send()