editIngredient.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. let editIngredient = {
  2. display: function(ingredient){
  3. let buttonList = document.getElementById("unitButtons");
  4. let quantLabel = document.getElementById("editIngQuantityLabel");
  5. let specialLabel = document.getElementById("editSpecialLabel");
  6. //Clear any existing data
  7. while(buttonList.children.length > 0){
  8. buttonList.removeChild(buttonList.firstChild);
  9. }
  10. //Populate basic fields
  11. document.getElementById("editIngTitle").innerText = ingredient.ingredient.name;
  12. document.getElementById("editIngName").value = ingredient.ingredient.name;
  13. document.getElementById("editIngCategory").value = ingredient.ingredient.category;
  14. quantLabel.innerText = `CURRENT STOCK (${ingredient.ingredient.unit.toUpperCase()})`;
  15. document.getElementById("editIngSubmit").onclick = ()=>{this.submit(ingredient)};
  16. //Make any changes for special ingredients
  17. if(ingredient.ingredient.unit === "bottle"){
  18. specialLabel.style.display = "flex";
  19. specialLabel.innerText = `BOTTLE SIZE (${ingredient.ingredient.unitType.toUpperCase()}):`;
  20. let sizeInput = document.createElement("input");
  21. sizeInput.id = "editIngSpecialSize";
  22. sizeInput.type = "number";
  23. sizeInput.min = "0";
  24. sizeInput.step = "0.01";
  25. sizeInput.value = ingredient.ingredient.unitSize.toFixed(2);
  26. specialLabel.appendChild(sizeInput);
  27. }else{
  28. specialLabel.style.display = "none";
  29. }
  30. //Populate sub ingredients
  31. let list = document.getElementById("subIngredientList");
  32. while(list.children.length > 0){
  33. list.removeChild(list.firstChild);
  34. }
  35. //Populate the unit buttons
  36. const units = ingredient.ingredient.getPotentialUnits();
  37. for(let i = 0; i < units.length; i++){
  38. let button = document.createElement("button");
  39. button.classList.add("unitButton");
  40. button.innerText = units[i].toUpperCase();
  41. button.onclick = ()=>{this.changeUnit(button)};
  42. buttonList.appendChild(button);
  43. if(units[i] === ingredient.ingredient.unit) button.classList.add("unitActive");
  44. }
  45. let quantInput = document.createElement("input");
  46. quantInput.id = "editIngQuantity";
  47. quantInput.type = "number";
  48. quantInput.min = "0";
  49. quantInput.step = "0.01";
  50. quantInput.value = ingredient.quantity.toFixed(2);
  51. quantLabel.appendChild(quantInput);
  52. },
  53. changeUnit(button){
  54. let buttons = document.getElementById("unitButtons");
  55. for(let i = 0; i < buttons.children.length; i++){
  56. buttons.children[i].classList.remove("unitActive");
  57. }
  58. button.classList.add("unitActive");
  59. },
  60. submit(ingredient){
  61. const quantity = parseFloat(document.getElementById("editIngQuantityLabel").children[0].value);
  62. let data = {
  63. id: ingredient.ingredient.id,
  64. name: document.getElementById("editIngName").value,
  65. category: document.getElementById("editIngCategory").value
  66. }
  67. data.quantity = ingredient.convertToBase(quantity);
  68. //Get the measurement unit
  69. let units = document.getElementById("unitButtons");
  70. for(let i = 0; i < units.children.length; i++){
  71. if(units.children[i].classList.contains("unitActive")){
  72. data.unit = units.children[i].innerText.toLowerCase();
  73. break;
  74. }
  75. }
  76. let loader = document.getElementById("loaderContainer");
  77. loader.style.display = "flex";
  78. fetch("/ingredients/update", {
  79. method: "put",
  80. headers: {
  81. "Content-Type": "application/json;charset=utf-8"
  82. },
  83. body: JSON.stringify(data)
  84. })
  85. .then(response => response.json())
  86. .then((response)=>{
  87. if(typeof(response) === "string"){
  88. controller.createBanner(response, "error");
  89. }else{
  90. merchant.removeIngredient(merchant.getIngredient(response.ingredient._id));
  91. merchant.addIngredients([response]);
  92. state.updateIngredients();
  93. controller.openStrand("ingredients");
  94. controller.createBanner("INGREDIENT UPDATED", "success");
  95. }
  96. })
  97. .catch((err)=>{
  98. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  99. })
  100. .finally(()=>{
  101. loader.style.display = "none";
  102. });
  103. }
  104. }
  105. module.exports = editIngredient;