recipeSetup.js 6.3 KB

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