createRecipes.js 6.1 KB

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