forked from kmcgrady/shopping-cart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcart.js
154 lines (134 loc) · 5.21 KB
/
cart.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
(function() {
var products = [{
name: "Rock&roll",
image: "item1.jpg",
price: "12.50"
}, {
name: "Pop",
image: "item1.jpg",
price: "9.50"
}, {
name: "Hard rock",
image: "item1.jpg",
price: "78.2"
}];
var state = {
orderItems: []
};
var app = {
initialize: function() {
this.setupListeners();
},
setupListeners: function() {
$(document).ready(app.onload);
$("#shipping-address-show").click(app.showShipTo);
$("#shipping-address-apply").click(app.applyShipTo);
$("#send").click(app.send);
},
send: function(e) {
let text = $("#shipToForm").serialize();
alert("The order has been sent: " + text);
$('#cartBody')
.empty()
.html('<p>The order has been sent. Thank you.</p>');
$(".addToCart").attr('disabled', '');
},
addToCart: function(e) {
const id = $(e.target).attr("data-id");
let item = state.orderItems.find(p=>p.id == id);
if (!item) {
item = {
id: id,
item: products[id],
num: 0
};
state.orderItems.push(item);
}
item.num++;
app.refreshCart();
},
removeFromCart: function(e) {
const id = $(e.target).attr("data-id");
let item = state.orderItems.find(p=>p.id == id);
if (item)
item.num--;
state.orderItems = state.orderItems.filter(x=>x.num > 0);
app.refreshCart();
},
refreshCart: function() {
const cart = $("#cart");
cart.empty();
for (let i in state.orderItems) {
let item = state.orderItems[i].item
, num = state.orderItems[i].num
, id = state.orderItems[i].id;
cart.append(`<p>
<button data-id="${id}" class="btn btn-danger btn-sm removeFromCart glyphicon glyphicon-minus"/>
<button data-id="${id}" class="btn btn-primary btn-sm addFromCart glyphicon glyphicon-plus"/>
${num} x ${item.name} $${item.price}
</p>`);
}
$(".addFromCart").click(app.addToCart);
$(".removeFromCart").click(app.removeFromCart);
const totalReport = $("#totalreport");
totalReport.empty();
if (state.orderItems.length > 0) {
const subTotal = state.orderItems.map(x=>parseFloat(x.item.price) * x.num).reduce((p,c)=>p + c, 0);
const tax = subTotal * 0.1;
const total = subTotal + tax;
totalReport.append(`<p>Subtotal:$${subTotal.toFixed(2)}</p>
<p>Tax:$${tax.toFixed(2)}</p>
<p>Total:<b>$${total.toFixed(2)}</b></p>`);
}
},
onload: function() {
for (var i in products) {
$(`<div class="col col-xs-12 col-md-6 col-lg-4">
<div>
<img src="Products/${products[i].image}">
</div>
<button data-id="${i}" class="btn btn-default addToCart">Add to Cart</button>
<h2 class="name">${products[i].name}</h2>
<p class="price">$${products[i].price}</p>
</div>`).appendTo($("#products"));
}
$(".addToCart").click(app.addToCart);
},
showShipTo: function() {
$("#myModal").modal("show");
},
validate: function() {
let formValid = true;
$('input').each((i,el)=>{
let formGroup = $(el).parents('.form-group');
let glyphicon = formGroup.find('.form-control-feedback');
if (el.checkValidity()) {
formGroup.addClass("has-success").removeClass("has-error");
glyphicon.addClass("glyphicon-ok").removeClass("glyphicon-remove");
} else {
formGroup.addClass('has-error').removeClass('has-success');
glyphicon.addClass('glyphicon-remove').removeClass('glyphicon-ok');
formValid = false;
}
}
);
if (formValid) {
$("#myModal").modal("hide");
}
return formValid;
},
applyShipTo: function() {
let validShipTo = app.validate();
if (!validShipTo) {
$("#shipping-address-show").removeClass("btn-success").addClass("btn-warning");
$("#shipping-address-icon").removeClass("glyphicon-ok").addClass("glyphicon-exclamation-sign");
$('#send').attr("disabled", true);
return;
}
$('#send').removeAttr('disabled');
$("#shipping-address-show").removeClass("btn-warning").addClass("btn-success");
$("#shipping-address-icon").removeClass("glyphicon-exclamation-sign").addClass("glyphicon-ok");
}
}
app.initialize();
}());