recipeBook.js 3.9 KB

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