newRecipe.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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("newRecipeSearch").onkeyup = ()=>{this.populateChoices()};
  14. this.unchosen = [];
  15. for(let i = 0; i < merchant.inventory.length; i++){
  16. this.unchosen.push(merchant.inventory[i].ingredient);
  17. }
  18. this.populateChoices();
  19. },
  20. populateChoices: function(){
  21. this.unchosen.sort((a, b) => (a.name > b.name) ? 1 : -1);
  22. let searchStr = document.getElementById("newRecipeSearch").value;
  23. let list = document.getElementById("recipeChoices");
  24. while(list.children.length > 0){
  25. list.removeChild(list.firstChild);
  26. }
  27. for(let i = 0; i < this.unchosen.length; i++){
  28. if(searchStr === "" || this.unchosen[i].name.toLowerCase().includes(searchStr)){
  29. let ingredient = document.createElement("button");
  30. ingredient.innerText = this.unchosen[i].name;
  31. ingredient.classList.add("choosable");
  32. ingredient.classList.add("selection");
  33. ingredient.onclick = ()=>{
  34. this.add(this.unchosen[i]);
  35. this.unchosen.splice(i, 1);
  36. this.populateChoices();
  37. };
  38. list.appendChild(ingredient);
  39. }
  40. }
  41. },
  42. add: function(ingredient){
  43. let element = document.getElementById("newRecipeChosenIngredient").content.children[0].cloneNode(true);
  44. element.children[0].children[0].innerText = ingredient.name;
  45. element.children[1].children[0].placeholder = "QUANTITY";
  46. element.children[0].children[1].onclick = ()=>{
  47. this.unchosen.push(ingredient);
  48. element.parentElement.removeChild(element);
  49. this.populateChoices();
  50. };
  51. element.ingredient = ingredient;
  52. let select = element.children[1].children[1];
  53. if(ingredient.convert.toMass > 0) select.children[0].style.display = "block";
  54. if(ingredient.convert.toVolume > 0) select.children[1].style.display = "block";
  55. if(ingredient.convert.toLength > 0) select.children[2].style.display = "block";
  56. select.value = (ingredient.unit === "bottle") ? "ml" : ingredient.unit;
  57. document.getElementById("newRecipeChosenList").appendChild(element);
  58. },
  59. submit: function(){
  60. let data = {
  61. name: document.getElementById("newRecipeName").value,
  62. price: parseInt(document.getElementById("newRecipePrice").value * 100),
  63. category: document.getElementById("newRecipeCategory").value,
  64. ingredients: []
  65. }
  66. let ingredients = document.getElementById("newRecipeChosenList").children;
  67. for(let i = 0; i < ingredients.length; i++){
  68. let ingredient = ingredients[i].ingredient;
  69. let unit = ingredients[i].children[1].children[1].value;
  70. data.ingredients.push({
  71. ingredient: ingredient.id,
  72. quantity: controller.toBase(ingredients[i].children[1].children[0].value, unit),
  73. unit: unit
  74. });
  75. }
  76. let loader = document.getElementById("loaderContainer");
  77. loader.style.display = "flex";
  78. fetch("/recipe/create", {
  79. method: "post",
  80. headers: {
  81. "Content-Type": "application/json"
  82. },
  83. body: JSON.stringify(data)
  84. })
  85. .then(response => response.json())
  86. .then((response)=>{
  87. if(typeof(response) === "string"){
  88. controller.createBanner(response, "error");
  89. }else{
  90. merchant.addRecipes([response]);
  91. state.updateRecipes();
  92. controller.createBanner("RECIPE CREATED", "success");
  93. controller.openStrand("recipeBook");
  94. }
  95. })
  96. .catch((err)=>{
  97. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  98. })
  99. .finally(()=>{
  100. loader.style.display = "none";
  101. });
  102. }
  103. };
  104. module.exports = newRecipe;