merchantSetup.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. banner.createNotification("All ingredients have been created and added to your inventory");
  154. showRecipe();
  155. })
  156. .catch((err)=>{
  157. banner.createError("There has been an error and your ingredients have not been saved");
  158. console.log(err);
  159. });
  160. }
  161. //Recipe functions
  162. let showRecipe = ()=>{
  163. let title = document.querySelector("#recipeName");
  164. title.innerText = recipeData[recipeDataIndex].name;
  165. let body = document.querySelector("#recipes tbody");
  166. for(let ing of recipeData[recipeDataIndex].ingredients){
  167. let row = document.createElement("tr");
  168. body.appendChild(row);
  169. let ingTd = document.createElement("td");
  170. row.appendChild(ingTd);
  171. let ingName = document.createElement("select");
  172. for(let ingredient of data.ingredients){
  173. let newOption = document.createElement("option");
  174. newOption.innerText = ingredient.name;
  175. newOption.value = ingredient.id;
  176. if(ingredient.id === ing.id){
  177. newOption.selected = "selected";
  178. }
  179. ingName.appendChild(newOption);
  180. }
  181. ingTd.appendChild(ingName);
  182. let quantTd = document.createElement("td");
  183. row.appendChild(quantTd);
  184. let ingQuant = document.createElement("input");
  185. ingQuant.type = "number";
  186. ingQuant.step = "0.01";
  187. ingQuant.value = ing.quantity;
  188. quantTd.appendChild(ingQuant);
  189. }
  190. let nextButton = document.querySelector("#next");
  191. if(recipeDataIndex === recipeData.length - 1){
  192. nextButton.innerText = "Finish";
  193. nextButton.onclick = submitAll;
  194. }else{
  195. nextButton.innerText = "Next Recipe";
  196. nextButton.onclick = ()=>{changeRecipe(1)};
  197. }
  198. let previousButton = document.querySelector("#previous");
  199. if(recipeDataIndex === 0){
  200. previousButton.style.display = "none";
  201. }else{
  202. previousButton.style.display = "inline-block";
  203. }
  204. }
  205. let addRecipeIngredientField = ()=>{
  206. let body = document.querySelector("#recipes tbody");
  207. let row = document.createElement("tr");
  208. body.appendChild(row);
  209. let ingTd = document.createElement("td");
  210. row.appendChild(ingTd);
  211. let ingName = document.createElement("select");
  212. for(let ingredient of data.ingredients){
  213. let newOption = document.createElement("option");
  214. newOption.innerText = ingredient.name;
  215. newOption.value = ingredient.id;
  216. ingName.appendChild(newOption);
  217. }
  218. ingTd.appendChild(ingName);
  219. let quantTd = document.createElement("td");
  220. row.appendChild(quantTd);
  221. let ingQuant = document.createElement("input");
  222. ingQuant.type = "number";
  223. ingQuant.step = "0.01";
  224. ingQuant.min = "0";
  225. quantTd.appendChild(ingQuant);
  226. let removeTd = document.createElement("td");
  227. row.appendChild(removeTd);
  228. let removeButton = document.createElement("button");
  229. removeButton.innerText = "-";
  230. removeButton.onclick = ()=>{row.parentNode.removeChild(row)};
  231. removeTd.appendChild(removeButton);
  232. }
  233. let changeRecipe = (num)=>{
  234. let body = document.querySelector("#recipes tbody");
  235. let recipeIngredients = [];
  236. while(body.children.length > 0){
  237. let row = body.firstChild;
  238. recipeIngredients.push({
  239. id: row.children[0].children[0].value,
  240. quantity: row.children[1].children[0].value
  241. });
  242. recipeData[recipeDataIndex].ingredients = recipeIngredients;
  243. body.removeChild(row);
  244. }
  245. recipeDataIndex += num;
  246. showRecipe();
  247. }
  248. let submitAll = ()=>{
  249. let body = document.querySelector("#recipes tbody");
  250. data.recipes = [];
  251. let recipeIngredients = [];
  252. while(body.children.length > 0){
  253. let row = body.firstChild;
  254. recipeIngredients.push({
  255. id: row.children[0].children[0].value,
  256. quantity: row.children[1].children[0].value
  257. });
  258. recipeData[recipeDataIndex].ingredients = recipeIngredients;
  259. body.removeChild(row);
  260. }
  261. for(let recipe of recipeData){
  262. let newRecipe = {
  263. cloverId: recipe.id,
  264. name: recipe.name,
  265. ingredients: []
  266. };
  267. for(let ingredient of recipe.ingredients){
  268. newRecipe.ingredients.push({
  269. id: ingredient.id,
  270. quantity: ingredient.quantity
  271. });
  272. }
  273. data.recipes.push(newRecipe);
  274. }
  275. let form = document.createElement("form");
  276. form.method = "post";
  277. form.action = "/merchant/create"
  278. let dataInput = document.createElement("input");
  279. dataInput.type = "hidden";
  280. dataInput.name = "data";
  281. dataInput.value = JSON.stringify(data);
  282. form.appendChild(dataInput);
  283. document.body.appendChild(form);
  284. form.submit();
  285. }
  286. populateIngredients();
  287. updateState(0);