merchantSetup.js 10 KB

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