recipeBook.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 = ()=>{
  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 clearButton = document.getElementById("recipeClearButton");
  40. let matchingRecipes = [];
  41. for(let i = 0; i < this.recipeDivList.length; i++){
  42. if(this.recipeDivList[i]._name.toLowerCase().includes(input)){
  43. matchingRecipes.push(this.recipeDivList[i]);
  44. }
  45. }
  46. while(recipeList.children.length > 0){
  47. recipeList.removeChild(recipeList.firstChild);
  48. }
  49. for(let i = 0; i < matchingRecipes.length; i++){
  50. recipeList.appendChild(matchingRecipes[i]);
  51. }
  52. if(input === ""){
  53. clearButton.style.display = "none";
  54. }else{
  55. clearButton.style.display = "inline";
  56. }
  57. },
  58. clearSorting: function(){
  59. document.getElementById("recipeSearch").value = "";
  60. this.search();
  61. },
  62. posUpdate: function(Recipe){
  63. let loader = document.getElementById("loaderContainer");
  64. loader.style.display = "flex";
  65. let url = `/recipe/update/${merchant.pos}`;
  66. fetch(url, {
  67. method: "GET",
  68. headers: {
  69. "Content-Type": "application/json;charset=utf-8"
  70. },
  71. })
  72. .then(response => response.json())
  73. .then((response)=>{
  74. if(typeof(response) === "string"){
  75. banner.createError(response);
  76. }else{
  77. for(let i = 0; i < response.new.length; i++){
  78. const recipe = new Recipe(
  79. response.new[i]._id,
  80. response.new[i].name,
  81. response.new[i].price,
  82. merchant,
  83. []
  84. );
  85. merchant.addRecipe(recipe);
  86. }
  87. for(let i = 0; i < response.removed.length; i++){
  88. for(let j = 0; j < merchant.recipes.length; j++){
  89. if(merchant.recipes[j].id === response.removed[i]._id){
  90. merchant.removeRecipe(merchant.recipes[j]);
  91. break;
  92. }
  93. }
  94. }
  95. this.display();
  96. }
  97. })
  98. .catch((err)=>{
  99. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  100. })
  101. .finally(()=>{
  102. loader.style.display = "none";
  103. });
  104. }
  105. }
  106. module.exports = recipeBook;