addIngredient.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. if(typeof(ingredient.data) === "string"){
  36. banner.createError(ingredient.data);
  37. }else{
  38. merchant.inventory.push(ingredient.data);
  39. inventoryObj.display();
  40. inventoryObj.filter();
  41. }
  42. })
  43. .catch((err)=>{
  44. banner.createError("Something went wrong and the ingredient could not be added");
  45. });
  46. }
  47. },
  48. submitNew: function(){
  49. event.preventDefault();
  50. let ingredient = {
  51. name: document.querySelector("#newName").value,
  52. category: document.querySelector("#newCategory").value,
  53. unit: document.querySelector("#newUnit").value
  54. }
  55. let quantity = document.querySelector("#newQuantity").value;
  56. if(validator.ingredient.all(ingredient, quantity)){
  57. axios.post("/ingredients/createone", {ingredient: ingredient, quantity: quantity})
  58. .then((item)=>{
  59. merchant.inventory.push(item.data);
  60. inventoryObj.display();
  61. inventoryObj.filter();
  62. })
  63. .catch((err)=>{
  64. banner.createError("Something went wrong and the ingredient could not be created");
  65. });
  66. }
  67. }
  68. }