-
Notifications
You must be signed in to change notification settings - Fork 3
/
Home&Living.js
263 lines (257 loc) · 9.8 KB
/
Home&Living.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
let HomeAndLivingArr = [
{
image_url:
'https://assets.myntassets.com/f_webp,dpr_1.5,q_60,w_210,c_limit,fl_progressive/assets/images/13374272/2022/1/1/6327ebd6-7a7b-48b7-be42-d3b7d97bd58a1641030225627SaralHomeBrownBeigeColourblockedAnti-SkidBedRunner1.jpg',
name: 'Brown & Beige Colourblocked Anti-Skid Bed Runner',
price: '₹999',
strikedoffprice: '₹1999',
brand: 'Saral Home',
discount: '50% OFF',
},
{
image_url:
'https://assets.myntassets.com/f_webp,dpr_1.5,q_60,w_210,c_limit,fl_progressive/assets/images/15702316/2021/10/2/358cd2f3-75d6-4fa4-86a2-5ef246f415881633113866058AthomTrendzBlueMaroonGeometricPrintedPremiumAntiSkidRunnerCa1.jpg',
name: 'Blue & Maroon Geometric Printed Premium Anti Skid Runner Carpet',
price: '₹1219',
strikedoffprice: '₹1999',
brand: 'Athom Trendz',
discount: '39% OFF',
},
{
image_url:
'https://assets.myntassets.com/h_1440,q_90,w_1080/v1/assets/images/productimage/2021/4/17/8850ec0b-d6bd-4739-91d5-c5fb366932b21618657048106-1.jpg',
name: 'Navy Blue & White Printed Rectangular Bed Runner',
price: '₹527',
strikedoffprice: '₹1599',
brand: 'Status',
discount: '67% OFF',
},
{
image_url:
'https://assets.myntassets.com/h_1440,q_90,w_1080/v1/assets/images/productimage/2020/1/16/620033d8-9925-4bab-845d-f1d5dd0120d91579125623253-1.jpg',
name: 'Kids Avenger Iron Man Printed Bed Runner',
price: '₹1199',
strikedoffprice: '₹1999',
brand: 'Marvel',
discount: '44% OFF',
},
{
image_url:
'https://assets.myntassets.com/f_webp,dpr_1.5,q_60,w_210,c_limit,fl_progressive/assets/images/productimage/2019/1/10/f6e3a463-698d-4060-ad73-d6b8a99c9ea81547116396759-1.jpg',
name: 'Black & Brown Bed Runner with Cushion Covers',
price: '₹1099',
strikedoffprice: '₹2199',
brand: 'S9home by Seasons',
discount: '50% OFF',
},
{
image_url:
'https://assets.myntassets.com/h_1440,q_90,w_1080/v1/assets/images/15659718/2021/9/28/71920202-80f3-4262-a48f-634151fed2391632826296486BIANCAUnisexBrownFloral1.jpg',
name:
'Grey & Peach-Coloured Geometric Printed Cotton 150 GSM Double King Bedding Set',
price: '₹199',
strikedoffprice: '₹4999',
brand: 'BIANCA',
discount: '60% OFF',
},
{
image_url:
'https://assets.myntassets.com/f_webp,dpr_1.5,q_60,w_210,c_limit,fl_progressive/assets/images/productimage/2019/4/6/c8ae0727-f1aa-454c-b2cd-94ec6c919bfb1554510193961-1.jpg',
name: 'Bedding Set with Comforter',
price: '₹4874',
strikedoffprice: '₹6499',
brand: 'DDecor',
discount: '25% OFF',
},
{
image_url:
'https://assets.myntassets.com/f_webp,dpr_1.5,q_60,w_210,c_limit,fl_progressive/assets/images/18248764/2022/5/12/77b15022-1839-4488-8b2b-cd545fe730121652368782142SPACESMAROON1DoubleBedsheet1DoubleComforter2PillowCovers1.jpg',
name: 'Floral Printed Bedding Set',
price: '₹2749',
strikedoffprice: '₹5499',
brand: 'SPACES',
discount: '₹2750 OFF',
},
{
image_url:
'https://assets.myntassets.com/f_webp,dpr_1.5,q_60,w_210,c_limit,fl_progressive/assets/images/18062318/2022/4/27/a9698385-469e-445a-97dc-05b0bc00b1891651063834535BeddingSet1.jpg',
name: 'Green Double King 4-Piece Bedding Set',
price: '₹1874',
strikedoffprice: '₹2499',
brand: 'JC Collection',
discount: '25% OFF',
},
{
image_url:
'https://assets.myntassets.com/f_webp,dpr_1.5,q_60,w_210,c_limit,fl_progressive/assets/images/productimage/2021/4/9/13bf35cc-7590-4f2f-a6a7-1536a3cc52ba1617965232837-1.jpg',
name: '148 TC 120 GSM Bedding Set',
price: '₹4199',
strikedoffprice: '₹5999',
brand: 'Boutiue Living India',
discount: '30% OFF',
},
{
image_url:
'https://assets.myntassets.com/f_webp,dpr_1.5,q_60,w_210,c_limit,fl_progressive/assets/images/13374272/2022/1/1/6327ebd6-7a7b-48b7-be42-d3b7d97bd58a1641030225627SaralHomeBrownBeigeColourblockedAnti-SkidBedRunner1.jpg',
name: 'Maroon & Grey Microfiber AC Room Double Bed Reversible Comforter',
price: '₹1199',
strikedoffprice: '₹3299',
brand: 'Hammer Home',
discount: '₹2100 OFF',
},
{
image_url:
'https://assets.myntassets.com/f_webp,dpr_1.5,q_60,w_210,c_limit,fl_progressive/assets/images/17059178/2022/2/5/8741ccbd-0cb6-467c-8116-ec7ced1af1031644043397983SasimoUnisexTealStriped1.jpg',
name: 'Teal & Grey Striped AC Room 210 GSM Double Bed Comforter',
price: '₹1859',
strikedoffprice: '₹5999',
brand: 'Sasimo',
discount: '₹4140 OFF',
},
{
image_url:
'https://assets.myntassets.com/f_webp,dpr_1.5,q_60,w_210,c_limit,fl_progressive/assets/images/17954050/2022/4/20/77b3dce7-d8f6-41b0-902f-c4e6f516b6ba1650456136866JCCollectionUnisexBlueBlanketsQuiltsandDohars1.jpg',
name: 'Blue & White Summer 350 GSM Single Bed Blanket',
price: '₹1949',
strikedoffprice: '₹2599',
brand: 'Js Collection',
discount: '25% OFF',
},
{
image_url:
'https://assets.myntassets.com/f_webp,dpr_1.5,q_60,w_210,c_limit,fl_progressive/assets/images/17306456/2022/2/26/4602769c-4e86-4afb-be3e-da9d978d93a01645856306613StoryhomeUnisexOliveBlanketsQuiltsandDohars1.jpg',
name: 'AC Room Reversible Bed Dohar ',
price: '₹1349',
strikedoffprice: '₹2999',
brand: 'Story@Home',
discount: '33% OFF',
},
{
image_url:
'https://assets.myntassets.com/f_webp,dpr_1.5,q_60,w_210,c_limit,fl_progressive/assets/images/15416776/2021/11/8/0e44dfeb-daf9-4cd2-b9a9-fb8556a7c3031636355313917-Aura-Black--Grey-Geometric-Print-Mild-Winter-150-GSM-Double--1.jpg',
name: 'Black & Grey Printed Mild Winter 150 GSM Double Bed Comforter',
price: '₹874',
strikedoffprice: '₹3499',
brand: 'Aura',
discount: '75% OFF, Hurry*',
},
]
let cartArr = JSON.parse(localStorage.getItem('cartData')) || []
window.addEventListener('load', function () {
cartItems()
})
function sortByGivenOptions() {
selected = document.querySelector('#sort').value
if (selected == 'Ascending') {
HomeAndLivingArr.sort(function (a, b) {
if (Number(a.price.substring(1)) > Number(b.price.substring(1))) return 1
if (Number(a.price.substring(1)) < Number(b.price.substring(1))) return -1
return 0
})
displayPage(HomeAndLivingArr)
}
if (selected == 'Descending') {
HomeAndLivingArr.sort(function (a, b) {
return Number(b.price.substring(1)) - Number(a.price.substring(1))
})
displayPage(HomeAndLivingArr)
}
if (selected == 'Discount') {
HomeAndLivingArr.sort(function (a, b) {
if (a.discount > b.discount) return -1
if (a.discount < b.discount) return 1
return 0
})
displayPage(HomeAndLivingArr)
}
}
function filterByGivenOptions() {
selected = document.querySelector('#filter').value
let filterList = HomeAndLivingArr.filter(function (el) {
return el.brand == selected
})
displayPage(filterList)
}
function sortByNames() {
selected = document.querySelector('#sortNames').value
if (selected == 'Ascending') {
HomeAndLivingArr.sort(function (a, b) {
if (a.name > b.name) return 1
if (a.name < b.name) return -1
return 0
})
displayPage(HomeAndLivingArr)
}
if (selected == 'Descending') {
HomeAndLivingArr.sort(function (a, b) {
if (a.name > b.name) return -1
if (a.name < b.name) return 1
return 0
})
displayPage(HomeAndLivingArr)
}
}
homeAndLivingArr = JSON.parse(localStorage.getItem('cartData')) || []
displayPage(HomeAndLivingArr)
function displayPage(HomeAndLivingArr) {
document.querySelector('#container').innerHTML = ''
HomeAndLivingArr.forEach(function (el) {
let box = document.createElement('div')
let img = document.createElement('img')
img.setAttribute('src', el.image_url)
let name = document.createElement('h4')
name.innerText = el.name.substring(0, 27) + '...'
name.style.fontWeight = 'bold'
let price = document.createElement('p')
price.innerHTML = el.price
let strikedoffprice = document.createElement('p')
strikedoffprice.innerHTML = el.strikedoffprice
let discount = document.createElement('p')
discount.innerHTML = el.discount
let prices = document.createElement('div')
// prices.innerHTML=`<div id="prices" ></div>`
prices.append(price, strikedoffprice, discount)
let brand = document.createElement('h4')
brand.innerText = el.brand
brand.style.color = 'gray'
let add2Cart = document.createElement('button')
add2Cart.innerText = 'Add to Cart'
add2Cart.addEventListener('click', function () {
addToCart(el)
})
box.append(img, name, brand, prices, add2Cart)
document.querySelector('#container').append(box)
// console.log(box);
})
}
function addToCart(ele) {
let flag = false
for (let i = 0; i < cartArr.length; i++) {
if (cartArr[i].name == ele.name) {
cartArr[i].quantity++
cartArr[i].cartprice = cartArr[i].price * cartArr[i].quantity
flag = true
alert(`Product ${ele.name} Added to Cart ${cartArr[i].quantity} Times`)
}
}
if (!flag) {
ele.quantity = 1
ele.price = Number(ele.price.substring(1, ele.price.length))
ele.cartprice = ele.price * ele.quantity
cartArr.push(ele)
alert(`Product ${ele.name} Added to Cart Succesfully`)
}
localStorage.setItem('cartData', JSON.stringify(cartArr))
cartItems()
}
function Gotohome() {
window.location.href = 'index.html'
}
function cartItems(){
let temp = JSON.parse(localStorage.getItem('cartData')) || []
let items=0
temp.forEach(function(ele){
items+=ele.quantity
})
document.getElementById("cart-items").innerText=items
}