addIngredient.js 3.0 KB

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