createRecipes.js 5.6 KB

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