recipeSetup.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. let recipeSetup = {
  2. recipeData: [],
  3. recipeDataIndex: 0,
  4. createRecipePage: function(){
  5. addIngredients.style.display = "none";
  6. newIngredients.style.display = "none";
  7. createRecipes.style.display = "flex";
  8. for(let recipe of recipes.elements){
  9. this.recipeData.push(
  10. {
  11. id: recipe.id,
  12. name: recipe.name,
  13. ingredients: []
  14. }
  15. )
  16. }
  17. this.showRecipe();
  18. },
  19. showRecipe: function(){
  20. let title = document.querySelector("#recipeName");
  21. title.innerText = this.recipeData[this.recipeDataIndex].name;
  22. let body = document.querySelector("#recipes tbody");
  23. for(let ing of this.recipeData[this.recipeDataIndex].ingredients){
  24. let row = document.createElement("tr");
  25. body.appendChild(row);
  26. let ingTd = document.createElement("td");
  27. row.appendChild(ingTd);
  28. let ingName = document.createElement("select");
  29. for(let ingredient of data.ingredients){
  30. let newOption = document.createElement("option");
  31. newOption.innerText = ingredient.name;
  32. newOption.value = ingredient.id;
  33. if(ingredient.id === ing.id){
  34. newOption.selected = "selected";
  35. }
  36. ingName.appendChild(newOption);
  37. }
  38. ingTd.appendChild(ingName);
  39. let quantTd = document.createElement("td");
  40. row.appendChild(quantTd);
  41. let ingQuant = document.createElement("input");
  42. ingQuant.type = "number";
  43. ingQuant.step = "0.01";
  44. ingQuant.value = ing.quantity;
  45. quantTd.appendChild(ingQuant);
  46. }
  47. let nextButton = document.querySelector("#next");
  48. if(this.recipeDataIndex === this.recipeData.length - 1){
  49. nextButton.innerText = "Finish";
  50. nextButton.onclick = ()=>{this.submitAll()};
  51. }else{
  52. nextButton.innerText = "Next Recipe";
  53. nextButton.onclick = ()=>{this.changeRecipe(1)};
  54. }
  55. let previousButton = document.querySelector("#previous");
  56. if(this.recipeDataIndex === 0){
  57. previousButton.style.display = "none";
  58. }else{
  59. previousButton.style.display = "inline-block";
  60. }
  61. },
  62. changeRecipe: function(num){
  63. let body = document.querySelector("#recipes tbody");
  64. let recipeIngredients = [];
  65. while(body.children.length > 0){
  66. let row = body.firstChild;
  67. recipeIngredients.push({
  68. id: row.children[0].children[0].value,
  69. quantity: row.children[1].children[0].value
  70. });
  71. this.recipeData[this.recipeDataIndex].ingredients = recipeIngredients;
  72. body.removeChild(row);
  73. }
  74. this.recipeDataIndex += num;
  75. this.showRecipe();
  76. },
  77. submitAll: function(){
  78. let body = document.querySelector("#recipes tbody");
  79. data.recipes = [];
  80. let recipeIngredients = [];
  81. while(body.children.length > 0){
  82. let row = body.firstChild;
  83. recipeIngredients.push({
  84. id: row.children[0].children[0].value,
  85. quantity: row.children[1].children[0].value
  86. });
  87. this.recipeData[this.recipeDataIndex].ingredients = recipeIngredients;
  88. body.removeChild(row);
  89. }
  90. for(let recipe of this.recipeData){
  91. let newRecipe = {
  92. cloverId: recipe.id,
  93. name: recipe.name,
  94. ingredients: []
  95. };
  96. for(let ingredient of recipe.ingredients){
  97. newRecipe.ingredients.push({
  98. id: ingredient.id,
  99. quantity: ingredient.quantity
  100. });
  101. }
  102. data.recipes.push(newRecipe);
  103. }
  104. let form = document.createElement("form");
  105. form.method = "post";
  106. form.action = "/merchant/create"
  107. let dataInput = document.createElement("input");
  108. dataInput.type = "hidden";
  109. dataInput.name = "data";
  110. dataInput.value = JSON.stringify(data);
  111. form.appendChild(dataInput);
  112. document.body.appendChild(form);
  113. form.submit();
  114. },
  115. addRecipeIngredientField: function(){
  116. let body = document.querySelector("#recipes tbody");
  117. let row = document.createElement("tr");
  118. body.appendChild(row);
  119. let ingTd = document.createElement("td");
  120. row.appendChild(ingTd);
  121. let ingName = document.createElement("select");
  122. for(let ingredient of data.ingredients){
  123. let newOption = document.createElement("option");
  124. newOption.innerText = ingredient.name;
  125. newOption.value = ingredient.id;
  126. ingName.appendChild(newOption);
  127. }
  128. ingTd.appendChild(ingName);
  129. let quantTd = document.createElement("td");
  130. row.appendChild(quantTd);
  131. let ingQuant = document.createElement("input");
  132. ingQuant.type = "number";
  133. ingQuant.step = "0.01";
  134. ingQuant.min = "0";
  135. quantTd.appendChild(ingQuant);
  136. let removeTd = document.createElement("td");
  137. row.appendChild(removeTd);
  138. let removeButton = document.createElement("button");
  139. removeButton.innerText = "-";
  140. removeButton.onclick = ()=>{row.parentNode.removeChild(row)};
  141. removeTd.appendChild(removeButton);
  142. }
  143. }