-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
179 lines (141 loc) · 5.15 KB
/
app.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
const products = { categories };
let cart = {
items: [],
total: 0
};
const shoppingCart = { cart };
window.addEventListener('DOMContentLoaded', (e) => {
handleNavigation();
// use Amp decision to take action on button color
const ctaBtn = document.querySelector('.ctaBtn');
ctaBtn.addEventListener('click', (e) => {
const label = e.target.textContent;
switch (label) {
case 'Checkout':
location.hash = '/checkout';
break;
case 'Submit Order':
location.hash = '/thank_you';
break;
case 'Continue Shopping':
location.hash = '';
cart = {
items: [],
total: 0
};
break;
default:
location.hash = '/cart';
}
});
});
window.addEventListener('hashchange', (e) => {
handleNavigation();
});
function handleNavigation() {
let html;
const ctaBtn = document.querySelector('.ctaBtn');
const main = document.querySelector('.main');
switch (location.hash) {
case '/', '':
main.innerHTML = Handlebars.templates.product(products);
listenersForCartButtons();
handleActions();
syncCartWithView();
break;
case '#/cart':
ctaBtn.textContent = 'Checkout';
main.innerHTML = Handlebars.templates.cart(shoppingCart);
const cartBtn = document.querySelector('.cartBtn');
cartBtn.addEventListener('click', (e) => { ctaBtn.click(); });
break;
case '#/checkout':
ctaBtn.textContent = 'Submit Order';
main.innerHTML = Handlebars.templates.checkout(shoppingCart);
const shipping = document.querySelector('.shipping');
if (decision.rushShipping === 0) {
shipping.style.visibility = 'hidden';
} else {
shipping.textContent = `Rush Shipping if you order in ${decision.rushShipping} minutes.`;
shipping.style.visibility = 'visible';
}
document.querySelector('.checkoutBtn').style.backgroundColor = decision.ctaColor;
break;
case '#/thank_you':
main.innerHTML = Handlebars.templates.thankYou(shoppingCart);
ctaBtn.textContent = 'Continue Shopping';
break;
default:
main.innerHTML = Handlebars.templates.product(products);
}
}
function handleActions() {
const buyButton = document.querySelectorAll('.buyNow');
const quickButton = document.querySelectorAll('.quick');
document.querySelectorAll('.btn').forEach(btn => {
btn.style.borderColor = decision.ctaColor;
btn.style.color = decision.ctaColor;
});
buyButton.forEach(btn => { btn.style.visibility = 'hidden'; });
quickButton.forEach(btn => { btn.style.visibility = 'hidden'; });
if (decision.btnSequence === 'addBuy') {
buyButton.forEach(btn => { btn.style.visibility = 'visible'; });
} else if (decision.btnSequence === 'addQuick') {
quickButton.forEach(btn => {btn.style.visibility = 'visible'; });
} else if (decision.btnSequence === 'addBuyQuick') {
buyButton.forEach(btn => { btn.style.visibility = 'visible'; });
quickButton.forEach(btn => { btn.style.visibility = 'visible'; });
}
}
function listenersForCartButtons() {
document.querySelectorAll('.btn').forEach(element => {
element.addEventListener('click', (e) => {
const target = e.target;
let existingItem = false;
let cartSize = 0;
let total = 0;
cart.items.forEach(item => {
if (item.id === target.dataset.id) {
item.quantity = parseInt(item.quantity, 10) + 1;
item.subtotal = parseFloat(item.quantity, 10) * parseFloat(item.price, 10).toFixed(2);
existingItem = true;
const targetQuantity = target.parentElement.parentElement.querySelector('.quantity');
targetQuantity.textContent = `Quantity: ${item.quantity}`;
}
cartSize += parseInt(item.quantity, 10);
total += parseFloat(item.quantity, 10) * parseFloat(item.price, 10);
});
if (!existingItem) {
cart.items.push({
id: target.dataset.id,
name: target.dataset.name,
description: target.dataset.description,
price: target.dataset.price,
quantity: 1,
subtotal: 1 * parseFloat(target.dataset.price, 10)
});
const targetQuantity = target.parentElement.parentElement.querySelector('.quantity');
targetQuantity.textContent = `Quantity: 1`;
cartSize += 1;
total += 1 * parseFloat(target.dataset.price, 10);
}
cart.total = parseFloat(total, 10).toFixed(2);
if (target.textContent === 'Buy Now') {
location.hash = '/checkout';
} else if (target.textContent === 'Quick Checkout') {
location.hash = '/thank_you';
}
document.querySelector('.ctaBtn').textContent = `Cart - ${cartSize} Items`;
});
});
}
function syncCartWithView() {
let numItems = 0;
cart.items.forEach(item => {
numItems += parseInt(item.quantity, 10);
const productIdElement = document.querySelector('[data-id=\"' +item.id+ '\"]');
const targetQuantity = productIdElement.parentElement.parentElement.querySelector('.quantity');
targetQuantity.textContent = `Quantity: ${item.quantity}`;
});
document.querySelector('.ctaBtn').textContent = `Cart - ${numItems} Items`;
}