addIngredient.js 4.6 KB

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