recipeBook.js 4.0 KB

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