recipeSetup.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. quantTd.appendChild(ingQuant);
  51. }
  52. let nextButton = document.querySelector("#next");
  53. if(this.recipeDataIndex === this.recipeData.length - 1){
  54. nextButton.innerText = "Finish";
  55. nextButton.onclick = ()=>{this.submitAll()};
  56. }else{
  57. nextButton.innerText = "Next Recipe";
  58. nextButton.onclick = ()=>{this.changeRecipe(1)};
  59. }
  60. let previousButton = document.querySelector("#previous");
  61. if(this.recipeDataIndex === 0){
  62. previousButton.style.display = "none";
  63. }else{
  64. previousButton.style.display = "inline-block";
  65. }
  66. },
  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. let recipeIngredients = [];
  73. while(body.children.length > 0){
  74. let row = body.firstChild;
  75. recipeIngredients.push({
  76. id: row.children[0].children[0].value,
  77. quantity: row.children[1].children[0].value
  78. });
  79. this.recipeData[this.recipeDataIndex].ingredients = recipeIngredients;
  80. body.removeChild(row);
  81. }
  82. this.recipeDataIndex += num;
  83. this.showRecipe();
  84. },
  85. //Add all recipes to data variable
  86. //Creates a form and submits data
  87. submitAll: function(){
  88. let body = document.querySelector("#recipes tbody");
  89. data.recipes = [];
  90. let recipeIngredients = [];
  91. while(body.children.length > 0){
  92. let row = body.firstChild;
  93. recipeIngredients.push({
  94. id: row.children[0].children[0].value,
  95. quantity: row.children[1].children[0].value
  96. });
  97. this.recipeData[this.recipeDataIndex].ingredients = recipeIngredients;
  98. body.removeChild(row);
  99. }
  100. for(let recipe of this.recipeData){
  101. let newRecipe = {
  102. cloverId: recipe.id,
  103. name: recipe.name,
  104. ingredients: []
  105. };
  106. for(let ingredient of recipe.ingredients){
  107. newRecipe.ingredients.push({
  108. id: ingredient.id,
  109. quantity: ingredient.quantity
  110. });
  111. }
  112. data.recipes.push(newRecipe);
  113. }
  114. let form = document.createElement("form");
  115. form.method = "post";
  116. form.action = "/merchant/create"
  117. let dataInput = document.createElement("input");
  118. dataInput.type = "hidden";
  119. dataInput.name = "data";
  120. dataInput.value = JSON.stringify(data);
  121. form.appendChild(dataInput);
  122. document.body.appendChild(form);
  123. form.submit();
  124. },
  125. //Creates a new, empty row in table to input data
  126. addRecipeIngredientField: function(){
  127. let body = document.querySelector("#recipes tbody");
  128. let row = document.createElement("tr");
  129. body.appendChild(row);
  130. let ingTd = document.createElement("td");
  131. row.appendChild(ingTd);
  132. let ingName = document.createElement("select");
  133. for(let ingredient of data.ingredients){
  134. let newOption = document.createElement("option");
  135. newOption.innerText = ingredient.name;
  136. newOption.value = ingredient.id;
  137. ingName.appendChild(newOption);
  138. }
  139. ingTd.appendChild(ingName);
  140. let quantTd = document.createElement("td");
  141. row.appendChild(quantTd);
  142. let ingQuant = document.createElement("input");
  143. ingQuant.type = "number";
  144. ingQuant.step = "0.01";
  145. ingQuant.min = "0";
  146. quantTd.appendChild(ingQuant);
  147. let removeTd = document.createElement("td");
  148. row.appendChild(removeTd);
  149. let removeButton = document.createElement("button");
  150. removeButton.innerText = "-";
  151. removeButton.onclick = ()=>{row.parentNode.removeChild(row)};
  152. removeTd.appendChild(removeButton);
  153. }
  154. }