merchantSetup.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. nameInput.onblur = ()=>{checkValid("name", nameInput)};
  79. name.appendChild(nameInput);
  80. row.appendChild(name);
  81. let category = document.createElement("td");
  82. let categoryInput = document.createElement("input");
  83. categoryInput.type = "text"
  84. categoryInput.onblur = ()=>{checkValid("category", categoryInput)};
  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. quantityInput.onblur = ()=>{checkValid("quantity", quantityInput)};
  92. quantity.appendChild(quantityInput);
  93. row.appendChild(quantity);
  94. let unit = document.createElement("td");
  95. let unitInput = document.createElement("input");
  96. unitInput.type = "text";
  97. unitInput.onblur = ()=>{checkValid("unit", unitInput)};
  98. unit.appendChild(unitInput);
  99. row.appendChild(unit);
  100. let removeTd = document.createElement("td");
  101. let removeButton = document.createElement("button");
  102. removeButton.innerText = "-";
  103. removeButton.onclick = ()=>{removeRow(row)};
  104. removeTd.appendChild(removeButton);
  105. row.appendChild(removeTd);
  106. body.appendChild(row);
  107. newIngredientElements.push(row);
  108. }
  109. let removeRow = (row)=>{
  110. for(let i = 0; i < newIngredientElements.length; i++){
  111. if(newIngredientElements[i] === row){
  112. newIngredientElements.splice(i, 1);
  113. }
  114. }
  115. row.parentNode.removeChild(row);
  116. }
  117. let createIngredientsList = ()=>{
  118. data.ingredients = [];
  119. for(let ingredient of existingIngredientElements){
  120. if(ingredient.children[0].children[0].checked){
  121. data.ingredients.push({
  122. id: ingredient.id,
  123. name: ingredient.children[1].textContent,
  124. quantity: ingredient.children[3].children[0].value,
  125. unitType: ingredient.children[4].textContent
  126. });
  127. }
  128. }
  129. let newIngredient = [];
  130. let newIngredientQuantity = [];
  131. for(let ingredient of newIngredientElements){
  132. newIngredient.push({
  133. name: ingredient.children[0].children[0].value,
  134. category: ingredient.children[1].children[0].value,
  135. unitType: ingredient.children[3].children[0].value
  136. });
  137. newIngredientQuantity.push({
  138. name: ingredient.children[0].children[0].value,
  139. quantity: ingredient.children[2].children[0].value
  140. });
  141. }
  142. let isValid = true;
  143. for(let i = 0; i < newIngredient.length; i++){
  144. if(!validator.ingredient.all(newIngredient[i], newIngredientQuantity[i].quantity)){
  145. isValid = false;
  146. data.ingredients = [];
  147. break;
  148. }
  149. }
  150. if(isValid){
  151. axios.post("/ingredients/create", newIngredient)
  152. .then((result)=>{
  153. for(let ingredient of result.data){
  154. let newIngredient = {
  155. id: ingredient._id,
  156. name: ingredient.name,
  157. unitType: ingredient.unitType
  158. }
  159. for(let item of newIngredientQuantity){
  160. if(ingredient.name === item.name){
  161. newIngredient.quantity = item.quantity;
  162. }
  163. }
  164. data.ingredients.push(newIngredient);
  165. }
  166. banner.createNotification("All ingredients have been created and added to your inventory");
  167. showRecipe();
  168. })
  169. .catch((err)=>{
  170. banner.createError("There has been an error and your ingredients have not been saved");
  171. console.log(err);
  172. });
  173. }
  174. }
  175. //Recipe functions
  176. let showRecipe = ()=>{
  177. let title = document.querySelector("#recipeName");
  178. title.innerText = recipeData[recipeDataIndex].name;
  179. let body = document.querySelector("#recipes tbody");
  180. for(let ing of recipeData[recipeDataIndex].ingredients){
  181. let row = document.createElement("tr");
  182. body.appendChild(row);
  183. let ingTd = document.createElement("td");
  184. row.appendChild(ingTd);
  185. let ingName = document.createElement("select");
  186. for(let ingredient of data.ingredients){
  187. let newOption = document.createElement("option");
  188. newOption.innerText = ingredient.name;
  189. newOption.value = ingredient.id;
  190. if(ingredient.id === ing.id){
  191. newOption.selected = "selected";
  192. }
  193. ingName.appendChild(newOption);
  194. }
  195. ingTd.appendChild(ingName);
  196. let quantTd = document.createElement("td");
  197. row.appendChild(quantTd);
  198. let ingQuant = document.createElement("input");
  199. ingQuant.type = "number";
  200. ingQuant.step = "0.01";
  201. ingQuant.value = ing.quantity;
  202. quantTd.appendChild(ingQuant);
  203. }
  204. let nextButton = document.querySelector("#next");
  205. if(recipeDataIndex === recipeData.length - 1){
  206. nextButton.innerText = "Finish";
  207. nextButton.onclick = submitAll;
  208. }else{
  209. nextButton.innerText = "Next Recipe";
  210. nextButton.onclick = ()=>{changeRecipe(1)};
  211. }
  212. let previousButton = document.querySelector("#previous");
  213. if(recipeDataIndex === 0){
  214. previousButton.style.display = "none";
  215. }else{
  216. previousButton.style.display = "inline-block";
  217. }
  218. }
  219. let addRecipeIngredientField = ()=>{
  220. let body = document.querySelector("#recipes tbody");
  221. let row = document.createElement("tr");
  222. body.appendChild(row);
  223. let ingTd = document.createElement("td");
  224. row.appendChild(ingTd);
  225. let ingName = document.createElement("select");
  226. for(let ingredient of data.ingredients){
  227. let newOption = document.createElement("option");
  228. newOption.innerText = ingredient.name;
  229. newOption.value = ingredient.id;
  230. ingName.appendChild(newOption);
  231. }
  232. ingTd.appendChild(ingName);
  233. let quantTd = document.createElement("td");
  234. row.appendChild(quantTd);
  235. let ingQuant = document.createElement("input");
  236. ingQuant.type = "number";
  237. ingQuant.step = "0.01";
  238. ingQuant.min = "0";
  239. quantTd.appendChild(ingQuant);
  240. let removeTd = document.createElement("td");
  241. row.appendChild(removeTd);
  242. let removeButton = document.createElement("button");
  243. removeButton.innerText = "-";
  244. removeButton.onclick = ()=>{row.parentNode.removeChild(row)};
  245. removeTd.appendChild(removeButton);
  246. }
  247. let changeRecipe = (num)=>{
  248. let body = document.querySelector("#recipes tbody");
  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. recipeDataIndex += num;
  260. showRecipe();
  261. }
  262. let submitAll = ()=>{
  263. let body = document.querySelector("#recipes tbody");
  264. data.recipes = [];
  265. let recipeIngredients = [];
  266. while(body.children.length > 0){
  267. let row = body.firstChild;
  268. recipeIngredients.push({
  269. id: row.children[0].children[0].value,
  270. quantity: row.children[1].children[0].value
  271. });
  272. recipeData[recipeDataIndex].ingredients = recipeIngredients;
  273. body.removeChild(row);
  274. }
  275. for(let recipe of recipeData){
  276. let newRecipe = {
  277. cloverId: recipe.id,
  278. name: recipe.name,
  279. ingredients: []
  280. };
  281. for(let ingredient of recipe.ingredients){
  282. newRecipe.ingredients.push({
  283. id: ingredient.id,
  284. quantity: ingredient.quantity
  285. });
  286. }
  287. data.recipes.push(newRecipe);
  288. }
  289. let form = document.createElement("form");
  290. form.method = "post";
  291. form.action = "/merchant/create"
  292. let dataInput = document.createElement("input");
  293. dataInput.type = "hidden";
  294. dataInput.name = "data";
  295. dataInput.value = JSON.stringify(data);
  296. form.appendChild(dataInput);
  297. document.body.appendChild(form);
  298. form.submit();
  299. }
  300. let checkValid = (valueToCheck, inputField)=>{
  301. console.log(inputField.value);
  302. if(!validator.ingredient[valueToCheck](inputField.value, createBanner = false)){
  303. inputField.classList += " input-error"
  304. }else{
  305. inputField.classList.remove("input-error");
  306. }
  307. }
  308. populateIngredients();
  309. updateState(0);