recipeBook.js 4.1 KB

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