createRecipes.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. let createRecipesObj = {
  2. recipeIndex: 0,
  3. display: function(){
  4. controller.clearScreen();
  5. controller.createRecipesStrand.style.display = "flex";
  6. if(recipes){
  7. controller.data.recipes = [];
  8. for(let recipe of recipes.elements){
  9. controller.data.recipes.push({
  10. name: recipe.name,
  11. posId: recipe.id,
  12. price: recipe.price,
  13. ingredients: [],
  14. });
  15. }
  16. }
  17. this.showRecipe();
  18. },
  19. //Loops through recipeData to create td's
  20. //Displays each ingredient for current recipe
  21. //Create and display correct buttons for navigation
  22. showRecipe: function(){
  23. let title = document.querySelector("#recipeName");
  24. title.innerText = controller.data.recipes[this.recipeIndex].name;
  25. if(!recipes){
  26. document.querySelector("#price").value = controller.data.recipes[this.recipeIndex].price || 0;
  27. }
  28. let tbody = document.querySelector("#recipeTable tbody");
  29. for(let recipeIngredient of controller.data.recipes[this.recipeIndex].ingredients){
  30. let row = document.createElement("tr");
  31. tbody.appendChild(row);
  32. let ingredientTd = document.createElement("td");
  33. row.appendChild(ingredientTd);
  34. let ingredientName = document.createElement("select");
  35. for(let inventoryIngredient of controller.data.inventory){
  36. let newOption = document.createElement("option");
  37. newOption.innerText = inventoryIngredient.ingredient.name;
  38. newOption.value = inventoryIngredient.ingredient.id;
  39. if(inventoryIngredient.ingredient.id === recipeIngredient.ingredient){
  40. newOption.selected = "selected";
  41. }
  42. ingredientName.appendChild(newOption);
  43. }
  44. ingredientTd.appendChild(ingredientName);
  45. let quantityTd = document.createElement("td");
  46. row.appendChild(quantityTd);
  47. let ingQuant = document.createElement("input");
  48. ingQuant.type = "number";
  49. ingQuant.step = "0.01";
  50. ingQuant.value = recipeIngredient.quantity;
  51. ingQuant.onblur = ()=>{controller.checkValid("quantity", ingQuant)};
  52. quantityTd.appendChild(ingQuant);
  53. }
  54. let nextButton = document.querySelector("#next");
  55. nextButton.onclick = ()=>{this.changeRecipe(1)};
  56. if(this.recipeIndex === controller.data.recipes.length - 1){
  57. nextButton.innerText = "Finish";
  58. }else{
  59. nextButton.innerText = "Next Recipe";
  60. }
  61. let previousButton = document.querySelector("#previous");
  62. if(this.recipeIndex === 0){
  63. previousButton.style.display = "none";
  64. }else{
  65. previousButton.style.display = "inline-block";
  66. }
  67. },
  68. //Adds ingredient data to recipeData
  69. //Empties all data in the table
  70. //Changes recipeDataIndex
  71. //Hands off to showRecipe function
  72. changeRecipe: function(num){
  73. let tbody = document.querySelector("#recipeTable tbody");
  74. controller.data.recipes[this.recipeIndex].ingredients = [];
  75. if(!recipes){
  76. controller.data.recipes[this.recipeIndex].price = document.querySelector("#price").value;
  77. }
  78. let isValid = true;
  79. for(let row of tbody.children){
  80. let quantity = row.children[1].children[0].value;
  81. if(validator.ingredient.quantity(quantity)){
  82. controller.data.recipes[this.recipeIndex].ingredients.push({
  83. ingredient: row.children[0].children[0].value,
  84. quantity: quantity
  85. });
  86. }else{
  87. isValid = false;
  88. break;
  89. }
  90. }
  91. this.recipeIndex += num
  92. if(isValid){
  93. if(this.recipeIndex > controller.data.recipes.length - 1){
  94. this.submit();
  95. }else{
  96. while(tbody.children.length > 0){
  97. tbody.removeChild(tbody.firstChild);
  98. }
  99. this.showRecipe();
  100. }
  101. }
  102. },
  103. //Creates a new, empty row in table to input data
  104. addRecipeIngredientField: function(){
  105. let tbody = document.querySelector("#recipeTable tbody");
  106. let row = document.createElement("tr");
  107. tbody.appendChild(row);
  108. let ingTd = document.createElement("td");
  109. row.appendChild(ingTd);
  110. let ingName = document.createElement("select");
  111. for(let ingredient of controller.data.inventory){
  112. let newOption = document.createElement("option");
  113. newOption.innerText = ingredient.ingredient.name;
  114. newOption.value = ingredient.ingredient.id;
  115. ingName.appendChild(newOption);
  116. }
  117. ingTd.appendChild(ingName);
  118. let quantTd = document.createElement("td");
  119. row.appendChild(quantTd);
  120. let ingQuant = document.createElement("input");
  121. ingQuant.type = "number";
  122. ingQuant.step = "0.01";
  123. ingQuant.min = "0";
  124. ingQuant.onblur = ()=>{controller.checkValid("quantity", ingQuant)};
  125. quantTd.appendChild(ingQuant);
  126. let removeTd = document.createElement("td");
  127. row.appendChild(removeTd);
  128. let removeButton = document.createElement("button");
  129. removeButton.innerText = "-";
  130. removeButton.onclick = ()=>{row.parentNode.removeChild(row)};
  131. removeTd.appendChild(removeButton);
  132. },
  133. //Add all recipes to data variable
  134. //Creates a form and submits data
  135. submit: function(){
  136. let form = document.createElement("form");
  137. form.method = "post";
  138. form.action = recipes ? "/merchant/clover/create" : "/merchant/none/create";
  139. let dataInput = document.createElement("input");
  140. dataInput.type = "hidden";
  141. dataInput.name = "data";
  142. dataInput.value = JSON.stringify(controller.data);
  143. form.appendChild(dataInput);
  144. document.body.appendChild(form);
  145. form.submit();
  146. }
  147. }