recipeBook.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. window.recipeBookStrandObj = {
  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. for(let i = 0; i < response.new.length; i++){
  65. merchant.editRecipe(new Recipe(
  66. response.new[i]._id,
  67. response.new[i].name,
  68. response.new[i].price,
  69. merchant,
  70. []
  71. ));
  72. }
  73. for(let i = 0; i < response.removed.length; i++){
  74. for(let j = 0; j < merchant.recipes.length; j++){
  75. if(response.removed[i]._id === merchant.recipes[j].id){
  76. merchant.editRecipe(merchant.recipes[j], true);
  77. break;
  78. }
  79. }
  80. }
  81. })
  82. .catch((err)=>{
  83. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  84. })
  85. .finally(()=>{
  86. loader.style.display = "none";
  87. });
  88. }
  89. }