addIngredient.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. let addIngredientsObj = {
  2. display: function(){
  3. controller.clearScreen();
  4. controller.addIngredientStrand.style.display = "flex";
  5. },
  6. //Fixerup
  7. //Display the modal to allow for adding a new ingredient
  8. displayAdd: function(){
  9. document.querySelector("#existingIngredient").style.display = "block";
  10. document.querySelector("#quantityInput").style.display = "none";
  11. document.querySelector("#newIngredient").style.display = "none";
  12. document.querySelector("#createNew").onclick = ()=>{this.displayNew();};
  13. let modal = document.querySelector(".add-ingredient");
  14. let removeModal = (modal)=>{modal.style.visibility = "hidden";}
  15. modal.onclick = ()=>{removeModal(modal);};
  16. modal.style.visibility = "visible";
  17. axios.get("/ingredients")
  18. .then((ingredients)=>{
  19. let tbody = document.querySelector("#existingIngredient table tbody");
  20. while(tbody.children.length > 0){
  21. tbody.removeChild(tbody.firstChild);
  22. }
  23. for(let ingredient of ingredients.data){
  24. let row = document.createElement("tr");
  25. tbody.appendChild(row);
  26. let name = document.createElement("td");
  27. name.innerText = ingredient.name;
  28. row.appendChild(name);
  29. let category = document.createElement("td");
  30. category.innerText = ingredient.category;
  31. row.appendChild(category);
  32. let unit = document.createElement("td");
  33. unit.innerText = ingredient.unit;
  34. row.appendChild(unit);
  35. let addButton = document.createElement("button");
  36. addButton.innerText = "Add";
  37. addButton.onclick = ()=>{this.configureAddIngredient(ingredient);};
  38. row.appendChild(addButton);
  39. }
  40. })
  41. .catch((err)=>{
  42. banner.createError("Failed to retrieve ingredients list");
  43. });
  44. },
  45. configureAddIngredient: function(ingredient){
  46. document.querySelector("#existingIngredient").style.display = "none";
  47. document.querySelector("#quantityInput").style.display = "block";
  48. document.querySelector("#newIngredient").style.display = "none";
  49. document.querySelector("#quantityInputTitle").innerText = `${ingredient.name} (${ingredient.unit})`;
  50. let button = document.querySelector("#addIngredient");
  51. let input = document.querySelector("#quantityInput input");
  52. button.onclick = ()=>{this.addIngredient({ingredient: ingredient, quantity: input.value});};
  53. },
  54. createIngredient: function(){
  55. let newItem = {
  56. ingredient: {
  57. name: document.querySelector("#newName").value,
  58. category: document.querySelector("#newCategory").value,
  59. unit: document.querySelector("#newUnit").value
  60. },
  61. quantity: document.querySelector("#newQuantity").value
  62. }
  63. this.addIngredient(newItem);
  64. },
  65. //Update new ingredient on both the page and the database
  66. //Close the modal
  67. addIngredient: function(item){
  68. if(validator.ingredient.all(item.ingredient, item.quantity)){
  69. merchant.inventory.push(item);
  70. if(item.ingredient._id){
  71. axios.post("/merchant/ingredients/create", item)
  72. .then((newMerchant)=>{
  73. this.filter();
  74. banner.createNotification("The new ingredient has been successfully added to your inventory");
  75. })
  76. .catch((err)=>{
  77. banner.createError("There was an error and the ingredient could not be added to your inventory");
  78. console.log(err);
  79. });
  80. }else{
  81. axios.post("/ingredients/createone", item)
  82. .then((newMerchant)=>{
  83. this.filter();
  84. banner.createNotification("The new ingredient has been successfully added to your inventory");
  85. })
  86. .catch((err)=>{
  87. console.log(err);
  88. banner.createError("There was an error and the ingredient could not be added to your inventory");
  89. });
  90. }
  91. }
  92. let modal = document.querySelector(".add-ingredient");
  93. modal.style.visibility = "hidden";
  94. }
  95. }