-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
134 lines (118 loc) · 4.44 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
import { cookieMenu } from '/data.js'
const menuContainer = document.getElementById('menu-container')
const orderContainer = document.getElementById('order-container')
const payModal = document.getElementById('payment-modal')
const payForm = document.getElementById('pay-form')
//use uuids to identify different elements of the order basket ??
//let uuidCounter = 0
//One event listener to group all clicks using data attributes
document.addEventListener('click', (e) => {
if (e.target.dataset.select) {
handleSelectionClick(e.target.dataset.select)
} else if (e.target.dataset.remove) {
handleRemoveClick(e.target.dataset.remove)
} else if (e.target.id === 'order-btn') {
handleOrderClick()
}
})
//Form needs 'submit' event listener so required input fields
//work. innerHTML is not used when input comes from user.
payForm.addEventListener('submit', (e) => {
e.preventDefault()
const payFormData = new FormData(payForm)
const name = payFormData.get('name')
payModal.style.display = 'none'
const nameDisplay = document.createElement('p')
nameDisplay.classList.add('order-confirm-msg')
nameDisplay.textContent = `Thanks, ${name}! Your order is on its way!`
orderContainer.innerHTML = `
<div class="order-confirm"></div>
`
document.querySelector('.order-confirm').append(nameDisplay)
setTimeout( () => {
orderArray = []
orderContainer.innerHTML = ``
}, 3000)
})
//initialize orderArray
let orderArray = []
function handleOrderClick() {
payModal.style.display = 'flex'
}
// Tests that the id property in data.js is the same as id stored in
//cookieData, which comes from select-button data attribute.
// Use [0] because we want to target the object in array of which there
//is only one (id is unique). Adds targetCookieObj to orderArray.
function handleSelectionClick(cookieData) {
const targetCookieObj = cookieMenu.filter((cookie) => {
return cookie.id == cookieData
})[0]
orderArray.push(targetCookieObj)
updateOrderHtml()
renderOrder()
}
function handleRemoveClick(itemIndex) {
orderArray.splice(itemIndex, 1)
// as an alternative to rebuilding the html with renderOrder(),
// the following could be used to just remove one element at a time
// (in this case, respective itemIndex needs to be figured out via item's uuid)
//let removedItem = document.querySelectorAll('.item-review')[itemIndex]
//removedItem.remove()
updateOrderHtml()
renderOrder()
}
console.log(orderArray)
function renderOrder() {
const reviewOrder = document.querySelector('.order-list')
reviewOrder.innerHTML = ""
orderArray.forEach(item => {
reviewOrder.innerHTML += `
<div class="item-review">
<div class="item-remove-pair">
<p class="cookie-review">${item.name}</p>
<button type="button" class="remove-btn" data-remove="
${orderArray.indexOf(item)}">remove</button>
</div>
<p class="price-review">$${item.price}</p>
</div>
`
})
}
function updateOrderHtml() {
if (orderArray.length > 0) {
orderContainer.style.display = 'flex'
}
else {
orderContainer.style.display = 'none'
}
const totalPrice = orderArray.reduce((total, currentItem) =>
total + currentItem.price, 0
)
const totalDisplay = document.querySelector('.total-price')
totalDisplay.textContent = `$${totalPrice}`
console.log(totalPrice)
}
function getCookieHtml(cookieMenu) {
const cookieHtml = cookieMenu.map((cookie) => {
const renderedIngredients = cookie.ingredients.map((ingredient) => {
return ingredient
})
return `
<div class="cookie-container">
<img src="${cookie.img}" class="cookie-img">
<div class="price-info-container">
<p class="cookie-name">${cookie.name}</p>
<p class="ingredients">${renderedIngredients.join(', ')}</p>
<p class="price">$${cookie.price}</p>
</div>
<button type="button" class="select-btn"
data-select="${cookie.id}">+</button>
</div>
<div class="divider-container">
<div class="divider"></div>
</div>
`
})
return cookieHtml.join('')
}
menuContainer.innerHTML = getCookieHtml(cookieMenu)