-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
149 lines (105 loc) · 3.64 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { menuData } from '../data.js'
const renderMenu = document.getElementById('renderMenu')
const renderOrder = document.getElementById('renderOrder')
const orderBtn = document.getElementById('orderBtn')
const renderTotal = document.getElementById('renderTotal')
const removeBtn = document.getElementById('removeBtn')
const modalBox = document.getElementById('modalBox')
const modalPayBtn = document.getElementById('modal-pay-btn')
const foodOrderArray = []
function render() {
menuData.forEach((item) => {
renderMenu.innerHTML += `
<div class="post flex items-center justify-between m-4 p-4 gap-16">
<div>
<img src=${item.image} class="icon">
</div>
<div class="flex-col smytheDark">
<p class="text-3xl">${item.name}</p>
<p class="text-lg text-gray-600">${item.description}</p>
<p class="text-2xl">$${item.price}</p>
</div>
<div class="flex justify-items-end">
<img src="images/addbtn.png" class="h-12 w-12 cursor-pointer hover:bg-red-200" data-add="${item.uuid}">
</div>
</div>
`
})
}
function handleClick(foodId) {
const targetFoodObj = menuData.filter((food) => {
return food.uuid === foodId
})[0]
// console.log(targetFoodObj)
renderOrder.classList.remove('hidden')
orderBtn.classList.remove('hidden')
renderTotal.classList.remove('hidden')
renderOrder.innerHTML = ''
foodOrderArray.push({
name: targetFoodObj.name,
price: targetFoodObj.price,
uuid: targetFoodObj.uuid
})
foodOrderArray.forEach((food) => {
renderOrder.innerHTML += `
<div class="smytheDark text-4xl flex items-center justify-between m-4 p-4 gap-16">
<p>${food.name} $${food.price}</p> <button data-remove=${food.uuid} class="text-gray-500 text-xl border-none">remove</button>
</div>
`
})
let sum = 0
foodOrderArray.forEach((order) => {
sum += order.price
})
renderTotal.innerHTML = `
<div class="post"></div>
<p>Total Price: $${sum}</p>
`
}
function deleteOrder(foodId) {
const targetFoodObj = foodOrderArray.filter((food) => {
return food.uuid === foodId
})[0]
for(let i = 0; i < foodOrderArray.length; i++) {
if (foodOrderArray[i] === targetFoodObj) {
foodOrderArray.splice(i, 1)
}
}
renderOrder.innerHTML = ''
foodOrderArray.forEach((food) => {
renderOrder.innerHTML += `
<div class="smytheDark text-4xl flex items-center justify-between m-4 p-4 gap-16">
<p>${food.name} $${food.price}</p> <button data-remove=${food.uuid} class="text-gray-500 text-xl border-none">remove</button>
</div>
`
let sum = 0
foodOrderArray.forEach((order) => {
sum += order.price
})
renderTotal.innerHTML = `
<div class="post"></div>
<p>Total Price: $${sum}</p>
`
})
}
function finalMessage() {
renderTotal.innerHTML += `<h1 class="flex-row justify-content align-content w-96 h-32 bg-green-300 text-black text-4xl ml-4 p-4 text-center">Thanks for ordering!</h1>`
}
document.addEventListener('click', (e) => {
if(e.target.dataset.add){
handleClick(e.target.dataset.add)
}
if(e.target.dataset.remove){
deleteOrder(e.target.dataset.remove)
}
})
orderBtn.addEventListener('click', (order) => {
modalBox.classList.remove('hidden')
console.log('ordered!')
})
modalPayBtn.addEventListener('click', (submit) => {
modalBox.classList.add('hidden')
submit.preventDefault()
finalMessage()
})
render()