recipeBook.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. let recipeBook = {
  2. isPopulated: false,
  3. recipeDivList: [],
  4. display: function(Recipe){
  5. if(!this.isPopulated){
  6. this.populateRecipes();
  7. if(merchant.pos !== "none"){
  8. document.getElementById("posUpdateRecipe").onclick = ()=>{this.posUpdate(Recipe)};
  9. }
  10. document.getElementById("recipeSearch").oninput = ()=>{this.search()};
  11. document.getElementById("recipeClearButton").onclick = ()=>{this.clearSorting()};
  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 = ()=>{controller.openSidebar("recipeDetails", merchant.recipes[i])};
  26. recipeDiv._name = merchant.recipes[i].name;
  27. recipeList.appendChild(recipeDiv);
  28. recipeDiv.children[0].innerText = merchant.recipes[i].name;
  29. recipeDiv.children[1].innerText = `$${merchant.recipes[i].price.toFixed(2)}`;
  30. this.recipeDivList.push(recipeDiv);
  31. }
  32. },
  33. search: function(){
  34. let input = document.getElementById("recipeSearch").value.toLowerCase();
  35. let recipeList = document.getElementById("recipeList");
  36. let clearButton = document.getElementById("recipeClearButton");
  37. let matchingRecipes = [];
  38. for(let i = 0; i < this.recipeDivList.length; i++){
  39. if(this.recipeDivList[i]._name.toLowerCase().includes(input)){
  40. matchingRecipes.push(this.recipeDivList[i]);
  41. }
  42. }
  43. while(recipeList.children.length > 0){
  44. recipeList.removeChild(recipeList.firstChild);
  45. }
  46. for(let i = 0; i < matchingRecipes.length; i++){
  47. recipeList.appendChild(matchingRecipes[i]);
  48. }
  49. if(input === ""){
  50. clearButton.style.display = "none";
  51. }else{
  52. clearButton.style.display = "inline";
  53. }
  54. },
  55. clearSorting: function(){
  56. document.getElementById("recipeSearch").value = "";
  57. this.search();
  58. },
  59. posUpdate: function(Recipe){
  60. let loader = document.getElementById("loaderContainer");
  61. loader.style.display = "flex";
  62. let url = `/recipe/update/${merchant.pos}`;
  63. fetch(url, {
  64. method: "GET",
  65. headers: {
  66. "Content-Type": "application/json;charset=utf-8"
  67. },
  68. })
  69. .then(response => response.json())
  70. .then((response)=>{
  71. for(let i = 0; i < response.new.length; i++){
  72. const recipe = new Recipe(
  73. response.new[i]._id,
  74. response.new[i].name,
  75. response.new[i].price,
  76. merchant,
  77. []
  78. );
  79. merchant.addRecipe(recipe);
  80. }
  81. for(let i = 0; i < response.removed.length; i++){
  82. const recipe = new Recipe(
  83. response.removed[i]._id,
  84. response.removed[i].name,
  85. response.removed[i].price,
  86. merchant,
  87. []
  88. );
  89. merchant.removeRecipe(recipe);
  90. }
  91. })
  92. .catch((err)=>{
  93. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  94. })
  95. .finally(()=>{
  96. loader.style.display = "none";
  97. });
  98. }
  99. }
  100. module.exports = recipeBook;