ingredientDetails.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. module.exports = {
  2. ingredient: {},
  3. display: function(ingredient){
  4. this.ingredient = ingredient;
  5. sidebar = document.querySelector("#ingredientDetails");
  6. document.querySelector("#ingredientDetails p").innerText = ingredient.ingredient.category;
  7. document.querySelector("#ingredientDetails h1").innerText = ingredient.ingredient.name;
  8. let ingredientStock = document.getElementById("ingredientStock");
  9. ingredientStock.innerText = `${ingredient.ingredient.convert(ingredient.quantity).toFixed(2)} ${ingredient.ingredient.unit.toUpperCase()}`;
  10. ingredientStock.style.display = "block";
  11. let ingredientInput = document.getElementById("ingredientInput");
  12. ingredientInput.value = ingredient.ingredient.convert(ingredient.quantity).toFixed(2);
  13. ingredientInput.style.display = "none";
  14. let quantities = [];
  15. let now = new Date();
  16. for(let i = 1; i < 31; i++){
  17. let endDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i)
  18. let startDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i - 1);
  19. let indices = merchant.transactionIndices(startDay, endDay);
  20. if(indices === false){
  21. quantities.push(0);
  22. }else{
  23. quantities.push(merchant.singleIngredientSold(indices, ingredient));
  24. }
  25. }
  26. let sum = 0;
  27. for(let quantity of quantities){
  28. sum += quantity;
  29. }
  30. document.querySelector("#dailyUse").innerText = `${(sum/quantities.length).toFixed(2)} ${ingredient.ingredient.unit}`;
  31. let ul = document.querySelector("#ingredientRecipeList");
  32. let recipes = merchant.getRecipesForIngredient(ingredient.ingredient);
  33. while(ul.children.length > 0){
  34. ul.removeChild(ul.firstChild);
  35. }
  36. for(let i = 0; i < recipes.length; i++){
  37. let li = document.createElement("li");
  38. li.innerText = recipes[i].name;
  39. li.onclick = ()=>{
  40. changeStrand("recipeBookStrand");
  41. recipeDetailsComp.display(recipes[i]);
  42. }
  43. ul.appendChild(li);
  44. }
  45. let ingredientButtons = document.getElementById("ingredientButtons");
  46. let units = [];
  47. let unitLabel = document.getElementById("displayUnitLabel");
  48. let defaultButton = document.getElementById("defaultUnit");
  49. if(this.ingredient.ingredient.unitType !== "other"){
  50. units = merchant.units[this.ingredient.ingredient.unitType];
  51. unitLabel.style.display = "block";
  52. defaultButton.style.display = "block";
  53. }else{
  54. unitLabel.style.display = "none";
  55. defaultButton.style.display = "none";
  56. }
  57. while(ingredientButtons.children.length > 0){
  58. ingredientButtons.removeChild(ingredientButtons.firstChild);
  59. }
  60. for(let i = 0; i < units.length; i++){
  61. let button = document.createElement("button");
  62. button.classList.add("unitButton");
  63. button.innerText = units[i].toUpperCase();
  64. button.onclick = ()=>{this.changeUnit(button, units[i])};
  65. ingredientButtons.appendChild(button);
  66. if(units[i] === this.ingredient.ingredient.unit){
  67. button.classList.add("unitActive");
  68. }
  69. }
  70. openSidebar(sidebar);
  71. },
  72. remove: function(){
  73. for(let i = 0; i < merchant.recipes.length; i++){
  74. for(let j = 0; j < merchant.recipes[i].ingredients.length; j++){
  75. if(this.ingredient.ingredient === merchant.recipes[i].ingredients[j].ingredient){
  76. banner.createError("MUST REMOVE INGREDIENT FROM ALL RECIPES BEFORE REMOVING FROM INVENTORY");
  77. return;
  78. }
  79. }
  80. }
  81. let loader = document.getElementById("loaderContainer");
  82. loader.style.display = "flex";
  83. fetch(`/merchant/ingredients/remove/${this.ingredient.ingredient.id}`, {
  84. method: "DELETE",
  85. })
  86. .then((response) => response.json())
  87. .then((response)=>{
  88. if(typeof(response) === "string"){
  89. banner.createError(response);
  90. }else{
  91. banner.createNotification("INGREDIENT REMOVED");
  92. merchant.editIngredients([this.ingredient], true);
  93. }
  94. })
  95. .catch((err)=>{})
  96. .finally(()=>{
  97. loader.style.display = "none";
  98. });
  99. },
  100. edit: function(){
  101. document.getElementById("ingredientStock").style.display = "none";
  102. document.getElementById("ingredientInput").style.display = "block";
  103. document.getElementById("editSubmitButton").style.display = "block";
  104. },
  105. editSubmit: function(){
  106. this.ingredient.quantity = Number(document.getElementById("ingredientInput").value);
  107. let data = [{
  108. id: this.ingredient.ingredient.id,
  109. quantity: this.ingredient.quantity
  110. }];
  111. let loader = document.getElementById("loaderContainer");
  112. loader.style.display = "flex";
  113. if(validator.ingredientQuantity(data[0].quantity)){
  114. fetch("/merchant/ingredients/update", {
  115. method: "PUT",
  116. headers: {
  117. "Content-Type": "application/json;charset=utf-8"
  118. },
  119. body: JSON.stringify(data)
  120. })
  121. .then((response) => response.json())
  122. .then((response)=>{
  123. if(typeof(response) === "string"){
  124. banner.createError(response);
  125. }else{
  126. merchant.editIngredients([this.ingredient]);
  127. banner.createNotification("INGREDIENT UPDATED");
  128. }
  129. })
  130. .catch((err)=>{
  131. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  132. })
  133. .finally(()=>{
  134. loader.style.display = "none";
  135. });
  136. }
  137. },
  138. changeUnit: function(newActive, unit){
  139. this.ingredient.ingredient.unit = unit;
  140. let ingredientButtons = document.querySelectorAll(".unitButton");
  141. for(let i = 0; i < ingredientButtons.length; i++){
  142. ingredientButtons[i].classList.remove("unitActive");
  143. }
  144. newActive.classList.add("unitActive");
  145. homeStrandObj.isPopulated = false;
  146. ingredientsStrandObj.populateByProperty("category");
  147. document.getElementById("ingredientStock").innerText = `${this.ingredient.ingredient.convert(this.ingredient.quantity).toFixed(2)} ${this.ingredient.ingredient.unit.toUpperCase()}`;
  148. },
  149. changeUnitDefault: function(){
  150. let loader = document.getElementById("loaderContainer");
  151. loader.style.display = "flex";
  152. let id = this.ingredient.ingredient.id;
  153. let unit = this.ingredient.ingredient.unit;
  154. fetch(`/merchant/ingredients/update/${id}/${unit}`, {
  155. method: "put",
  156. headers: {
  157. "Content-Type": "application/json;charset=utf-8"
  158. },
  159. })
  160. .then((response)=>{
  161. if(typeof(response) === "string"){
  162. banner.createError(response);
  163. }else{
  164. banner.createNotification("INGREDIENT DEFAULT UNIT UPDATED");
  165. }
  166. })
  167. .catch((err)=>{
  168. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  169. })
  170. .finally(()=>{
  171. loader.style.display = "none";
  172. });
  173. }
  174. }