merchantSetup.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. let state = 0;
  2. let data = {};
  3. console.log(ingredients);
  4. console.log(recipes);
  5. let addIngredients = document.querySelector("#addIngredients");
  6. let newIngredients = document.querySelector("#newIngredients");
  7. let createRecipes = document.querySelector("#createRecipes");
  8. let existingIngredientElements = [];
  9. let newIngredientElements = [];
  10. let recipeData = [];
  11. let recipeDataIndex = 0;
  12. for(let recipe of recipes.elements){
  13. recipeData.push(
  14. {
  15. id: recipe.id,
  16. name: recipe.name,
  17. ingredients: []
  18. }
  19. )
  20. }
  21. let updateState = (num)=>{
  22. state += num;
  23. if(state === 0){
  24. addIngredients.style.display = "flex";
  25. newIngredients.style.display = "none";
  26. createRecipes.style.display = "none";
  27. }else if(state === 1){
  28. addIngredients.style.display = "none";
  29. newIngredients.style.display = "flex";
  30. createRecipes.style.display = "none";
  31. }else if(state === 2){
  32. addIngredients.style.display = "none";
  33. newIngredients.style.display = "none";
  34. createRecipes.style.display = "flex";
  35. createIngredientsList();
  36. }
  37. }
  38. //Ingredient functions
  39. let populateIngredients = ()=>{
  40. let tBody = document.createElement("tbody");
  41. for(let ingredient of ingredients){
  42. let row = document.createElement("tr");
  43. row.id = ingredient._id;
  44. let add = document.createElement("td");
  45. let checkbox = document.createElement("input");
  46. checkbox.type = "checkbox";
  47. add.appendChild(checkbox);
  48. row.appendChild(add);
  49. let name = document.createElement("td");
  50. name.innerText = ingredient.name;
  51. row.appendChild(name);
  52. let category = document.createElement("td");
  53. category.innerText = ingredient.category;
  54. row.appendChild(category);
  55. let quantity = document.createElement("td");
  56. let quantityInput = document.createElement("input");
  57. quantityInput.type = "number";
  58. quantityInput.step = "0.01";
  59. quantityInput.min = "0";
  60. quantity.appendChild(quantityInput);
  61. row.appendChild(quantity);
  62. let unit = document.createElement("td");
  63. unit.innerText = ingredient.unitType;
  64. row.appendChild(unit);
  65. let idField = document.createElement("input");
  66. idField.type = "hidden";
  67. idField.value = ingredient._id;
  68. tBody.appendChild(row);
  69. let oldTBody = document.querySelector("#ingredient-display tbody");
  70. oldTBody.parentNode.replaceChild(tBody, oldTBody);
  71. existingIngredientElements.push(row);
  72. }
  73. }
  74. let newIngredientField = ()=>{
  75. let inputField = document.querySelector("#inputField");
  76. let row = document.createElement("tr");
  77. let name = document.createElement("td");
  78. let nameInput = document.createElement("input");
  79. nameInput.type = "text";
  80. name.appendChild(nameInput);
  81. row.appendChild(name);
  82. let category = document.createElement("td");
  83. let categoryInput = document.createElement("input");
  84. categoryInput.type = "text"
  85. category.appendChild(categoryInput);
  86. row.appendChild(category);
  87. let quantity = document.createElement("td");
  88. let quantityInput = document.createElement("input");
  89. quantityInput.type = "number";
  90. quantityInput.step = "0.01";
  91. quantity.appendChild(quantityInput);
  92. row.appendChild(quantity);
  93. let unit = document.createElement("td");
  94. let unitInput = document.createElement("input");
  95. unitInput.type = "text";
  96. unit.appendChild(unitInput);
  97. row.appendChild(unit);
  98. inputField.appendChild(row);
  99. newIngredientElements.push(row);
  100. }
  101. let createIngredientsList = ()=>{
  102. data.ingredients = []; //changed
  103. for(let ingredient of existingIngredientElements){
  104. if(ingredient.childNodes[0].childNodes[0].checked){
  105. data.ingredients.push({
  106. id: ingredient.id,
  107. name: ingredient.childNodes[1].childNodes[0].nodeValue,
  108. quantity: ingredient.childNodes[3].childNodes[0].value,
  109. unitType: ingredient.childNodes[4].childNodes[0].nodeValue
  110. });
  111. }
  112. }
  113. let newIngredient = [];
  114. let newIngredientQuantity = [];
  115. for(let ingredient of newIngredientElements){
  116. newIngredient.push({
  117. name: ingredient.childNodes[0].childNodes[0].value,
  118. category: ingredient.childNodes[1].childNodes[0].value,
  119. unitType: ingredient.childNodes[3].childNodes[0].value
  120. });
  121. newIngredientQuantity.push({
  122. name: ingredient.childNodes[0].childNodes[0].value,
  123. quantity: ingredient.childNodes[2].childNodes[0].value
  124. })
  125. }
  126. axios.post("/ingredients/create", newIngredient)
  127. .then((result)=>{
  128. for(let ingredient of result.data){
  129. let newIngredient = {
  130. id: ingredient._id,
  131. name: ingredient.name,
  132. unitType: ingredient.unitType
  133. }
  134. for(let item of newIngredientQuantity){
  135. if(ingredient.name === item.name){
  136. newIngredient.quantity = item.quantity;
  137. }
  138. }
  139. data.ingredients.push(newIngredient);
  140. }
  141. showRecipe();
  142. })
  143. .catch((err)=>{
  144. console.log(err);
  145. });
  146. }
  147. //Recipe functions
  148. let showRecipe = ()=>{
  149. let title = document.querySelector("#recipeName");
  150. title.innerText = recipeData[recipeDataIndex].name;
  151. let body = document.querySelector("#recipes tbody");
  152. for(let ing of recipeData[recipeDataIndex].ingredients){
  153. let row = document.createElement("tr");
  154. body.appendChild(row);
  155. let ingTd = document.createElement("td");
  156. row.appendChild(ingTd);
  157. let ingName = document.createElement("select");
  158. console.log(data.ingredients);
  159. for(let ingredient of data.ingredients){
  160. let newOption = document.createElement("option");
  161. newOption.innerText = ingredient.name;
  162. newOption.value = ingredient.id;
  163. ingName.appendChild(newOption);
  164. }
  165. ingTd.appendChild(ingName);
  166. let quantTd = document.createElement("td");
  167. row.appendChild(quantTd);
  168. let ingQuant = document.createElement("input");
  169. ingQuant.type = "number";
  170. ingQuant.step = "0.01";
  171. ingQuant.value = ing.quantity;
  172. quantTd.appendChild(ingQuant);
  173. }
  174. let nextButton = document.querySelector("#next");
  175. if(recipeDataIndex === recipeData.length - 1){
  176. nextButton.innerText = "Finish";
  177. nextButton.onclick = submitAll;
  178. }else{
  179. nextButton.innerText = "Next Recipe";
  180. nextButton.onclick = ()=>{changeRecipe(1)};
  181. }
  182. let previousButton = document.querySelector("#previous");
  183. if(recipeDataIndex === 0){
  184. previousButton.style.display = "none";
  185. }else{
  186. previousButton.style.display = "inline-block";
  187. }
  188. }
  189. let addRecipeIngredientField = ()=>{
  190. let body = document.querySelector("#recipes tbody");
  191. let row = document.createElement("tr");
  192. body.appendChild(row);
  193. let ingTd = document.createElement("td");
  194. row.appendChild(ingTd);
  195. let ingName = document.createElement("select");
  196. for(let ingredient of data.ingredients){
  197. let newOption = document.createElement("option");
  198. newOption.innerText = ingredient.name;
  199. newOption.value = ingredient.id;
  200. ingName.appendChild(newOption);
  201. }
  202. ingTd.appendChild(ingName);
  203. let quantTd = document.createElement("td");
  204. row.appendChild(quantTd);
  205. let ingQuant = document.createElement("input");
  206. ingQuant.type = "number";
  207. ingQuant.step = "0.01";
  208. ingQuant.min = "0";
  209. quantTd.appendChild(ingQuant);
  210. }
  211. let changeRecipe = (num)=>{
  212. let body = document.querySelector("#recipes tbody");
  213. let recipeIngredients = [];
  214. while(body.hasChildNodes()){
  215. let row = body.firstChild;
  216. recipeIngredients.push({
  217. id: row.childNodes[0].childNodes[0].value,
  218. quantity: row.childNodes[1].childNodes[0].value
  219. });
  220. recipeData[recipeDataIndex].ingredients = recipeIngredients;
  221. body.removeChild(row);
  222. }
  223. recipeDataIndex += num;
  224. showRecipe();
  225. }
  226. let submitAll = ()=>{
  227. data.recipes = [];
  228. for(let recipe of recipeData){
  229. let newRecipe = {
  230. cloverId: recipe.id,
  231. name: recipe.name,
  232. ingredients: []
  233. };
  234. for(let ingredient of recipe.ingredients){
  235. newRecipe.ingredients.push({
  236. id: ingredient.id,
  237. quantity: ingredient.quantity
  238. });
  239. }
  240. data.recipes.push(newRecipe);
  241. }
  242. let form = document.createElement("form");
  243. form.method = "post";
  244. form.action = "/merchant/create"
  245. let dataInput = document.createElement("input");
  246. dataInput.type = "hidden";
  247. dataInput.name = "data";
  248. dataInput.value = JSON.stringify(data);
  249. form.appendChild(dataInput);
  250. document.body.appendChild(form);
  251. form.submit();
  252. }
  253. populateIngredients();
  254. updateState(0);