newRecipe.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. let newRecipe = {
  2. unchosen: [],
  3. display: function(){
  4. document.getElementById("sidebarDiv").classList.add("sidebarWide");
  5. document.getElementById("submitNewRecipe").onclick = ()=>{this.submit()};
  6. document.getElementById("recipeFileUpload").onclick = ()=>{controller.openModal("recipeSpreadsheet")};
  7. document.getElementById("newRecipeSearch").onkeyup = ()=>{this.populateChoices()};
  8. this.unchosen = [];
  9. for(let i = 0; i < merchant.inventory.length; i++){
  10. this.unchosen.push(merchant.inventory[i].ingredient);
  11. }
  12. this.populateChoices();
  13. },
  14. populateChoices: function(){
  15. this.unchosen.sort((a, b) => (a.name > b.name) ? 1 : -1);
  16. let searchStr = document.getElementById("newRecipeSearch").value;
  17. let list = document.getElementById("recipeChoices");
  18. while(list.children.length > 0){
  19. list.removeChild(list.firstChild);
  20. }
  21. for(let i = 0; i < this.unchosen.length; i++){
  22. if(searchStr === "" || this.unchosen[i].name.toLowerCase().includes(searchStr)){
  23. let ingredient = document.createElement("button");
  24. ingredient.innerText = this.unchosen[i].name;
  25. ingredient.classList.add("choosable");
  26. ingredient.onclick = ()=>{
  27. this.add(this.unchosen[i]);
  28. this.unchosen.splice(i, 1);
  29. this.populateChoices();
  30. };
  31. list.appendChild(ingredient);
  32. }
  33. }
  34. },
  35. add: function(ingredient){
  36. let element = document.getElementById("newRecipeChosenIngredient").content.children[0].cloneNode(true);
  37. element.children[0].innerText = ingredient.name;
  38. element.children[1].children[0].placeholder = `QUANTITY (${ingredient.unit.toUpperCase()})`;
  39. element.children[1].children[1].onclick = ()=>{
  40. this.unchosen.push(ingredient);
  41. element.parentElement.removeChild(element);
  42. this.populateChoices();
  43. };
  44. element.ingredient = ingredient;
  45. document.getElementById("newRecipeChosenList").appendChild(element);
  46. },
  47. submit: function(){
  48. let data = {
  49. name: document.getElementById("newRecipeName").value,
  50. price: document.getElementById("newRecipePrice").value,
  51. ingredients: []
  52. };
  53. let ingredients = document.getElementById("newRecipeChosenList").children;
  54. for(let i = 0; i < ingredients.length; i++){
  55. let ingredient = ingredients[i].ingredient;
  56. data.ingredients.push({
  57. ingredient: ingredient.id,
  58. quantity: controller.baseUnit(ingredients[i].children[1].children[0].value, ingredient.unit)
  59. });
  60. }
  61. let loader = document.getElementById("loaderContainer");
  62. loader.style.display = "flex";
  63. fetch("/recipe/create", {
  64. method: "post",
  65. headers: {
  66. "Content-Type": "application/json"
  67. },
  68. body: JSON.stringify(data)
  69. })
  70. .then(response => response.json())
  71. .then((response)=>{
  72. if(typeof(response) === "string"){
  73. controller.createBanner(response, "error");
  74. }else{
  75. merchant.addRecipes([response]);
  76. state.updateRecipes();
  77. controller.createBanner("RECIPE CREATED", "success");
  78. controller.openStrand("recipeBook");
  79. }
  80. })
  81. .catch((err)=>{
  82. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  83. })
  84. .finally(()=>{
  85. loader.style.display = "none";
  86. });
  87. },
  88. submitSpreadsheet: function(){
  89. event.preventDefault();
  90. controller.closeModal();
  91. const file = document.getElementById("spreadsheetInput").files[0];
  92. let data = new FormData();
  93. data.append("recipes", file);
  94. let loader = document.getElementById("loaderContainer");
  95. loader.style.display = "flex";
  96. fetch("/recipes/create/spreadsheet", {
  97. method: "post",
  98. body: data
  99. })
  100. .then(response => response.json())
  101. .then((response)=>{
  102. if(typeof(response) === "string"){
  103. controller.createBanner(response, "error");
  104. }else{
  105. merchant.addRecipes(response);
  106. state.updateRecipes();
  107. controller.createBanner("ALL RECIPES SUCCESSFULLY CREATED", "success");
  108. controller.openStrand("recipeBook");
  109. }
  110. })
  111. .catch((err)=>{
  112. controller.createBanner("UNABLE TO DISPLAY NEW RECIPES. PLEASE REFRESH THE PAGE", "error");
  113. })
  114. .finally(()=>{
  115. loader.style.display = "none";
  116. });
  117. }
  118. };
  119. module.exports = newRecipe;