recipeSetup.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. let recipeSetup = {
  2. recipeData: [], //stores data from recipes, including ingredients
  3. recipeDataIndex: 0, //index for recipeData, which one is currently displaying
  4. //Display recipe page and hide others
  5. //Populate recipeData with data from Clover
  6. createRecipePage: function(){
  7. addIngredients.style.display = "none";
  8. newIngredients.style.display = "none";
  9. createRecipes.style.display = "flex";
  10. for(let recipe of recipes.elements){
  11. this.recipeData.push({
  12. id: recipe.id,
  13. name: recipe.name,
  14. ingredients: []
  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 = this.recipeData[this.recipeDataIndex].name;
  25. let body = document.querySelector("#recipes tbody");
  26. for(let ing of this.recipeData[this.recipeDataIndex].ingredients){
  27. let row = document.createElement("tr");
  28. body.appendChild(row);
  29. let ingTd = document.createElement("td");
  30. row.appendChild(ingTd);
  31. let ingName = document.createElement("select");
  32. for(let ingredient of controller.data.ingredients){
  33. let newOption = document.createElement("option");
  34. newOption.innerText = ingredient.name;
  35. newOption.value = ingredient.id;
  36. if(ingredient.id === ing.id){
  37. newOption.selected = "selected";
  38. }
  39. ingName.appendChild(newOption);
  40. }
  41. ingTd.appendChild(ingName);
  42. let quantTd = document.createElement("td");
  43. row.appendChild(quantTd);
  44. let ingQuant = document.createElement("input");
  45. ingQuant.type = "number";
  46. ingQuant.step = "0.01";
  47. ingQuant.value = ing.quantity;
  48. ingQuant.onblur = ()=>{checkValid("quantity", ingQuant)};
  49. quantTd.appendChild(ingQuant);
  50. }
  51. let nextButton = document.querySelector("#next");
  52. if(this.recipeDataIndex === this.recipeData.length - 1){
  53. nextButton.innerText = "Finish";
  54. nextButton.onclick = ()=>{this.submitAll()};
  55. }else{
  56. nextButton.innerText = "Next Recipe";
  57. nextButton.onclick = ()=>{this.changeRecipe(1)};
  58. }
  59. let previousButton = document.querySelector("#previous");
  60. if(this.recipeDataIndex === 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 body = document.querySelector("#recipes tbody");
  72. this.recipeData[this.recipeDataIndex].ingredients = [];
  73. let isValid = true;
  74. for(let row of body.children){
  75. newIngredient ={
  76. id: row.children[0].children[0].value,
  77. quantity: row.children[1].children[0].value
  78. };
  79. this.recipeData[this.recipeDataIndex].ingredients.push(newIngredient);
  80. if(!validator.ingredient.quantity(newIngredient.quantity)){
  81. isValid = false;
  82. break;
  83. }
  84. }
  85. if(isValid){
  86. while(body.children.length > 0){
  87. body.removeChild(body.firstChild);
  88. }
  89. this.recipeDataIndex += num;
  90. this.showRecipe();
  91. }
  92. },
  93. //Add all recipes to data variable
  94. //Creates a form and submits data
  95. submitAll: function(){
  96. this.recipeData[this.recipeDataIndex].ingredients = [];
  97. let body = document.querySelector("#recipes tbody");
  98. controller.data.recipes = [];
  99. let isValid = true;
  100. for(let row of body.children){
  101. newIngredient ={
  102. id: row.children[0].children[0].value,
  103. quantity: row.children[1].children[0].value
  104. };
  105. this.recipeData[this.recipeDataIndex].ingredients.push(newIngredient);
  106. if(!validator.ingredient.quantity(newIngredient.quantity)){
  107. isValid = false;
  108. break;
  109. }
  110. }
  111. if(isValid){
  112. for(let recipe of this.recipeData){
  113. let newRecipe = {
  114. posId: recipe.id,
  115. name: recipe.name,
  116. ingredients: []
  117. };
  118. for(let ingredient of recipe.ingredients){
  119. newRecipe.ingredients.push({
  120. ingredient: ingredient.id,
  121. quantity: ingredient.quantity
  122. });
  123. }
  124. controller.data.recipes.push(newRecipe);
  125. }
  126. let form = document.createElement("form");
  127. form.method = "post";
  128. form.action = "/merchant/create"
  129. let dataInput = document.createElement("input");
  130. dataInput.type = "hidden";
  131. dataInput.name = "data";
  132. dataInput.value = JSON.stringify(controller.data);
  133. form.appendChild(dataInput);
  134. document.body.appendChild(form);
  135. form.submit();
  136. }
  137. },
  138. //Creates a new, empty row in table to input data
  139. addRecipeIngredientField: function(){
  140. let body = document.querySelector("#recipes tbody");
  141. let row = document.createElement("tr");
  142. body.appendChild(row);
  143. let ingTd = document.createElement("td");
  144. row.appendChild(ingTd);
  145. let ingName = document.createElement("select");
  146. for(let ingredient of controller.data.ingredients){
  147. let newOption = document.createElement("option");
  148. newOption.innerText = ingredient.name;
  149. newOption.value = ingredient.id;
  150. ingName.appendChild(newOption);
  151. }
  152. ingTd.appendChild(ingName);
  153. let quantTd = document.createElement("td");
  154. row.appendChild(quantTd);
  155. let ingQuant = document.createElement("input");
  156. ingQuant.type = "number";
  157. ingQuant.step = "0.01";
  158. ingQuant.min = "0";
  159. ingQuant.onblur = ()=>{controller.checkValid("quantity", ingQuant)};
  160. quantTd.appendChild(ingQuant);
  161. let removeTd = document.createElement("td");
  162. row.appendChild(removeTd);
  163. let removeButton = document.createElement("button");
  164. removeButton.innerText = "-";
  165. removeButton.onclick = ()=>{row.parentNode.removeChild(row)};
  166. removeTd.appendChild(removeButton);
  167. }
  168. }