createRecipes.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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("#createRecipesStrand tbody");
  29. if(controller.data.recipes[this.recipeIndex].ingredients.length <= 0){
  30. document.querySelector("#createRecipesStrand table").style.display = "none";
  31. }else{
  32. document.querySelector("#createRecipesStrand table").style.display = "table";
  33. }
  34. for(let recipeIngredient of controller.data.recipes[this.recipeIndex].ingredients){
  35. let row = document.createElement("tr");
  36. tbody.appendChild(row);
  37. let ingredientTd = document.createElement("td");
  38. row.appendChild(ingredientTd);
  39. let ingredientName = document.createElement("select");
  40. for(let inventoryIngredient of controller.data.inventory){
  41. let newOption = document.createElement("option");
  42. newOption.innerText = inventoryIngredient.ingredient.name;
  43. newOption.value = inventoryIngredient.ingredient.id;
  44. if(inventoryIngredient.ingredient.id === recipeIngredient.ingredient){
  45. newOption.selected = "selected";
  46. }
  47. ingredientName.appendChild(newOption);
  48. }
  49. ingredientTd.appendChild(ingredientName);
  50. let quantityTd = document.createElement("td");
  51. row.appendChild(quantityTd);
  52. let ingQuant = document.createElement("input");
  53. ingQuant.type = "number";
  54. ingQuant.step = "0.01";
  55. ingQuant.value = recipeIngredient.quantity;
  56. ingQuant.onblur = ()=>{controller.checkValid("quantity", ingQuant)};
  57. quantityTd.appendChild(ingQuant);
  58. let actionTd = document.createElement("td");
  59. row.appendChild(actionTd);
  60. let removeButton = document.createElement("button");
  61. removeButton.innerText = "Remove";
  62. removeButton.classList = "button-small";
  63. removeButton.onclick = ()=>{this.removeRow(row)};
  64. actionTd.appendChild(removeButton);
  65. }
  66. let nextButton = document.querySelector("#next");
  67. nextButton.onclick = ()=>{this.changeRecipe(1)};
  68. if(this.recipeIndex === controller.data.recipes.length - 1){
  69. nextButton.innerText = "Finish";
  70. }else{
  71. nextButton.innerText = "Next Recipe";
  72. }
  73. let previousButton = document.querySelector("#previous");
  74. if(this.recipeIndex === 0){
  75. previousButton.style.display = "none";
  76. }else{
  77. previousButton.style.display = "inline-block";
  78. }
  79. },
  80. //Adds ingredient data to recipeData
  81. //Empties all data in the table
  82. //Changes recipeDataIndex
  83. //Hands off to showRecipe function
  84. changeRecipe: function(num){
  85. let tbody = document.querySelector("#createRecipesStrand tbody");
  86. controller.data.recipes[this.recipeIndex].ingredients = [];
  87. if(!recipes){
  88. controller.data.recipes[this.recipeIndex].price = document.querySelector("#price").value;
  89. }
  90. let isValid = true;
  91. for(let row of tbody.children){
  92. let quantity = row.children[1].children[0].value;
  93. if(validator.ingredient.quantity(quantity)){
  94. controller.data.recipes[this.recipeIndex].ingredients.push({
  95. ingredient: row.children[0].children[0].value,
  96. quantity: quantity
  97. });
  98. }else{
  99. isValid = false;
  100. break;
  101. }
  102. }
  103. this.recipeIndex += num
  104. if(isValid){
  105. if(this.recipeIndex > controller.data.recipes.length - 1){
  106. this.submit();
  107. }else{
  108. while(tbody.children.length > 0){
  109. tbody.removeChild(tbody.firstChild);
  110. }
  111. this.showRecipe();
  112. }
  113. }
  114. },
  115. //Creates a new, empty row in table to input data
  116. addRecipeIngredientField: function(){
  117. let tbody = document.querySelector("#createRecipesStrand tbody");
  118. document.querySelector("#createRecipesStrand table").style.display = "table";
  119. let row = document.createElement("tr");
  120. tbody.appendChild(row);
  121. let ingTd = document.createElement("td");
  122. row.appendChild(ingTd);
  123. let ingName = document.createElement("select");
  124. for(let ingredient of controller.data.inventory){
  125. let newOption = document.createElement("option");
  126. newOption.innerText = ingredient.ingredient.name;
  127. newOption.value = ingredient.ingredient.id;
  128. ingName.appendChild(newOption);
  129. }
  130. ingTd.appendChild(ingName);
  131. let quantTd = document.createElement("td");
  132. row.appendChild(quantTd);
  133. let ingQuant = document.createElement("input");
  134. ingQuant.type = "number";
  135. ingQuant.step = "0.01";
  136. ingQuant.min = "0";
  137. ingQuant.onblur = ()=>{controller.checkValid("quantity", ingQuant)};
  138. quantTd.appendChild(ingQuant);
  139. let removeTd = document.createElement("td");
  140. row.appendChild(removeTd);
  141. let removeButton = document.createElement("button");
  142. removeButton.innerText = "Remove";
  143. removeButton.classList = "button-small";
  144. removeButton.onclick = ()=>{this.removeRow(row)};
  145. removeTd.appendChild(removeButton);
  146. },
  147. removeRow: function(row){
  148. let tbody = document.querySelector("#createRecipesStrand tbody");
  149. row.parentNode.removeChild(row);
  150. if(tbody.children.length <= 0){
  151. document.querySelector("#createRecipesStrand table").style.display = "none";
  152. }
  153. },
  154. //Add all recipes to data variable
  155. //Creates a form and submits data
  156. submit: function(){
  157. let form = document.createElement("form");
  158. form.method = "post";
  159. form.action = recipes ? "/merchant/clover/create" : "/merchant/none/create";
  160. form.style.display = "none";
  161. let dataInput = document.createElement("input");
  162. dataInput.type = "hidden";
  163. dataInput.name = "data";
  164. dataInput.value = JSON.stringify(controller.data);
  165. form.appendChild(dataInput);
  166. document.body.appendChild(form);
  167. form.submit();
  168. }
  169. }