editIngredient.js 4.8 KB

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