recipeBook.js 4.1 KB

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