-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpages_manager_AddMenuItem.js.html
269 lines (224 loc) · 10.3 KB
/
pages_manager_AddMenuItem.js.html
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: pages/manager/AddMenuItem.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: pages/manager/AddMenuItem.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>import React, { useState } from "react";
import "../../css/AddMenuItem.css";
import { HOST } from "../../host";
import ManagerPop from "../../components/managerErrorPop";
/**
A React component that allows the user to add ingredients and create a menu item.
@returns {JSX.Element} A React component that includes input fields for adding ingredients, and creating a menu item.
*/
function MenuAdder() {
const [ingredient, setIngredient] = useState("");
const [quantity, setQuantity] = useState("");
const [itemName, setItemName] = useState("");
const [price, setPrice] = useState("");
const [ingredients, setIngredients] = useState([]);
const [ingDone, setIngDone] = useState(false);
const [showManagerPop, setManagerPop] = useState(false);
const [ErrorPrompt, setErrorPrompt] = useState("");
/**
Function that adds a new ingredient to the array of ingredients and clears the input fields.
*/
const handleAddIngredient = () => {
const newIngredient = { name: ingredient, quantity: quantity };
setIngredients([...ingredients, newIngredient]);
setIngredient("");
setQuantity("");
};
/**
Function that prompts the user if they are missing ingredients.
*/
const handleMissingIngredients = () => {
setErrorPrompt("missIng");
};
/**
Function that displays the help menu to the user.
*/
const helpScreen = () => {
setErrorPrompt("helpMenu");
setManagerPop(true);
}
/**
Function that sends the list of ingredients to the server and completes the process of adding a new menu item.
*/
const handleDoneIngredients = () => {
console.log(ingredients);
fetch(`${HOST}/addMenu`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ ingredients })
}).then(response => response.json())
.then(data => {
console.log(data);
if (data.message === "fault") {
console.log("fault");
}
const form = document.getElementById("menuadder-form");
form.innerHTML = ``;
setIngDone(true);
})
.catch(error => {
console.error(error)
console.log("paad");
const form = document.getElementById("menuadder-form");
handleMissingIngredients();
setManagerPop(true);
form.innerHTML = `
<label htmlFor="ingredient">Ingredient:</label>
<input type="text" id="ingredient" name="ingredient" value="${ingredient}" onChange={handleIngredientChange} required />
<label htmlFor="quantity">Quantity:</label>
<input type="number" id="quantity" name="quantity" value="${quantity}" onChange={handleQuantityChange} required />
<button type="button" onClick={handleAddIngredient}>Add Ingredient</button>
`;
setManagerPop(true);
});
};
/**
Handles adding an order to the menu.
Sends an HTTP POST request to the server with the item name and price.
@function
@returns {void}
*/
const handleAddOrder = () => {
// Do something with the item name and price (e.g. add them to an order list)
fetch(`${HOST}/addmenu/completeMenu`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ 'name': itemName, 'price': price })
}).then(response => response.json())
.then(data => { console.log(data); })
.catch(error => { console.error(error); });
// Reset the form to add ingredients again
const form = document.getElementById("menuadder-form");
form.innerHTML = `
<label htmlFor="ingredient">Ingredient:</label>
<input type="text" id="ingredient" name="ingredient" value="${ingredient}" onChange={handleIngredientChange} required />
<label htmlFor="quantity">Quantity:</label>
<input type="number" id="quantity" name="quantity" value="${quantity}" onChange={handleQuantityChange} required />
<button type="button" onClick={handleAddIngredient}>Add Ingredient</button>
`;
};
/**
Handles a change in the ingredient input field and updates the state.
@function
@param {Object} event - The event object.
@returns {void}
*/
const handleIngredientChange = (event) => {
setIngredient(event.target.value);
};
/**
Handles a change in the quantity input field and updates the state.
@function
@param {Object} event - The event object.
@returns {void}
*/
const handleQuantityChange = (event) => {
setQuantity(event.target.value);
};
/**
Handles a change in the item name input field and updates the state.
@function
@param {Object} event - The event object.
@returns {void}
*/
const handleItemNameChange = (event) => {
setItemName(event.target.value);
};
/**
Handles a change in the price input field and updates the state.
@function
@param {Object} event - The event object.
@returns {void}
*/
const handlePriceChange = (event) => {
setPrice(event.target.value);
};
return (
<>
{/* <button type = "button" class="my-button" > Help</button> */}
<div id="menuadder">
<div className="help">
<button type="button" className="help-btn" onClick={helpScreen} > Help</button>
</div>
<h3>Add Ingredients</h3>
<form id="menuadder-form">
<label htmlFor="ingredient">Ingredient:</label>
<input type="text" id="ingredient" name="ingredient" value={ingredient} onChange={handleIngredientChange} required />
<label htmlFor="quantity">Quantity:</label>
<input type="number" id="quantity" name="quantity" value={quantity} onChange={handleQuantityChange} required />
<button type="button" onClick={handleAddIngredient}>Add Ingredient</button>
{ingredients.length > 0 && (
<div>
<h3>Added Ingredients:</h3>
<ul>
{ingredients.map((ingredient, index) => (
<li key={index}>{ingredient.name} - {ingredient.quantity} | </li>
))}
</ul>
</div>
)}
{ingredients.length > 0 && (
<button type="button" onClick={handleDoneIngredients}>Done with Ingredients</button>
)}
{ingDone && (
<div>
<label htmlFor="item-name">Item Name:</label>
<input type="text" id="item-name" name="item-name" value={itemName} onChange={handleItemNameChange} required />
<label htmlFor="price">Price:</label>
<input type="number" id="price" name="price" value={price} onChange={handlePriceChange} required />
<button type="button" onClick={handleAddOrder}>Add Order</button>
</div>
)}
</form>
<br></br>
</div>
<div>
{showManagerPop && (
<ManagerPop
ErrorPrompt={ErrorPrompt}
setErrorPrompt={setErrorPrompt}
setManagerPop={setManagerPop}
/>
)}
</div>
</>
);
}
export default MenuAdder;
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.html#App">App</a></li><li><a href="global.html#Cashier">Cashier</a></li><li><a href="global.html#CashierDrink">CashierDrink</a></li><li><a href="global.html#CashierHeader">CashierHeader</a></li><li><a href="global.html#CashierMeal">CashierMeal</a></li><li><a href="global.html#CashierSauce">CashierSauce</a></li><li><a href="global.html#CashierSeasonal">CashierSeasonal</a></li><li><a href="global.html#Customer">Customer</a></li><li><a href="global.html#CustomerDrink">CustomerDrink</a></li><li><a href="global.html#ExcessReport">ExcessReport</a></li><li><a href="global.html#HOST">HOST</a></li><li><a href="global.html#Home">Home</a></li><li><a href="global.html#Inventory">Inventory</a></li><li><a href="global.html#Login">Login</a></li><li><a href="global.html#Logout">Logout</a></li><li><a href="global.html#Manager">Manager</a></li><li><a href="global.html#MenuAdder">MenuAdder</a></li><li><a href="global.html#MenuItems">MenuItems</a></li><li><a href="global.html#Orders">Orders</a></li><li><a href="global.html#PrivateRouteCashier">PrivateRouteCashier</a></li><li><a href="global.html#PrivateRouteManager">PrivateRouteManager</a></li><li><a href="global.html#UserInputLogger">UserInputLogger</a></li><li><a href="global.html#getMenuEffect">getMenuEffect</a></li><li><a href="global.html#getMenuPrice">getMenuPrice</a></li><li><a href="global.html#handleComplete">handleComplete</a></li><li><a href="global.html#handleIdChange">handleIdChange</a></li><li><a href="global.html#handleInventory">handleInventory</a></li><li><a href="global.html#handleNameChange">handleNameChange</a></li><li><a href="global.html#handleNewOrder">handleNewOrder</a></li><li><a href="global.html#handlePriceChange">handlePriceChange</a></li><li><a href="global.html#handleSubmit">handleSubmit</a></li><li><a href="global.html#local">local</a></li><li><a href="global.html#root">root</a></li><li><a href="global.html#useEffect">useEffect</a></li><li><a href="global.html#useGoogleSignIn">useGoogleSignIn</a></li><li><a href="global.html#useLocalState">useLocalState</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Tue May 02 2023 22:48:22 GMT-0500 (Central Daylight Time)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>