recipeBook.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. if(typeof(response) === "string"){
  72. banner.createError(response);
  73. }else{
  74. for(let i = 0; i < response.new.length; i++){
  75. const recipe = new Recipe(
  76. response.new[i]._id,
  77. response.new[i].name,
  78. response.new[i].price,
  79. merchant,
  80. []
  81. );
  82. merchant.addRecipe(recipe);
  83. }
  84. for(let i = 0; i < response.removed.length; i++){
  85. for(let j = 0; j < merchant.recipes.length; j++){
  86. if(merchant.recipes[j].id === response.removed[i]._id){
  87. merchant.removeRecipe(merchant.recipes[j]);
  88. break;
  89. }
  90. }
  91. }
  92. this.display();
  93. }
  94. })
  95. .catch((err)=>{
  96. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  97. })
  98. .finally(()=>{
  99. loader.style.display = "none";
  100. });
  101. }
  102. }
  103. module.exports = recipeBook;