createRecipes.js 5.9 KB

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