components.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. let recipeDetailsComp = {
  2. recipe: {},
  3. display: function(recipe){
  4. this.recipe = recipe;
  5. openSidebar(document.querySelector("#recipeDetails"));
  6. document.querySelector("#recipeName").style.display = "block";
  7. document.querySelector("#recipeNameIn").style.display = "none";
  8. document.querySelector("#recipeDetails h1").innerText = recipe.name;
  9. let ingredientList = document.querySelector("#recipeIngredientList");
  10. while(ingredientList.children.length > 0){
  11. ingredientList.removeChild(ingredientList.firstChild);
  12. }
  13. let template = document.querySelector("#recipeIngredient").content.children[0];
  14. for(let i = 0; i < recipe.ingredients.length; i++){
  15. ingredientDiv = template.cloneNode(true);
  16. ingredientDiv.children[0].innerText = recipe.ingredients[i].ingredient.name;
  17. ingredientDiv.children[2].innerText = `${recipe.ingredients[i].quantity} ${recipe.ingredients[i].ingredient.unit}`;
  18. ingredientDiv._id = recipe.ingredients[i].ingredient._id;
  19. ingredientDiv.name = recipe.ingredients[i].ingredient.name;
  20. ingredientList.appendChild(ingredientDiv);
  21. }
  22. document.querySelector("#addRecIng").style.display = "none";
  23. let price = document.querySelector("#recipePrice");
  24. price.children[1].style.display = "block";
  25. price.children[2].style.display = "none";
  26. price.children[1].innerText = `$${(recipe.price / 100).toFixed(2)}`;
  27. document.querySelector("#recipeUpdate").style.display = "none";
  28. },
  29. edit: function(){
  30. let ingredientDivs = document.querySelector("#recipeIngredientList");
  31. if(merchant.pos === "none"){
  32. let name = document.querySelector("#recipeName");
  33. let nameIn = document.querySelector("#recipeNameIn");
  34. name.style.display = "none";
  35. nameIn.style.display = "block";
  36. nameIn.placeholder = name.innerText;
  37. let price = document.querySelector("#recipePrice");
  38. price.children[1].style.display = "none";
  39. price.children[2].style.display = "block";
  40. price.children[2].placeholder = price.children[1].innerText;
  41. }
  42. for(let i = 0; i < ingredientDivs.children.length; i++){
  43. let div = ingredientDivs.children[i];
  44. div.children[2].innerText = this.recipe.ingredients[i].ingredient.unit;
  45. div.children[1].style.display = "block";
  46. div.children[1].placeholder = this.recipe.ingredients[i].quantity;
  47. div.children[3].style.display = "block";
  48. div.children[3].onclick = ()=>{div.parentElement.removeChild(div)};
  49. }
  50. document.querySelector("#addRecIng").style.display = "flex";
  51. document.querySelector("#recipeUpdate").style.display = "flex";
  52. },
  53. update: function(){
  54. let updatedRecipe = {
  55. _id: this.recipe._id,
  56. name: document.querySelector("#recipeNameIn").value || this.recipe.name,
  57. price: Math.round((document.querySelector("#recipePrice").children[2].value * 100)) || this.recipe.price,
  58. ingredients: []
  59. }
  60. let divs = document.querySelector("#recipeIngredientList").children;
  61. for(let i = 0; i < divs.length; i++){
  62. if(divs[i].name === "new"){
  63. updatedRecipe.ingredients.push({
  64. ingredient: divs[i].children[0].value,
  65. quantity: divs[i].children[1].value
  66. })
  67. }else{
  68. updatedRecipe.ingredients.push({
  69. ingredient: divs[i]._id,
  70. quantity: divs[i].children[1].value || divs[i].children[1].placeholder
  71. });
  72. }
  73. }
  74. if(validator.recipe(updatedRecipe)){
  75. fetch("/recipe/update", {
  76. method: "PUT",
  77. headers: {
  78. "Content-Type": "application/json;charset=utf-8"
  79. },
  80. body: JSON.stringify(updatedRecipe)
  81. })
  82. .then((response) => response.json())
  83. .then((response)=>{
  84. if(typeof(response) === "string"){
  85. banner.createError(response);
  86. }else{
  87. updateRecipes(updatedRecipe);
  88. banner.createNotification("Recipe successfully updated");
  89. }
  90. })
  91. .catch((err)=>{
  92. banner.createError("Something went wrong. Please refresh the page");
  93. })
  94. }
  95. },
  96. remove: function(){
  97. fetch(`/merchant/recipes/remove/${this.recipe.id}`, {
  98. method: "DELETE"
  99. })
  100. .then((response) => response.json())
  101. .then((response)=>{
  102. if(typeof(response) === "string"){
  103. banner.createError(response);
  104. }else{
  105. merchant.editRecipe(this.recipe, true);
  106. banner.createNotification("Recipe removed");
  107. }
  108. })
  109. .catch((err)=>{
  110. banner.createError("Something went wrong. Try refreshing the page");
  111. });
  112. },
  113. displayAddIngredient: function(){
  114. let template = document.querySelector("#addRecIngredient").content.children[0].cloneNode(true);
  115. template.name = "new";
  116. document.querySelector("#recipeIngredientList").appendChild(template);
  117. let categories = merchant.categorizeIngredients();
  118. for(let i = 0; i < categories.length; i++){
  119. let optGroup = document.createElement("optgroup");
  120. optGroup.label = categories[i].name;
  121. template.children[0].appendChild(optGroup);
  122. for(let j = 0; j < categories[i].ingredients.length; j++){
  123. let option = document.createElement("option");
  124. option.innerText = `${categories[i].ingredients[j].ingredient.name} (${categories[i].ingredients[j].ingredient.unit})`;
  125. option.value = categories[i].ingredients[j].id;
  126. optGroup.appendChild(option);
  127. }
  128. }
  129. }
  130. }
  131. let newOrderComp = {
  132. isPopulated: false,
  133. display: function(){
  134. openSidebar(document.querySelector("#newOrder"));
  135. if(!this.isPopulated){
  136. let categories = categorizeIngredients(merchant.inventory);
  137. let categoriesList = document.querySelector("#newOrderCategories");
  138. let template = document.querySelector("#addIngredientsCategory").content.children[0];
  139. let ingredientTemplate = document.querySelector("#addIngredientsIngredient").content.children[0];
  140. for(let i = 0; i < categories.length; i++){
  141. let category = template.cloneNode(true);
  142. category.children[0].children[0].innerText = categories[i].name;
  143. category.children[0].children[1].onclick = ()=>{addIngredientsComp.toggleAddIngredient(category)};
  144. category.children[0].children[1].children[1].style.display = "none";
  145. category.children[1].style.display = "none";
  146. categoriesList.appendChild(category);
  147. for(let j = 0; j < categories[i].ingredients.length; j++){
  148. let ingredientDiv = ingredientTemplate.cloneNode(true);
  149. ingredientDiv.children[0].innerText = categories[i].ingredients[j].name;
  150. ingredientDiv.children[1].placeholder = categories[i].ingredients[j].unit;
  151. ingredientDiv._id = categories[i].ingredients[j].id;
  152. ingredientDiv._name = categories[i].ingredients[j].name;
  153. ingredientDiv._unit = categories[i].ingredients[j].unit;
  154. ingredientDiv._category = categories[i].name;
  155. let priceInput = document.createElement("input");
  156. priceInput.type = "number";
  157. priceInput.min = "0";
  158. priceInput.step = "0.01";
  159. priceInput.placeholder = "Price Per Unit";
  160. ingredientDiv.appendChild(priceInput);
  161. category.children[1].appendChild(ingredientDiv);
  162. }
  163. }
  164. this.isPopulated = true;
  165. }
  166. },
  167. submit: function(){
  168. let categoriesList = document.querySelector("#newOrderCategories");
  169. let newOrder = {
  170. orderId: document.querySelector("#orderName").value,
  171. date: new Date(document.querySelector("#orderDate").value),
  172. ingredients: []
  173. }
  174. for(let i = 0; i < categoriesList.children.length; i++){
  175. for(let j = 0; j < categoriesList.children[i].children[1].children.length; j++){
  176. let ingredientDiv = categoriesList.children[i].children[1].children[j];
  177. let quantity = ingredientDiv.children[1].value;
  178. let price = ingredientDiv.children[2].value;
  179. if(quantity !== "" || price !== ""){
  180. let newIngredient = {
  181. id: ingredientDiv._id,
  182. ingredient: ingredientDiv._id,
  183. quantity: parseFloat(quantity),
  184. price: parseInt(price * 100)
  185. }
  186. newOrder.ingredients.push(newIngredient);
  187. }
  188. }
  189. }
  190. if(validator.order(newOrder)){
  191. fetch("/order", {
  192. method: "POST",
  193. headers: {
  194. "Content-Type": "application/json;charset=utf-8"
  195. },
  196. body: JSON.stringify(newOrder)
  197. })
  198. .then(response => response.json())
  199. .then((response)=>{
  200. if(typeof(response) === "string"){
  201. banner.createError(response);
  202. }else{
  203. banner.createNotification("New order created");
  204. updateOrders(newOrder);
  205. updateInventory(newOrder.ingredients);
  206. }
  207. })
  208. .catch((err)=>{
  209. banner.createError("Something went wrong. Try refreshing the page");
  210. });
  211. }
  212. },
  213. }
  214. let newIngredientComp = {
  215. display: function(){
  216. openSidebar(document.querySelector("#newIngredient"));
  217. document.querySelector("#newIngName").value = "";
  218. document.querySelector("#newIngCategory").value = "";
  219. document.querySelector("#newIngQuantity").value = 0;
  220. document.querySelector("#newIngUnit").value = ""
  221. },
  222. submit: function(){
  223. let newIngredient = {
  224. ingredient: {
  225. name: document.querySelector("#newIngName").value,
  226. category: document.querySelector("#newIngCategory").value,
  227. unit: document.querySelector("#newIngUnit").value
  228. },
  229. quantity: document.querySelector("#newIngQuantity").value
  230. }
  231. if(validator.ingredient(newIngredient)){
  232. fetch("/ingredients/create", {
  233. method: "POST",
  234. headers: {
  235. "Content-Type": "application/json;charset=utf-8"
  236. },
  237. body: JSON.stringify(newIngredient)
  238. })
  239. .then((response) => response.json())
  240. .then((response)=>{
  241. if(typeof(response) === "string"){
  242. banner.createError(response);
  243. }else{
  244. merchant.editIngredients([{
  245. ingredient: new Ingredient(
  246. response.ingredient._id,
  247. response.ingredient.name,
  248. response.ingredient.category,
  249. response.ingredient.unit
  250. ),
  251. quantity: response.quantity
  252. }]);
  253. banner.createNotification("Ingredient successfully created");
  254. }
  255. })
  256. .catch((err)=>{
  257. banner.createError("Something went wrong. Try refreshing the page");
  258. });
  259. }
  260. }
  261. }
  262. let orderDetailsComp = {
  263. display: function(order){
  264. openSidebar(document.querySelector("#orderDetails"));
  265. document.querySelector("#removeOrderBtn").onclick = ()=>{this.remove(order._id)};
  266. document.querySelector("#orderDetails h1").innerText = order.orderId || order._id;
  267. document.querySelector("#orderDetails h3").innerText = new Date(order.date).toLocaleDateString("en-US");
  268. let ingredientList = document.querySelector("#orderIngredients");
  269. while(ingredientList.children.length > 0){
  270. ingredientList.removeChild(ingredientList.firstChild);
  271. }
  272. let template = document.querySelector("#orderIngredient").content.children[0];
  273. let grandTotal = 0;
  274. for(let i = 0; i < order.ingredients.length; i++){
  275. let ingredient = template.cloneNode(true);
  276. let price = (order.ingredients[i].quantity * order.ingredients[i].price) / 100;
  277. grandTotal += price;
  278. for(let j = 0; j < merchant.inventory.length; j++){
  279. if(order.ingredients[i].ingredient === merchant.inventory[j].ingredient._id){
  280. ingredient.children[0].innerText = `${merchant.inventory[j].ingredient.name}: ${order.ingredients[i].quantity}`;
  281. break;
  282. }
  283. }
  284. ingredient.children[1].innerText = `$${(order.ingredients[i].price / 100).toFixed(2)}`;
  285. ingredient.children[2].innerText = price.toFixed(2);
  286. ingredientList.appendChild(ingredient);
  287. }
  288. document.querySelector("#orderTotalPrice p").innerText = `$${grandTotal.toFixed(2)}`;
  289. },
  290. remove: function(id){
  291. fetch(`/order/${id}`, {
  292. method: "DELETE",
  293. headers: {
  294. "Content-Type": "application/json;charset=utf-8"
  295. }
  296. })
  297. .then((response) => response.json())
  298. .then((response)=>{
  299. if(typeof(response) === "string"){
  300. banner.createError(response);
  301. }else{
  302. updateOrders({_id: id}, true);
  303. banner.createNotification("Order successfully removed");
  304. }
  305. })
  306. .catch((err)=>{
  307. banner.createError("Something went wrong, try refreshing the page");
  308. });
  309. }
  310. }
  311. let addIngredientsComp = {
  312. isPopulated: false,
  313. fakeMerchant: {},
  314. chosenIngredients: [],
  315. display: function(){
  316. let sidebar = document.querySelector("#addIngredients");
  317. if(!this.isPopulated){
  318. fetch("/ingredients")
  319. .then((response) => response.json())
  320. .then((response)=>{
  321. if(typeof(response) === "string"){
  322. banner.createError(response);
  323. }else{
  324. for(let i = 0; i < merchant.ingredients.length; i++){
  325. for(let j = 0; j < response.length; j++){
  326. if(merchant.ingredients[i].ingredient.id === response[j]._id){
  327. response.splice(j, 1);
  328. break;
  329. }
  330. }
  331. }
  332. for(let i = 0; i < response.length; i++){
  333. response[i] = {ingredient: response[i]}
  334. }
  335. this.fakeMerchant = new Merchant(
  336. {
  337. name: "none",
  338. inventory: response,
  339. recipes: [],
  340. },
  341. []
  342. );
  343. this.populateAddIngredients();
  344. }
  345. })
  346. .catch((err)=>{
  347. banner.createError("Unable to retrieve data");
  348. });
  349. this.isPopulated = true;
  350. }
  351. openSidebar(sidebar);
  352. },
  353. populateAddIngredients: function(){
  354. let addIngredientsDiv = document.getElementById("addIngredientList");
  355. let categoryTemplate = document.getElementById("addIngredientsCategory");
  356. let ingredientTemplate = document.getElementById("addIngredientsIngredient");
  357. let categories = this.fakeMerchant.categorizeIngredients();
  358. while(addIngredientsDiv.children.length > 0){
  359. addIngredientsDiv.removeChild(addIngredientsDiv.firstChild);
  360. }
  361. for(let i = 0; i < categories.length; i++){
  362. let categoryDiv = categoryTemplate.content.children[0].cloneNode(true);
  363. categoryDiv.children[0].children[0].innerText = categories[i].name;
  364. categoryDiv.children[0].children[1].onclick = ()=>{addIngredientsComp.toggleAddIngredient(categoryDiv)};
  365. categoryDiv.children[1].style.display = "none";
  366. categoryDiv.children[0].children[1].children[1].style.display = "none";
  367. addIngredientsDiv.appendChild(categoryDiv);
  368. for(let j = 0; j < categories[i].ingredients.length; j++){
  369. let ingredientDiv = ingredientTemplate.content.children[0].cloneNode(true);
  370. ingredientDiv.children[0].innerText = categories[i].ingredients[j].ingredient.name;
  371. ingredientDiv.ingredient = categories[i].ingredients[j].ingredient;
  372. categoryDiv.children[1].appendChild(ingredientDiv);
  373. }
  374. }
  375. },
  376. toggleAddIngredient: function(categoryElement){
  377. let button = categoryElement.children[0].children[1];
  378. let ingredientDisplay = categoryElement.children[1];
  379. if(ingredientDisplay.style.display === "none"){
  380. ingredientDisplay.style.display = "flex";
  381. button.children[0].style.display = "none";
  382. button.children[1].style.display = "block";
  383. }else{
  384. ingredientDisplay.style.display = "none";
  385. button.children[0].style.display = "block";
  386. button.children[1].style.display = "none";
  387. }
  388. },
  389. addOne: function(element){
  390. element.parentElement.removeChild(element);
  391. document.getElementById("myIngredients").appendChild(element);
  392. document.getElementById("myIngredientsDiv").style.display = "flex";
  393. for(let i = 0; i < this.fakeMerchant.ingredients.length; i++){
  394. if(this.fakeMerchant.ingredients[i].ingredient === element.ingredient){
  395. this.fakeMerchant.ingredients.splice(i, 1);
  396. this.chosenIngredients.push(element.ingredient);
  397. break;
  398. }
  399. }
  400. let input = document.createElement("input");
  401. input.type = "number";
  402. input.min = "0";
  403. input.step = "0.01";
  404. input.placeholder = element._unit;
  405. element.insertBefore(input, element.children[1]);
  406. element.children[2].innerText = "-";
  407. element.children[2].onclick = ()=>{this.removeOne(element)};
  408. },
  409. removeOne: function(element){
  410. element.parentElement.removeChild(element);
  411. element.removeChild(element.children[1]);
  412. element.children[1].innerText = "+";
  413. element.children[1].onclick = ()=>{this.addOne(element)};
  414. if(document.getElementById("myIngredients").children.length === 0){
  415. document.getElementById("myIngredientsDiv").style.display = "none";
  416. }
  417. for(let i = 0; i < this.chosenIngredients.length; i++){
  418. if(this.chosenIngredients[i] === element.ingredient){
  419. this.chosenIngredients.splice(i, 1);
  420. this.fakeMerchant.ingredients.push({
  421. ingredient: element.ingredient
  422. });
  423. break;
  424. }
  425. }
  426. this.populateAddIngredients();
  427. },
  428. submit: function(){
  429. let ingredients = document.getElementById("myIngredients").children;
  430. let newIngredients = [];
  431. let fetchable = [];
  432. for(let i = 0; i < ingredients.length; i++){
  433. if(ingredients[i].children[1].value === ""){
  434. banner.createError("Please enter a quantity for each ingredient you want to add to your inventory");
  435. return;
  436. }
  437. newIngredients.push({
  438. ingredient: ingredients[i].ingredient,
  439. quantity: ingredients[i].children[1].value
  440. });
  441. fetchable.push({
  442. id: ingredients[i].ingredient.id,
  443. quantity: ingredients[i].children[1].value
  444. });
  445. }
  446. fetch("/merchant/ingredients/add", {
  447. method: "POST",
  448. headers: {
  449. "Content-Type": "application/json;charset=utf-8"
  450. },
  451. body: JSON.stringify(fetchable)
  452. })
  453. .then((response)=>{
  454. if(typeof(response) === "string"){
  455. banner.createError(response);
  456. }else{
  457. merchant.editIngredients(newIngredients);
  458. banner.createNotification("All ingredients added successfully");
  459. }
  460. })
  461. .catch((err)=>{
  462. banner.createError("Something went wrong. Try refreshing the page");
  463. });
  464. }
  465. }
  466. let ingredientDetailsComp = {
  467. ingredient: {},
  468. display: function(ingredient, category){
  469. this.ingredient = ingredient;
  470. sidebar = document.querySelector("#ingredientDetails");
  471. document.querySelector("#ingredientDetails p").innerText = category.name;
  472. document.querySelector("#ingredientDetails h1").innerText = ingredient.ingredient.name;
  473. document.querySelector("#ingredientStock").innerText = `${ingredient.quantity} ${ingredient.ingredient.unit}`;
  474. document.querySelector("#ingredientInput").placeholder = `${ingredient.quantity} ${ingredient.ingredient.unit}`;
  475. let quantities = [];
  476. let now = new Date();
  477. console.time("Single Ingredient Sold");
  478. for(let i = 1; i < 31; i++){
  479. let endDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i)
  480. let startDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i - 1);
  481. let indices = merchant.transactionIndices(startDay, endDay);
  482. if(indices === false){
  483. quantities.push(0);
  484. }else{
  485. quantities.push(merchant.singleIngredientSold(indices, ingredient));
  486. }
  487. }
  488. console.timeEnd("Single Ingredient Sold");
  489. let sum = 0;
  490. for(let quantity of quantities){
  491. sum += quantity;
  492. }
  493. document.querySelector("#dailyUse").innerText = `${(sum/quantities.length).toFixed(2)} ${ingredient.ingredient.unit}`;
  494. let ul = document.querySelector("#ingredientRecipeList");
  495. let recipes = merchant.getRecipesForIngredient(ingredient.ingredient);
  496. while(ul.children.length > 0){
  497. ul.removeChild(ul.firstChild);
  498. }
  499. for(let i = 0; i < recipes.length; i++){
  500. let li = document.createElement("li");
  501. li.innerText = recipes[i].name;
  502. li.onclick = ()=>{
  503. changeStrand("recipeBookStrand");
  504. recipeDetailsComp.display(recipes[i]);
  505. }
  506. ul.appendChild(li);
  507. }
  508. openSidebar(sidebar);
  509. },
  510. remove: function(){
  511. for(let i = 0; i < merchant.recipes.length; i++){
  512. for(let j = 0; j < merchant.recipes[i].ingredients.length; j++){
  513. if(this.ingredient.id === merchant.recipes[i].ingredients[j].ingredient._id){
  514. banner.createError("Must remove ingredient from all recipes before removing");
  515. return;
  516. }
  517. }
  518. }
  519. fetch(`/merchant/ingredients/remove/${this.ingredient.ingredient.id}`, {
  520. method: "DELETE",
  521. })
  522. .then((response) => response.json())
  523. .then((response)=>{
  524. if(typeof(response) === "string"){
  525. banner.createError(response);
  526. }else{
  527. banner.createNotification("Ingredient removed");
  528. merchant.editIngredients([this.ingredient], true);
  529. }
  530. })
  531. .catch((err)=>{});
  532. },
  533. edit: function(){
  534. document.querySelector("#ingredientStock").style.display = "none";
  535. document.querySelector("#ingredientInput").style.display = "block";
  536. document.querySelector("#editSubmitButton").style.display = "block";
  537. },
  538. editSubmit: function(){
  539. this.ingredient.quantity = Number(document.getElementById("ingredientInput").value);
  540. let data = [{
  541. id: this.ingredient.ingredient.id,
  542. quantity: this.ingredient.quantity
  543. }];
  544. if(validator.ingredientQuantity(data[0].quantity)){
  545. fetch("/merchant/ingredients/update", {
  546. method: "PUT",
  547. headers: {
  548. "Content-Type": "application/json;charset=utf-8"
  549. },
  550. body: JSON.stringify(data)
  551. })
  552. .then((response) => response.json())
  553. .then((response)=>{
  554. if(typeof(response) === "string"){
  555. banner.createError(response);
  556. }else{
  557. merchant.editIngredients([this.ingredient]);
  558. banner.createNotification("Ingredient updated");
  559. }
  560. })
  561. .catch((err)=>{
  562. banner.createError("Something went wrong, try refreshing the page");
  563. });
  564. }
  565. }
  566. }
  567. let newRecipeComp = {
  568. display: function(){
  569. let ingredientsSelect = document.querySelector("#recipeInputIngredients select");
  570. let categories = merchant.categorizeIngredients();
  571. for(let category of categories){
  572. let optgroup = document.createElement("optgroup");
  573. optgroup.label = category.name;
  574. ingredientsSelect.appendChild(optgroup);
  575. for(let ingredient of category.ingredients){
  576. let option = document.createElement("option");
  577. option.value = ingredient.ingredient.id;
  578. option.innerText = ingredient.ingredient.name;
  579. optgroup.appendChild(option);
  580. }
  581. }
  582. openSidebar(document.querySelector("#addRecipe"));
  583. },
  584. //Updates the number of ingredient inputs displayed for new recipes
  585. changeRecipeCount: function(){
  586. let newCount = document.querySelector("#ingredientCount").value;
  587. let ingredientsDiv = document.querySelector("#recipeInputIngredients");
  588. let oldCount = ingredientsDiv.children.length;
  589. if(newCount > oldCount){
  590. let newDivs = newCount - oldCount;
  591. for(let i = 0; i < newDivs; i++){
  592. let newNode = ingredientsDiv.children[0].cloneNode(true);
  593. newNode.children[2].children[0].value = "";
  594. ingredientsDiv.appendChild(newNode);
  595. }
  596. for(let i = 0; i < newCount; i++){
  597. ingredientsDiv.children[i].children[0].innerText = `Ingredient ${i + 1}`;
  598. }
  599. }else if(newCount < oldCount){
  600. let newDivs = oldCount - newCount;
  601. for(let i = 0; i < newDivs; i++){
  602. ingredientsDiv.removeChild(ingredientsDiv.children[ingredientsDiv.children.length-1]);
  603. }
  604. }
  605. },
  606. submit: function(){
  607. let newRecipe = {
  608. name: document.querySelector("#newRecipeName").value,
  609. price: document.querySelector("#newRecipePrice").value,
  610. ingredients: []
  611. }
  612. let inputs = document.querySelectorAll("#recipeInputIngredients > div");
  613. for(let input of inputs){
  614. newRecipe.ingredients.push({
  615. ingredient: input.children[1].children[0].value,
  616. quantity: input.children[2].children[0].value
  617. });
  618. }
  619. if(!validator.recipe(newRecipe)){
  620. return;
  621. }
  622. fetch("/recipe/create", {
  623. method: "POST",
  624. headers: {
  625. "Content-Type": "application/json;charset=utf-8"
  626. },
  627. body: JSON.stringify(newRecipe)
  628. })
  629. .then((response) => response.json())
  630. .then((response)=>{
  631. if(typeof(response) === "string"){
  632. banner.createError(response);
  633. }else{
  634. let recipe = new Recipe(
  635. response._id,
  636. response.name,
  637. response.price,
  638. response.ingredients,
  639. merchant,
  640. );
  641. merchant.editRecipe(recipe);
  642. banner.createNotification("New recipe successfully created");
  643. }
  644. })
  645. .catch((err)=>{
  646. banner.createError("Refresh page to update data");
  647. });
  648. },
  649. }