-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday.js
More file actions
277 lines (230 loc) · 8.7 KB
/
day.js
File metadata and controls
277 lines (230 loc) · 8.7 KB
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
// jQuery is a fast, small, and feature-rich JavaScript library.
// It makes things like HTML document traversal and manipulation, event handling, animation, and Ajax much simpler with an easy-to-use API that works across a multitude of browsers.
// https://jquery.com/
class Shop extends ShopDOM {
constructor(props) {
super(props);
this.init();
}
init() {
this.cartPrefix = "nerd-";
this.cartName = this.cartPrefix + "cart";
this.shippingRates = this.cartPrefix + "shippingRates";
this.total = this.cartPrefix + "total";
this.currency = "R$";
this.currencyString = "R$";
this.pagseguroCurrency = "BRL";
// https://dev.pagseguro.uol.com.br/
this.pagseguroEmail = "hemerson.lourenco@gmail.com";
this.pagseguroToken = '4248B835BCD34A7FA9494D1C2555BB34';
this.pagseguroURL = `https://ws.sandbox.pagseguro.uol.com.br/v2/transactions?email=${this.pagseguroEmail}&token=${this.pagseguroToken}`;
this.requiredFields = {
expression: { value: /^([\w-\.]+)@((?:[\w]+\.)+)([a-z]){2,4}$/ },
str: { value: "" }
};
this.createCart();
this.handleAddToCart();
this.handleCheckoutOrder();
this.emptyCart();
this.updateCart();
this.displayCart();
this.deleteProduct();
this.displayUserDetails();
this.populatePagSeguroForm();
}
createCart() {
if(this.storage.getItem(this.cartName) == null) {
const cart = { items: [] };
this.storage.setItem(this.cartName, this._toJSONString(cart));
this.storage.setItem(this.shippingRates, '0');
this.storage.setItem(this.total, '0');
}
}
handleAddToCart() {
this.$formAddToCart.each((i, el) => {
const $form = $(el);
const $product = $form.parent();
const price = this._convertString($product.data('price'));
const name = $product.data('name');
$form.on('submit', () => {
const qty = this._convertString($form.find('[data-js="qty"]').val());
const subTotal = qty * price;
const total = this._convertString(this.storage.getItem(this.total));
const sTotal = total + subTotal;
this.storage.setItem(this.total, sTotal);
this._addToCart({
product: name,
price: price,
qty: qty
});
const shipping = this._convertString(this.storage.getItem(this.shippingRates));
const shippingRates = this._calculateShipping(qty);
const totalShipping = shipping + shippingRates;
this.storage.setItem(this.shippingRates, totalShipping);
});
});
}
handleCheckoutOrder() {
if(this.$checkoutOrderForm.length) {
const $sameAsBilling = $('[data-js="sameAsBilling"]');
$sameAsBilling.on('change', evt => {
const $check = $(evt.target);
if( $check.prop( "checked" ) ) {
$('[data-js="fieldsetShipping"]').slideUp( "normal" );
} else {
$('[data-js="fieldsetShipping"]').slideDown( "normal" );
}
});
this.$checkoutOrderForm.on('submit', evt => {
const $form = $(evt.target);
const valid = this._validateForm($form);
return !valid ? valid : this._saveFormData($form);
});
}
}
emptyCart() {
if(this.$emptyCartBtn.length) {
this.$emptyCartBtn.on('click', () => {
this._emptyCart();
});
}
}
updateCart() {
if(this.$updateCartBtn.length) {
this.$updateCartBtn.on('click', () => {
const $rows = this.$formCart.find('tbody tr');
// var cart = this.storage.getItem(this.cartName);
// var shippingRates = this.storage.getItem(this.shippingRates);
// var total = this.storage.getItem(this.total);
const updatedCart = { items: [] };
let updatedTotal = 0;
let totalQty = 0;
$rows.each((i, el) => {
const $row = $(el);
const pname = $.trim($row.find('.pname').text());
const pqty = this._convertString($row.find('.pqty > .qty').val());
const pprice = this._convertString(this._extractPrice( $row.find('.pprice')));
const cartObj = {
product: pname,
price: pprice,
qty: pqty
};
updatedCart.items.push(cartObj);
const subTotal = pqty * pprice;
updatedTotal += subTotal;
totalQty += pqty;
});
this.storage.setItem(this.total, this._convertNumber( updatedTotal));
this.storage.setItem(this.shippingRates, this._convertNumber(this._calculateShipping(totalQty)));
this.storage.setItem(this.cartName, this._toJSONString(updatedCart));
});
}
}
displayCart() {
if (this.$formCart.length) {
const cart = this._toJSONObject(this.storage.getItem(this.cartName));
const items = cart.items;
const $tableCart = this.$formCart.find('[data-js="shoppingCart"]');
const $tableCartBody = $tableCart.find('tbody');
if (items.length === 0) {
$tableCartBody.html('');
} else {
for (let i = 0; i < items.length; ++i) {
const item = items[i];
const product = item.product;
const price = this.currency + " " + item.price;
const qty = item.qty;
const html = `
<tr>
<td class='pname'>${product}</td>
<td class='pqty'><input type='text' value='${qty}' class='qty'/></td>
<td class='pprice'>${price}</td>
<td class='pdelete'><a href='' data-product='${product}'>×</a></td>
</tr>
`;
console.log($tableCart.find("tbody"));
$tableCartBody.html(`${$tableCartBody.html()} ${html}`);
}
}
if (items.length == 0) {
this.$subTotal[0].innerHTML = this.currency + " " + 0.00;
} else {
const total = this.storage.getItem(this.total);
this.$subTotal[0].innerHTML = this.currency + " " + total;
}
} else if (this.$checkoutCart.length) {
const checkoutCart = this._toJSONObject(this.storage.getItem(this.cartName));
const cartItems = checkoutCart.items;
const $cartBody = this.$checkoutCart.find('tbody');
if (cartItems.length > 0) {
for (let j = 0; j < cartItems.length; ++j) {
const cartItem = cartItems[j];
const cartProduct = cartItem.product;
const cartPrice = this.currency + " " + cartItem.price;
const cartQty = cartItem.qty;
const cartHTML = `
<tr>
<td class='pname'>${cartProduct}</td>
<td class='pqty'>${cartQty}</td>
<td class='pprice'>${cartPrice}</td>
</tr>
`;
$cartBody.html(`${$cartBody.html()} ${cartHTML}`);
}
} else {
$cartBody.html('');
}
if(cartItems.length > 0) {
const cartTotal = this.storage.getItem(this.total);
const cartShipping = this.storage.getItem(this.shippingRates);
const subTot = this._convertString(cartTotal) + this._convertString(cartShipping);
this.$subTotal[0].innerHTML = this.currency + " " + this._convertNumber(subTot);
this.$shipping[0].innerHTML = this.currency + " " + cartShipping;
} else {
this.$subTotal[0].innerHTML = this.currency + " " + 0.00;
this.$shipping[0].innerHTML = this.currency + " " + 0.00;
}
}
}
deleteProduct() {
if (this.$formCart.length) {
const cart = this._toJSONObject(this.storage.getItem( this.cartName));
const items = cart.items;
$(document).on('click', '.pdelete a', evt => {
evt.preventDefault();
const productName = $(evt.target).data('product');
let newItems = [];
for (let i = 0; i < items.length; ++i) {
const item = items[i];
const product = item.product;
if (product == productName) {
items.splice(i, 1);
}
}
newItems = items;
const updatedCart = { items: newItems };
let updatedTotal = 0;
let totalQty = 0;
if (newItems.length == 0) {
updatedTotal = 0;
totalQty = 0;
} else {
for (let j = 0; j < newItems.length; ++j) {
const prod = newItems[j];
const sub = prod.price * prod.qty;
updatedTotal += sub;
totalQty += prod.qty;
}
}
this.storage.setItem(this.total, this._convertNumber( updatedTotal));
this.storage.setItem(this.shippingRates, this._convertNumber(this._calculateShipping(totalQty)));
this.storage.setItem(this.cartName, this._toJSONString(updatedCart));
$(evt.target).parents('tr').remove();
this.$subTotal[0].innerHTML = this.currency + " " + this.storage.getItem(this.total);
});
}
}
}
$(() => {
new Shop('[data-js="shop"]');
});