-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecommerce.html
308 lines (297 loc) · 12.4 KB
/
ecommerce.html
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<html>
<head>
<title>Class work | Home Work | test server change</title>
<link rel='icon' type='image/png' href='simple-twitter-16x16-icon-png-7.png' />
<link rel='stylesheet' href='style.css' />
<style>
.discount-price {
text-decoration: line-through;
color: red
}
.actual-price {
color: green;
}
.paymentLabel {
display: flex;
align-items: center;
}
</style>
</head>
<body>
<div id='header' class='slideDown'>
<h1>Header</h1>
</div>
<div id='container'>
<div id='sidebar'>
<h1>Sidebar</h1>
</div>
<div id='mainBox' style='display: flex; width: 100%; gap:10px'>
<div class='grid-item slideDown' style='width: 30%;'>
<form id='ecommerceForm' onsubmit='event.preventDefault();' style='display: flex; flex-direction:column; gap: 4px; '>
<label>Checkout Form</label>
<input type='text' name='first_name' required id='first_name' placeholder="Enter you first_name"/>
<span style='color:red;' hidden id='taskErrorField-first_name'>This field is required</span>
<input type='text' name='last_name' required id='last_name' placeholder="Enter you last_name"/>
<span style='color:red;' hidden id='taskErrorField-last_name'>This field is required</span>
<input type='tel' name='number' required id='number' placeholder="Enter you phone_number"/>
<span style='color:red;' hidden id='taskErrorField-number'>This field is required</span>
<textarea id="address" name="address" placeholder="enter your full address"></textarea>
<span style='color:red;' hidden id='taskErrorField-address'>This field is required</span>
<input type='email' name='email' required id='email' placeholder="Enter you email"/>
<span style='color:red;' hidden id='taskErrorField-email'>This field is required</span>
<input type='text' name='postal_code' required id='postal_code' placeholder="Enter you postal_code"/>
<span style='color:red;' hidden id='taskErrorField-postal_code'>This field is required</span>
<select id="city" name="city">
<option selected>Karachi</option>
<option >Lahore</option>
<option >Islamabed</option>
<option >Peshawar</option>
<option >Queta</option>
</select>
<span style='color:red;' hidden id='taskErrorField-city'>This field is required</span>
<select id="country" name="country">
<option selected>Pakistan</option>
</select>
<span style='color:red;' hidden id='taskErrorField-country'>This field is required</span>
<div>
<label class="paymentLabel blue-700">
<input type='radio' name='task' required id='payment_method' checked placeholder="Enter you payment_method"/>
Cash on Delivery
</label>
<label class="paymentLabel">
<input type='radio' name='task' required id='payment_method' placeholder="Enter you payment_method"/>
Online Payment
</label>
</div>
<button type='button' onclick="sumbitCheckout()" id='submitBtn' class='submitBtn' style='background-color: blue; color: white; padding: 10px; border: none; border-radius: 5px;'>Checkout</button>
</form>
<div>
<label>Cart Details</label>
<div id="boxList" style="width: 80%; display: flex; flex-direction: column; gap: 4px;" class="grid-item slideDown">
</div>
</div>
</div>
<div id='main'>
</div>
</div>
</body>
<script>
var initialCheckoutForm = {
first_name: '',
last_name: '',
phone_number: 0,
address: '',
email: '',
postal_code: '',
city: '',
country: '',
payment_method: '',
shipping_details: {
address:'',
city: '',
postal_code: '',
country: '',
phone_number: '',
reciever_name: ''
},
total_amount: '',
cart_details: [],
}
var checkoutForm = {
first_name: '',
last_name: '',
phone_number: 0,
address: '',
email: '',
postal_code: '',
city: '',
country: '',
payment_method: '',
shipping_details: {
address:'',
city: '',
postal_code: '',
country: '',
phone_number: '',
reciever_name: ''
},
total_amount: '',
cart_details: [],
}
var products = [
{
id: 1,
name: 'water bottle',
description: 'color: blue size: large',
price: 100,
discounted_price: 99,
},
{
id: 2,
name: 'mouse',
description: 'wired usb port',
price: 250,
discounted_price: 199,
},
{
id: 3,
name: 'watch',
description: 'color: black, watch type: smart, brand: apple',
price: 5000,
discounted_price: 4999,
},
{
id: 4,
name: 'infinix s23',
description: 'color blue, storage: 120gb ram: 2gb display: 6x4',
price: 25000,
discounted_price: 24999,
},
{
id: 5,
name: 'shoes',
description: 'size: 8, color blue, type: sports wear',
price: 3000,
discounted_price: 3000,
},
];
renderProducts();
function validateFields() {
var basicFields = [
'first_name',
'last_name',
'address',
'postal_code'
]
const basicValidation = () => {
var checkValidation = true;
basicFields.forEach((x)=> {
if(document.getElementById(x).value == '' || document.getElementById(x).value == null) {
checkValidation = false;
document.getElementById('taskErrorField-'+x).hidden = false
} else {
document.getElementById('taskErrorField-'+x).hidden = true
}
})
return checkValidation
}
var testBasicValidation = basicValidation();
if(!testBasicValidation) {
return false
}
const validatePhoneNumber = (phoneNumber) => {
const regex = /^(\+92|92|033|03)[ -]*\d{3}[ -]*\d{7}$/;
return regex.test(phoneNumber);
}
if (!validatePhoneNumber(document.getElementById('number').value)) {
document.getElementById('taskErrorField-number').hidden = false
return false
} else {
document.getElementById('taskErrorField-number').hidden = true
}
const validateEmail = (email) => {
const regex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
return regex.test(email);
}
if(!validateEmail(document.getElementById('email').value)) {
document.getElementById('taskErrorField-email').hidden = false;
return false;
} else {
document.getElementById('taskErrorField-email').hidden = true;
}
if(checkoutForm.cart_details.length == 0) {
alert("Please add items in your cart first");
return false
}
return true
}
function sumbitCheckout() {
debugger;
const checkForm = validateFields();
if(checkForm) {
alert("Thank you for shopping with us");
checkoutForm = { ...initialCheckoutForm };
checkoutForm.cart_details = [];
document.getElementById('ecommerceForm').reset();
renderCarts();
}
}
function addToCart(product) {
debugger;
var currentProduct = products.find((x) => { return x.id == product });
var currentQty = document.getElementById('qty'+product).value == '' || document.getElementById('qty'+product).value == null ? 1: document.getElementById('qty'+product).value;
cartExistIndex = checkoutForm.cart_details.findIndex((x)=> { return x.id == product} );
if(cartExistIndex != -1) {
checkoutForm.cart_details[cartExistIndex].quantity += parseInt(currentQty);
} else {
var cartItem = {
...currentProduct,
quantity: currentQty,
}
checkoutForm.cart_details.push(cartItem);
}
document.getElementById('qty'+product).value = '';
renderCarts();
}
/**
* This function is used to remove items from cart
* @params number id
* return void | null
*/
function removeItemCart(id) {
debugger;
var proIndex = checkoutForm.cart_details.findIndex((x) => {return x.id == id});
checkoutForm.cart_details.splice(proIndex,1);
renderCarts();
}
function renderCarts() {
let cartHtml = '';
var cartDiv = document.getElementById('boxList');
checkoutForm.cart_details.forEach((x) => {
var priceBox = x.discounted_price < x.price ? `<div class='actual-price'>
<span class='discount-price'> `+x.price+`</span>
`+x.discounted_price+`
</div>` : `<div class='actual-price'>
`+x.price+`
</div>` ;
cartHtml += `<div class="box" style="background-color: aqua;">
<div style="padding-left:10px; border-radius: 50%; padding-right: 10px; display:flex; justify-content: space-between; align-items: center;">
<p>`+ x.name +`</p>
`+ priceBox +`
<div>
Quantity `+ x.quantity +`
</div>
<div>
<button onclick="removeItemCart(`+ x.id +`)">
X
</button>
</div>
</div>
</div>`;
});
cartDiv.innerHTML = cartHtml;
}
function renderProducts() {
let productHtml = '';
var productDiv = document.getElementById('main');
products.forEach((x) => {
var priceBox = x.discounted_price < x.price ? `<div class='actual-price'>
<span class='text-blue-700'> `+x.price+`</span>
`+x.discounted_price+`
</div>` : `<div class='actual-price'>
`+x.price+`
</div>` ;
productHtml += `<div class='grid-item slideDown'>
<h1>`+ x.name +`</h1>
<p>`+ x.description +`</p>
`+ priceBox +`
<div class='addToCartBox'>
<input type='number' id='qty`+ x.id +`'/>
<button onclick='addToCart(`+x.id+`)'>Add to Cart</button>
</div>
</div>`;
});
productDiv.innerHTML = productHtml;
}
</script>
</html>