editIngredient.js 5.1 KB

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