recipeBook.js 4.3 KB

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