recipeBook.js 3.7 KB

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