recipeBook.js 3.9 KB

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