newRecipe.js 5.0 KB

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