-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
134 lines (116 loc) · 3.32 KB
/
index.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 { menuArray } from './data.js'
const menuItems = document.getElementById('menu-items')
const orderItems = document.getElementById('order-items')
const modal = document.getElementById('modal')
const payBtn = document.getElementById('pay')
const confirmationEl = document.getElementById('confirmation')
let orders = []
document.addEventListener('click', (e) => {
if (e.target.dataset.add) addItem(e.target.dataset.add)
if (e.target.dataset.remove) removeItem(e.target.dataset.remove)
if (e.target.id === 'checkout-btn') paymentProcess()
})
function addItem(name) {
const targetItem = menuArray.find((item) => {
return item.name === name
})
orderItems.classList.remove('hidden')
orders.push(targetItem)
render()
}
function removeItem(name) {
const targetItem = menuArray.find((item) => {
return item.name === name
})
orders = orders.filter((item) => {
return item.name !== targetItem.name
})
if (orders.length === 0) orderItems.classList.add('hidden')
render()
}
function cartOrder() {
let total = 0
let orderHtml = `<h1 id="order-details" class="container">Your order</h1>`
orders.forEach((item) => {
total += item.price
orderHtml += `
<div class="cart container">
<div class="order-cart">
<div class="item-cart">
<div class="item-name">${item.name}</div>
<div class="item-remove">
<button class="remove-btn" data-remove="${item.name}">remove</button>
</div>
</div>
<div class="item-price">$${item.price}</div>
</div>
</div>
`
})
orderHtml += `
<div class="order-details container">
<div class="order-checkout">
<div class="total-text">Total price:</div>
<div class="total-amount">$${total}</div>
</div>
</div>
<div class="checkout container">
<button id="checkout-btn">Complete order</button>
</div>
`
return orderHtml
}
form.addEventListener('submit', (e) => {
e.preventDefault()
const formData = new FormData(e.target)
const name = formData.get('name')
purchaseDetails(name)
})
function paymentProcess() {
modal.classList.remove('hidden')
orderItems.classList.add('hidden')
payBtn.addEventListener('click', () => {
if (!form.checkValidity()) return
modal.classList.add('hidden')
orders = []
})
}
function purchaseDetails(name) {
setTimeout(() => {
let confirmation = `<div class="confirmation-details container">
<div class="confirmation-text">Thanks, ${name}! Your order is on its way!</div>
</div>`
confirmationEl.innerHTML = confirmation
setTimeout(() => {
confirmationEl.innerHTML = ''
}, 3000)
}, 100)
}
function menu() {
let menuHtml = ``
menuArray.forEach((item) => {
menuHtml += `
<div class="order container">
<div class="menu">
<div class="emoji">
<img src="${item.img}" alt="${item.name}" />
</div>
<div class="order-details">
<div class="name">${item.name}</div>
<div class="ingredients">${item.ingredients}</div>
<div class="price">$${item.price}</div>
</div>
</div>
<div class="select-order">
<button class="add-btn" data-add="${item.name}">+</button>
</div>
</div>
`
})
return menuHtml
}
function render() {
menuItems.innerHTML = menu()
orderItems.innerHTML = cartOrder()
}
render()