recipeBook.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. const Recipe = require("../classes/Recipe.js");
  2. let recipeBook = {
  3. isPopulated: false,
  4. recipeDivList: [],
  5. display: function(){
  6. if(!this.isPopulated){
  7. this.populateRecipes();
  8. if(merchant.pos !== "none"){
  9. document.getElementById("posUpdateRecipe").onclick = ()=>{this.posUpdate()};
  10. }
  11. document.getElementById("recipeSearch").oninput = ()=>{this.search()};
  12. this.populateRecipes();
  13. this.isPopulated = true;
  14. }
  15. },
  16. populateRecipes: function(){
  17. let recipeList = document.getElementById("recipeList");
  18. let template = document.getElementById("recipe").content.children[0];
  19. this.recipeDivList = [];
  20. while(recipeList.children.length > 0){
  21. recipeList.removeChild(recipeList.firstChild);
  22. }
  23. for(let i = 0; i < merchant.recipes.length; i++){
  24. let recipeDiv = template.cloneNode(true);
  25. recipeDiv.onclick = ()=>{
  26. controller.openSidebar("recipeDetails", merchant.recipes[i]);
  27. recipeDiv.classList.add("active");
  28. }
  29. recipeDiv._name = merchant.recipes[i].name;
  30. recipeList.appendChild(recipeDiv);
  31. recipeDiv.children[0].innerText = merchant.recipes[i].name;
  32. recipeDiv.children[1].innerText = `$${merchant.recipes[i].price.toFixed(2)}`;
  33. this.recipeDivList.push(recipeDiv);
  34. }
  35. },
  36. search: function(){
  37. let input = document.getElementById("recipeSearch").value.toLowerCase();
  38. let recipeList = document.getElementById("recipeList");
  39. let matchingRecipes = [];
  40. for(let i = 0; i < this.recipeDivList.length; i++){
  41. if(this.recipeDivList[i]._name.toLowerCase().includes(input)){
  42. matchingRecipes.push(this.recipeDivList[i]);
  43. }
  44. }
  45. while(recipeList.children.length > 0){
  46. recipeList.removeChild(recipeList.firstChild);
  47. }
  48. for(let i = 0; i < matchingRecipes.length; i++){
  49. recipeList.appendChild(matchingRecipes[i]);
  50. }
  51. },
  52. posUpdate: function(){
  53. let loader = document.getElementById("loaderContainer");
  54. loader.style.display = "flex";
  55. let url = `/recipes/update/${merchant.pos}`;
  56. fetch(url, {
  57. method: "GET",
  58. headers: {
  59. "Content-Type": "application/json;charset=utf-8"
  60. },
  61. })
  62. .then(response => response.json())
  63. .then((response)=>{
  64. if(typeof(response) === "string"){
  65. controller.createBanner(response, "error");
  66. }else{
  67. let newRecipes = [];
  68. for(let i = 0; i < response.new.length; i++){
  69. newRecipes.push(new Recipe(
  70. response.new[i]._id,
  71. response.new[i].name,
  72. response.new[i].price,
  73. merchant,
  74. []
  75. ));
  76. }
  77. merchant.addRecipes(newRecipes);
  78. for(let i = 0; i < response.removed.length; i++){
  79. for(let j = 0; j < merchant.recipes.length; j++){
  80. if(merchant.recipes[j].id === response.removed[i]._id){
  81. merchant.removeRecipe(merchant.recipes[j]);
  82. break;
  83. }
  84. }
  85. }
  86. controller.createBanner("RECIPES SUCCESSFULLY UPDATED", "success");
  87. this.display();
  88. }
  89. })
  90. .catch((err)=>{
  91. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  92. })
  93. .finally(()=>{
  94. loader.style.display = "none";
  95. });
  96. }
  97. }
  98. module.exports = recipeBook;