recipeSetup.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. if(recipes){
  8. for(let recipe of recipes.elements){
  9. this.recipeData.push({
  10. id: recipe.id,
  11. name: recipe.name,
  12. ingredients: []
  13. });
  14. }
  15. this.showRecipe();
  16. }else{
  17. this.createOwnRecipes();
  18. }
  19. },
  20. //Display page to create recipe names
  21. createOwnRecipes: function(){
  22. controller.clearScreen();
  23. controller.newRecipesComp.style.display = "flex";
  24. this.addNameField();
  25. },
  26. //Add another input box to input another recipe name
  27. addNameField: function(){
  28. let nameList = document.querySelector("#nameList");
  29. let nameDiv = document.createElement("div");
  30. nameList.appendChild(nameDiv);
  31. let nameInput = document.createElement("input");
  32. nameInput.type = "text";
  33. nameDiv.appendChild(nameInput);
  34. let nameRemove = document.createElement("button");
  35. nameRemove.innerText = "Remove";
  36. nameRemove.onclick = ()=>{nameDiv.parentNode.removeChild(nameDiv)};
  37. nameDiv.appendChild(nameRemove);
  38. },
  39. //Adds the names of the recipes
  40. //Calls showRecipe to carry on
  41. createNames: function(){
  42. for(let nameDiv of document.querySelector("#nameList").children){
  43. this.recipeData.push({
  44. name: nameDiv.children[0].value,
  45. ingredients: []
  46. });
  47. }
  48. this.showRecipe();
  49. },
  50. //Loops through recipeData to create td's
  51. //Displays each ingredient for current recipe
  52. //Create and display correct buttons for navigation
  53. showRecipe: function(){
  54. controller.clearScreen();
  55. controller.createRecipesComp.style.display = "flex";
  56. let title = document.querySelector("#recipeName");
  57. title.innerText = this.recipeData[this.recipeDataIndex].name;
  58. let body = document.querySelector("#recipeTable tbody");
  59. for(let ing of this.recipeData[this.recipeDataIndex].ingredients){
  60. let row = document.createElement("tr");
  61. body.appendChild(row);
  62. let ingTd = document.createElement("td");
  63. row.appendChild(ingTd);
  64. let ingName = document.createElement("select");
  65. for(let ingredient of controller.data.ingredients){
  66. let newOption = document.createElement("option");
  67. newOption.innerText = ingredient.name;
  68. newOption.value = ingredient.id;
  69. if(ingredient.id === ing.id){
  70. newOption.selected = "selected";
  71. }
  72. ingName.appendChild(newOption);
  73. }
  74. ingTd.appendChild(ingName);
  75. let quantTd = document.createElement("td");
  76. row.appendChild(quantTd);
  77. let ingQuant = document.createElement("input");
  78. ingQuant.type = "number";
  79. ingQuant.step = "0.01";
  80. ingQuant.value = ing.quantity;
  81. ingQuant.onblur = ()=>{checkValid("quantity", ingQuant)};
  82. quantTd.appendChild(ingQuant);
  83. }
  84. let nextButton = document.querySelector("#next");
  85. if(this.recipeDataIndex === this.recipeData.length - 1){
  86. nextButton.innerText = "Finish";
  87. nextButton.onclick = ()=>{this.submitAll()};
  88. }else{
  89. nextButton.innerText = "Next Recipe";
  90. nextButton.onclick = ()=>{this.changeRecipe(1)};
  91. }
  92. let previousButton = document.querySelector("#previous");
  93. if(this.recipeDataIndex === 0){
  94. previousButton.style.display = "none";
  95. }else{
  96. previousButton.style.display = "inline-block";
  97. }
  98. },
  99. //Adds ingredient data to recipeData
  100. //Empties all data in the table
  101. //Changes recipeDataIndex
  102. //Hands off to showRecipe function
  103. changeRecipe: function(num){
  104. let body = document.querySelector("#recipeTable tbody");
  105. this.recipeData[this.recipeDataIndex].ingredients = [];
  106. let isValid = true;
  107. for(let row of body.children){
  108. newIngredient ={
  109. id: row.children[0].children[0].value,
  110. quantity: row.children[1].children[0].value
  111. };
  112. this.recipeData[this.recipeDataIndex].ingredients.push(newIngredient);
  113. if(!validator.ingredient.quantity(newIngredient.quantity)){
  114. isValid = false;
  115. break;
  116. }
  117. }
  118. if(isValid){
  119. while(body.children.length > 0){
  120. body.removeChild(body.firstChild);
  121. }
  122. this.recipeDataIndex += num;
  123. this.showRecipe();
  124. }
  125. },
  126. //Add all recipes to data variable
  127. //Creates a form and submits data
  128. submitAll: function(){
  129. this.recipeData[this.recipeDataIndex].ingredients = [];
  130. let body = document.querySelector("#recipeTable tbody");
  131. controller.data.recipes = [];
  132. let isValid = true;
  133. for(let row of body.children){
  134. newIngredient ={
  135. id: row.children[0].children[0].value,
  136. quantity: row.children[1].children[0].value
  137. };
  138. this.recipeData[this.recipeDataIndex].ingredients.push(newIngredient);
  139. if(!validator.ingredient.quantity(newIngredient.quantity)){
  140. isValid = false;
  141. break;
  142. }
  143. }
  144. if(isValid){
  145. for(let recipe of this.recipeData){
  146. let newRecipe = {
  147. posId: recipe.id,
  148. name: recipe.name,
  149. ingredients: []
  150. };
  151. for(let ingredient of recipe.ingredients){
  152. newRecipe.ingredients.push({
  153. ingredient: ingredient.id,
  154. quantity: ingredient.quantity
  155. });
  156. }
  157. controller.data.recipes.push(newRecipe);
  158. }
  159. let form = document.createElement("form");
  160. form.method = "post";
  161. form.action = "/merchant/create"
  162. let dataInput = document.createElement("input");
  163. dataInput.type = "hidden";
  164. dataInput.name = "data";
  165. dataInput.value = JSON.stringify(controller.data);
  166. form.appendChild(dataInput);
  167. document.body.appendChild(form);
  168. form.submit();
  169. }
  170. },
  171. //Creates a new, empty row in table to input data
  172. addRecipeIngredientField: function(){
  173. let body = document.querySelector("#recipeTable tbody");
  174. let row = document.createElement("tr");
  175. body.appendChild(row);
  176. let ingTd = document.createElement("td");
  177. row.appendChild(ingTd);
  178. let ingName = document.createElement("select");
  179. for(let ingredient of controller.data.ingredients){
  180. let newOption = document.createElement("option");
  181. newOption.innerText = ingredient.name;
  182. newOption.value = ingredient.id;
  183. ingName.appendChild(newOption);
  184. }
  185. ingTd.appendChild(ingName);
  186. let quantTd = document.createElement("td");
  187. row.appendChild(quantTd);
  188. let ingQuant = document.createElement("input");
  189. ingQuant.type = "number";
  190. ingQuant.step = "0.01";
  191. ingQuant.min = "0";
  192. ingQuant.onblur = ()=>{controller.checkValid("quantity", ingQuant)};
  193. quantTd.appendChild(ingQuant);
  194. let removeTd = document.createElement("td");
  195. row.appendChild(removeTd);
  196. let removeButton = document.createElement("button");
  197. removeButton.innerText = "-";
  198. removeButton.onclick = ()=>{row.parentNode.removeChild(row)};
  199. removeTd.appendChild(removeButton);
  200. }
  201. }