merchantSetup.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. //refactor
  20. //buttons should run a function which should call the state update, not the other way around
  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. }
  36. }
  37. //Ingredient functions
  38. let populateIngredients = ()=>{
  39. let tBody = document.createElement("tbody");
  40. for(let ingredient of ingredients){
  41. let row = document.createElement("tr");
  42. row.id = ingredient._id;
  43. let add = document.createElement("td");
  44. let checkbox = document.createElement("input");
  45. checkbox.type = "checkbox";
  46. add.appendChild(checkbox);
  47. row.appendChild(add);
  48. let name = document.createElement("td");
  49. name.innerText = ingredient.name;
  50. row.appendChild(name);
  51. let category = document.createElement("td");
  52. category.innerText = ingredient.category;
  53. row.appendChild(category);
  54. let quantity = document.createElement("td");
  55. let quantityInput = document.createElement("input");
  56. quantityInput.type = "number";
  57. quantityInput.step = "0.01";
  58. quantityInput.min = "0";
  59. quantity.appendChild(quantityInput);
  60. row.appendChild(quantity);
  61. let unit = document.createElement("td");
  62. unit.innerText = ingredient.unitType;
  63. row.appendChild(unit);
  64. let idField = document.createElement("input");
  65. idField.type = "hidden";
  66. idField.value = ingredient._id;
  67. tBody.appendChild(row);
  68. let oldTBody = document.querySelector("#ingredient-display tbody");
  69. oldTBody.parentNode.replaceChild(tBody, oldTBody);
  70. existingIngredientElements.push(row);
  71. }
  72. }
  73. let newIngredientField = ()=>{
  74. let body = document.querySelector("#inputField tbody");
  75. let row = document.createElement("tr");
  76. let name = document.createElement("td");
  77. let nameInput = document.createElement("input");
  78. nameInput.type = "text";
  79. nameInput.onblur = ()=>{checkValid("name", nameInput)};
  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. categoryInput.onblur = ()=>{checkValid("category", categoryInput)};
  86. category.appendChild(categoryInput);
  87. row.appendChild(category);
  88. let quantity = document.createElement("td");
  89. let quantityInput = document.createElement("input");
  90. quantityInput.type = "number";
  91. quantityInput.step = "0.01";
  92. quantityInput.onblur = ()=>{checkValid("quantity", quantityInput)};
  93. quantity.appendChild(quantityInput);
  94. row.appendChild(quantity);
  95. let unit = document.createElement("td");
  96. let unitInput = document.createElement("input");
  97. unitInput.type = "text";
  98. unitInput.onblur = ()=>{checkValid("unit", unitInput)};
  99. unit.appendChild(unitInput);
  100. row.appendChild(unit);
  101. let removeTd = document.createElement("td");
  102. let removeButton = document.createElement("button");
  103. removeButton.innerText = "-";
  104. removeButton.onclick = ()=>{removeRow(row)};
  105. removeTd.appendChild(removeButton);
  106. row.appendChild(removeTd);
  107. body.appendChild(row);
  108. newIngredientElements.push(row);
  109. }
  110. let removeRow = (row)=>{
  111. for(let i = 0; i < newIngredientElements.length; i++){
  112. if(newIngredientElements[i] === row){
  113. newIngredientElements.splice(i, 1);
  114. }
  115. }
  116. row.parentNode.removeChild(row);
  117. }
  118. //refactor
  119. //nothin should run unless everything is valid
  120. let createIngredientsList = ()=>{
  121. data.ingredients = [];
  122. for(let ingredient of existingIngredientElements){
  123. if(ingredient.children[0].children[0].checked){
  124. data.ingredients.push({
  125. id: ingredient.id,
  126. name: ingredient.children[1].textContent,
  127. quantity: ingredient.children[3].children[0].value,
  128. unitType: ingredient.children[4].textContent
  129. });
  130. }
  131. }
  132. let newIngredient = [];
  133. let newIngredientQuantity = [];
  134. for(let ingredient of newIngredientElements){
  135. newIngredient.push({
  136. name: ingredient.children[0].children[0].value,
  137. category: ingredient.children[1].children[0].value,
  138. unitType: ingredient.children[3].children[0].value
  139. });
  140. newIngredientQuantity.push({
  141. name: ingredient.children[0].children[0].value,
  142. quantity: ingredient.children[2].children[0].value
  143. });
  144. }
  145. let isValid = true;
  146. for(let i = 0; i < newIngredient.length; i++){
  147. if(!validator.ingredient.all(newIngredient[i], newIngredientQuantity[i].quantity)){
  148. isValid = false;
  149. data.ingredients = [];
  150. break;
  151. }
  152. }
  153. if(isValid){
  154. axios.post("/ingredients/create", newIngredient)
  155. .then((result)=>{
  156. for(let ingredient of result.data){
  157. let newIngredient = {
  158. id: ingredient._id,
  159. name: ingredient.name,
  160. unitType: ingredient.unitType
  161. }
  162. for(let item of newIngredientQuantity){
  163. if(ingredient.name === item.name){
  164. newIngredient.quantity = item.quantity;
  165. }
  166. }
  167. data.ingredients.push(newIngredient);
  168. }
  169. banner.createNotification("All ingredients have been created and added to your inventory");
  170. updateState(1);
  171. showRecipe();
  172. })
  173. .catch((err)=>{
  174. banner.createError("There has been an error and your ingredients have not been saved");
  175. console.log(err);
  176. });
  177. }
  178. }
  179. //Recipe functions
  180. let showRecipe = ()=>{
  181. let title = document.querySelector("#recipeName");
  182. title.innerText = recipeData[recipeDataIndex].name;
  183. let body = document.querySelector("#recipes tbody");
  184. for(let ing of recipeData[recipeDataIndex].ingredients){
  185. let row = document.createElement("tr");
  186. body.appendChild(row);
  187. let ingTd = document.createElement("td");
  188. row.appendChild(ingTd);
  189. let ingName = document.createElement("select");
  190. for(let ingredient of data.ingredients){
  191. let newOption = document.createElement("option");
  192. newOption.innerText = ingredient.name;
  193. newOption.value = ingredient.id;
  194. if(ingredient.id === ing.id){
  195. newOption.selected = "selected";
  196. }
  197. ingName.appendChild(newOption);
  198. }
  199. ingTd.appendChild(ingName);
  200. let quantTd = document.createElement("td");
  201. row.appendChild(quantTd);
  202. let ingQuant = document.createElement("input");
  203. ingQuant.type = "number";
  204. ingQuant.step = "0.01";
  205. ingQuant.value = ing.quantity;
  206. quantTd.appendChild(ingQuant);
  207. }
  208. let nextButton = document.querySelector("#next");
  209. if(recipeDataIndex === recipeData.length - 1){
  210. nextButton.innerText = "Finish";
  211. nextButton.onclick = submitAll;
  212. }else{
  213. nextButton.innerText = "Next Recipe";
  214. nextButton.onclick = ()=>{changeRecipe(1)};
  215. }
  216. let previousButton = document.querySelector("#previous");
  217. if(recipeDataIndex === 0){
  218. previousButton.style.display = "none";
  219. }else{
  220. previousButton.style.display = "inline-block";
  221. }
  222. }
  223. let addRecipeIngredientField = ()=>{
  224. let body = document.querySelector("#recipes tbody");
  225. let row = document.createElement("tr");
  226. body.appendChild(row);
  227. let ingTd = document.createElement("td");
  228. row.appendChild(ingTd);
  229. let ingName = document.createElement("select");
  230. for(let ingredient of data.ingredients){
  231. let newOption = document.createElement("option");
  232. newOption.innerText = ingredient.name;
  233. newOption.value = ingredient.id;
  234. ingName.appendChild(newOption);
  235. }
  236. ingTd.appendChild(ingName);
  237. let quantTd = document.createElement("td");
  238. row.appendChild(quantTd);
  239. let ingQuant = document.createElement("input");
  240. ingQuant.type = "number";
  241. ingQuant.step = "0.01";
  242. ingQuant.min = "0";
  243. quantTd.appendChild(ingQuant);
  244. let removeTd = document.createElement("td");
  245. row.appendChild(removeTd);
  246. let removeButton = document.createElement("button");
  247. removeButton.innerText = "-";
  248. removeButton.onclick = ()=>{row.parentNode.removeChild(row)};
  249. removeTd.appendChild(removeButton);
  250. }
  251. let changeRecipe = (num)=>{
  252. let body = document.querySelector("#recipes tbody");
  253. let recipeIngredients = [];
  254. while(body.children.length > 0){
  255. let row = body.firstChild;
  256. recipeIngredients.push({
  257. id: row.children[0].children[0].value,
  258. quantity: row.children[1].children[0].value
  259. });
  260. recipeData[recipeDataIndex].ingredients = recipeIngredients;
  261. body.removeChild(row);
  262. }
  263. recipeDataIndex += num;
  264. showRecipe();
  265. }
  266. let submitAll = ()=>{
  267. let body = document.querySelector("#recipes tbody");
  268. data.recipes = [];
  269. let recipeIngredients = [];
  270. while(body.children.length > 0){
  271. let row = body.firstChild;
  272. recipeIngredients.push({
  273. id: row.children[0].children[0].value,
  274. quantity: row.children[1].children[0].value
  275. });
  276. recipeData[recipeDataIndex].ingredients = recipeIngredients;
  277. body.removeChild(row);
  278. }
  279. for(let recipe of recipeData){
  280. let newRecipe = {
  281. cloverId: recipe.id,
  282. name: recipe.name,
  283. ingredients: []
  284. };
  285. for(let ingredient of recipe.ingredients){
  286. newRecipe.ingredients.push({
  287. id: ingredient.id,
  288. quantity: ingredient.quantity
  289. });
  290. }
  291. data.recipes.push(newRecipe);
  292. }
  293. let form = document.createElement("form");
  294. form.method = "post";
  295. form.action = "/merchant/create"
  296. let dataInput = document.createElement("input");
  297. dataInput.type = "hidden";
  298. dataInput.name = "data";
  299. dataInput.value = JSON.stringify(data);
  300. form.appendChild(dataInput);
  301. document.body.appendChild(form);
  302. form.submit();
  303. }
  304. let checkValid = (valueToCheck, inputField)=>{
  305. console.log(inputField.value);
  306. if(!validator.ingredient[valueToCheck](inputField.value, createBanner = false)){
  307. inputField.classList += " input-error"
  308. }else{
  309. inputField.classList.remove("input-error");
  310. }
  311. }
  312. populateIngredients();
  313. updateState(0);