recipeSetup.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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)};
  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. //Adds ingredient data to recipeData
  69. //Empties all data in the table
  70. //Changes recipeDataIndex
  71. //Hands off to showRecipe function
  72. changeRecipe: function(num){
  73. let body = document.querySelector("#recipes tbody");
  74. this.recipeData[this.recipeDataIndex].ingredients = [];
  75. let isValid = true;
  76. for(let row of body.children){
  77. newIngredient ={
  78. id: row.children[0].children[0].value,
  79. quantity: row.children[1].children[0].value
  80. };
  81. this.recipeData[this.recipeDataIndex].ingredients.push(newIngredient);
  82. if(!validator.ingredient.quantity(newIngredient.quantity)){
  83. isValid = false;
  84. break;
  85. }
  86. }
  87. if(isValid){
  88. while(body.children.length > 0){
  89. body.removeChild(body.firstChild);
  90. }
  91. this.recipeDataIndex += num;
  92. this.showRecipe();
  93. }
  94. },
  95. //Add all recipes to data variable
  96. //Creates a form and submits data
  97. submitAll: function(){
  98. this.recipeData[this.recipeDataIndex].ingredients = [];
  99. let body = document.querySelector("#recipes tbody");
  100. data.recipes = [];
  101. let isValid = true;
  102. for(let row of body.children){
  103. newIngredient ={
  104. id: row.children[0].children[0].value,
  105. quantity: row.children[1].children[0].value
  106. };
  107. this.recipeData[this.recipeDataIndex].ingredients.push(newIngredient);
  108. if(!validator.ingredient.quantity(newIngredient.quantity)){
  109. isValid = false;
  110. break;
  111. }
  112. }
  113. if(isValid){
  114. for(let recipe of this.recipeData){
  115. let newRecipe = {
  116. cloverId: recipe.id,
  117. name: recipe.name,
  118. ingredients: []
  119. };
  120. for(let ingredient of recipe.ingredients){
  121. newRecipe.ingredients.push({
  122. id: ingredient.id,
  123. quantity: ingredient.quantity
  124. });
  125. }
  126. data.recipes.push(newRecipe);
  127. }
  128. let form = document.createElement("form");
  129. form.method = "post";
  130. form.action = "/merchant/create"
  131. let dataInput = document.createElement("input");
  132. dataInput.type = "hidden";
  133. dataInput.name = "data";
  134. dataInput.value = JSON.stringify(data);
  135. form.appendChild(dataInput);
  136. document.body.appendChild(form);
  137. form.submit();
  138. }
  139. },
  140. //Creates a new, empty row in table to input data
  141. addRecipeIngredientField: function(){
  142. let body = document.querySelector("#recipes tbody");
  143. let row = document.createElement("tr");
  144. body.appendChild(row);
  145. let ingTd = document.createElement("td");
  146. row.appendChild(ingTd);
  147. let ingName = document.createElement("select");
  148. for(let ingredient of data.ingredients){
  149. let newOption = document.createElement("option");
  150. newOption.innerText = ingredient.name;
  151. newOption.value = ingredient.id;
  152. ingName.appendChild(newOption);
  153. }
  154. ingTd.appendChild(ingName);
  155. let quantTd = document.createElement("td");
  156. row.appendChild(quantTd);
  157. let ingQuant = document.createElement("input");
  158. ingQuant.type = "number";
  159. ingQuant.step = "0.01";
  160. ingQuant.min = "0";
  161. ingQuant.onblur = ()=>{checkValid("quantity", ingQuant)};
  162. quantTd.appendChild(ingQuant);
  163. let removeTd = document.createElement("td");
  164. row.appendChild(removeTd);
  165. let removeButton = document.createElement("button");
  166. removeButton.innerText = "-";
  167. removeButton.onclick = ()=>{row.parentNode.removeChild(row)};
  168. removeTd.appendChild(removeButton);
  169. }
  170. }