newRecipe.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. category: document.getElementById("newRecipeCategory").value,
  51. price: parseInt(document.getElementById("newRecipePrice").value * 100),
  52. ingredients: []
  53. };
  54. let ingredients = document.getElementById("newRecipeChosenList").children;
  55. for(let i = 0; i < ingredients.length; i++){
  56. let ingredient = ingredients[i].ingredient;
  57. data.ingredients.push({
  58. ingredient: ingredient.id,
  59. quantity: controller.baseUnit(ingredients[i].children[1].children[0].value, ingredient.unit)
  60. });
  61. }
  62. let loader = document.getElementById("loaderContainer");
  63. loader.style.display = "flex";
  64. fetch("/recipe/create", {
  65. method: "post",
  66. headers: {
  67. "Content-Type": "application/json"
  68. },
  69. body: JSON.stringify(data)
  70. })
  71. .then(response => response.json())
  72. .then((response)=>{
  73. if(typeof(response) === "string"){
  74. controller.createBanner(response, "error");
  75. }else{
  76. merchant.addRecipes([response]);
  77. state.updateRecipes();
  78. controller.createBanner("RECIPE CREATED", "success");
  79. controller.openStrand("recipeBook");
  80. }
  81. })
  82. .catch((err)=>{
  83. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  84. })
  85. .finally(()=>{
  86. loader.style.display = "none";
  87. });
  88. },
  89. submitSpreadsheet: function(){
  90. event.preventDefault();
  91. controller.closeModal();
  92. const file = document.getElementById("spreadsheetInput").files[0];
  93. let data = new FormData();
  94. data.append("recipes", file);
  95. let loader = document.getElementById("loaderContainer");
  96. loader.style.display = "flex";
  97. fetch("/recipes/create/spreadsheet", {
  98. method: "post",
  99. body: data
  100. })
  101. .then(response => response.json())
  102. .then((response)=>{
  103. if(typeof(response) === "string"){
  104. controller.createBanner(response, "error");
  105. }else{
  106. merchant.addRecipes(response);
  107. state.updateRecipes();
  108. controller.createBanner("ALL RECIPES SUCCESSFULLY CREATED", "success");
  109. controller.openStrand("recipeBook");
  110. }
  111. })
  112. .catch((err)=>{
  113. controller.createBanner("UNABLE TO DISPLAY NEW RECIPES. PLEASE REFRESH THE PAGE", "error");
  114. })
  115. .finally(()=>{
  116. loader.style.display = "none";
  117. });
  118. }
  119. };
  120. module.exports = newRecipe;