createRecipes.js 5.9 KB

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