addIngredient.js 3.5 KB

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