addIngredient.js 3.6 KB

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