addIngredient.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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((response)=>{
  14. if(typeof(response.data) === "string"){
  15. banner.createError(response.data);
  16. }else{
  17. let select = document.querySelector("#addIngredientStrand select");
  18. for(let ingredient of response.data){
  19. let option = document.createElement("option");
  20. option.value = ingredient._id;
  21. option.innerText = `${ingredient.name} (${ingredient.unit})`;
  22. select.appendChild(option);
  23. }
  24. }
  25. })
  26. .catch((err)=>{
  27. banner.createError("Error: Could not retrieve ingredients");
  28. inventoryObj.display();
  29. });
  30. },
  31. submitAdd: function(){
  32. event.preventDefault();
  33. let item = {
  34. ingredient: document.querySelector("#addName").value,
  35. quantity: document.querySelector("#addQuantity").value
  36. }
  37. if(validator.ingredient.quantity(item.quantity)){
  38. axios.post("/merchant/ingredients/create", item)
  39. .then((ingredient)=>{
  40. if(typeof(ingredient.data) === "string"){
  41. banner.createError(ingredient.data);
  42. }else{
  43. merchant.inventory.push(ingredient.data);
  44. inventoryObj.display();
  45. inventoryObj.filter();
  46. }
  47. })
  48. .catch((err)=>{
  49. banner.createError("Error: Unable to add ingredient");
  50. });
  51. }
  52. },
  53. submitNew: function(){
  54. event.preventDefault();
  55. let ingredient = {
  56. name: document.querySelector("#newName").value,
  57. category: document.querySelector("#newCategory").value,
  58. unit: document.querySelector("#newUnit").value
  59. }
  60. let quantity = document.querySelector("#newQuantity").value;
  61. if(validator.ingredient.all(ingredient, quantity)){
  62. axios.post("/ingredients/createone", {ingredient: ingredient, quantity: quantity})
  63. .then((response)=>{
  64. if(typeof(response.data) === "string"){
  65. banner.createError(response.data);
  66. }else{
  67. merchant.inventory.push(response.data);
  68. inventoryObj.display();
  69. inventoryObj.filter();
  70. }
  71. })
  72. .catch((err)=>{
  73. banner.createError("Something went wrong and the ingredient could not be created");
  74. });
  75. }
  76. }
  77. }