addIngredient.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. let addIngredientObj = {
  2. isPopulated: false,
  3. display: function(){
  4. controller.clearScreen();
  5. controller.addIngredientStrand.style.display = "flex";
  6. if(!this.isPopulated){
  7. this.populateIngredients();
  8. this.isPopulated = true;
  9. }
  10. },
  11. populateIngredients: function(){
  12. axios.get("/ingredients")
  13. .then((ingredients)=>{
  14. let select = document.querySelector("#addIngredientStrand select");
  15. for(let ingredient of ingredients.data){
  16. let option = document.createElement("option");
  17. option.value = ingredient._id;
  18. option.innerText = `${ingredient.name} (${ingredient.unit})`;
  19. select.appendChild(option);
  20. }
  21. })
  22. .catch((err)=>{
  23. banner.createError("Could not reach the database");
  24. });
  25. },
  26. submitAdd: function(){
  27. event.preventDefault();
  28. let item = {
  29. ingredient: document.querySelector("#addName").value,
  30. quantity: document.querySelector("#addQuantity").value
  31. }
  32. if(validator.ingredient.quantity){
  33. axios.post("/merchant/ingredients/create", item)
  34. .then((ingredient)=>{
  35. item.ingredient = ingredient.data;
  36. merchant.inventory.push(item);
  37. inventoryObj.display();
  38. inventoryObj.filter();
  39. })
  40. .catch((err)=>{
  41. banner.createError("Something went wrong and the ingredient could not be added");
  42. });
  43. }
  44. },
  45. submitNew: function(){
  46. event.preventDefault();
  47. let ingredient = {
  48. name: document.querySelector("#newName").value,
  49. category: document.querySelector("#newCategory").value,
  50. unit: document.querySelector("#newUnit").value
  51. }
  52. let quantity = document.querySelector("#newQuantity").value;
  53. if(validator.ingredient.all(ingredient, quantity)){
  54. axios.post("/ingredients/createone", {ingredient: ingredient, quantity: quantity})
  55. .then((item)=>{
  56. merchant.inventory.push(item.data);
  57. inventoryObj.display();
  58. inventoryObj.filter();
  59. })
  60. .catch((err)=>{
  61. banner.createError("Something went wrong and the ingredient could not be created");
  62. });
  63. }
  64. }
  65. }