newRecipe.js 4.6 KB

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