newRecipe.js 5.5 KB

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