recipeBook.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. const Recipe = require("../classes/Recipe.js");
  2. let recipeBook = {
  3. isPopulated: false,
  4. recipeDivList: [],
  5. display: function(){
  6. if(!this.isPopulated){
  7. this.populateRecipes();
  8. let button = document.getElementById("recipeUpdate");
  9. switch(merchant.pos){
  10. case "square":
  11. button.innerText = "UPDATE";
  12. button.onclick = ()=>{this.posUpdate()};
  13. break;
  14. case "none":
  15. button.innerText = "NEW";
  16. button.onclick = ()=>{controller.openSidebar("addRecipe")};
  17. break;
  18. }
  19. document.getElementById("recipeSearch").oninput = ()=>{this.search()};
  20. this.populateRecipes();
  21. this.isPopulated = true;
  22. }
  23. },
  24. populateRecipes: function(){
  25. let recipeList = document.getElementById("recipeList");
  26. let template = document.getElementById("recipe").content.children[0];
  27. this.recipeDivList = [];
  28. while(recipeList.children.length > 0){
  29. recipeList.removeChild(recipeList.firstChild);
  30. }
  31. for(let i = 0; i < merchant.recipes.length; i++){
  32. if(merchant.recipes[i].hidden === true) continue;
  33. let recipeDiv = template.cloneNode(true);
  34. recipeDiv.onclick = ()=>{
  35. controller.openSidebar("recipeDetails", merchant.recipes[i]);
  36. recipeDiv.classList.add("active");
  37. }
  38. recipeDiv._name = merchant.recipes[i].name;
  39. recipeList.appendChild(recipeDiv);
  40. recipeDiv.children[0].innerText = merchant.recipes[i].name;
  41. recipeDiv.children[1].innerText = `$${merchant.recipes[i].price.toFixed(2)}`;
  42. this.recipeDivList.push(recipeDiv);
  43. }
  44. },
  45. search: function(){
  46. let input = document.getElementById("recipeSearch").value.toLowerCase();
  47. let recipeList = document.getElementById("recipeList");
  48. let matchingRecipes = [];
  49. for(let i = 0; i < this.recipeDivList.length; i++){
  50. if(this.recipeDivList[i]._name.toLowerCase().includes(input)){
  51. matchingRecipes.push(this.recipeDivList[i]);
  52. }
  53. }
  54. while(recipeList.children.length > 0){
  55. recipeList.removeChild(recipeList.firstChild);
  56. }
  57. for(let i = 0; i < matchingRecipes.length; i++){
  58. recipeList.appendChild(matchingRecipes[i]);
  59. }
  60. },
  61. posUpdate: function(){
  62. let loader = document.getElementById("loaderContainer");
  63. loader.style.display = "flex";
  64. let url = `/recipes/update/${merchant.pos}`;
  65. fetch(url, {
  66. method: "GET",
  67. headers: {
  68. "Content-Type": "application/json;charset=utf-8"
  69. },
  70. })
  71. .then(response => response.json())
  72. .then((response)=>{
  73. if(typeof(response) === "string"){
  74. controller.createBanner(response, "error");
  75. }else{
  76. let newRecipes = [];
  77. for(let i = 0; i < response.new.length; i++){
  78. newRecipes.push(new Recipe(
  79. response.new[i]._id,
  80. response.new[i].name,
  81. response.new[i].price,
  82. merchant,
  83. [],
  84. response.new[i].hidden
  85. ));
  86. }
  87. merchant.addRecipes(newRecipes);
  88. for(let i = 0; i < response.removed.length; i++){
  89. for(let j = 0; j < merchant.recipes.length; j++){
  90. if(merchant.recipes[j].id === response.removed[i]._id){
  91. merchant.removeRecipe(merchant.recipes[j]);
  92. break;
  93. }
  94. }
  95. }
  96. state.updateRecipes();
  97. controller.createBanner("RECIPES SUCCESSFULLY UPDATED", "success");
  98. }
  99. })
  100. .catch((err)=>{
  101. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  102. })
  103. .finally(()=>{
  104. loader.style.display = "none";
  105. });
  106. }
  107. }
  108. module.exports = recipeBook;