newRecipe.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. let newRecipe = {
  2. display: function(Recipe){
  3. document.getElementById("newRecipeName").value = "";
  4. document.getElementById("newRecipePrice").value = "";
  5. document.getElementById("ingredientCount").value = 1;
  6. let categories = merchant.categorizeIngredients();
  7. let ingredientsSelect = document.getElementById("recipeInputIngredients");
  8. while(ingredientsSelect.children.length > 0){
  9. ingredientsSelect.removeChild(ingredientsSelect.firstChild);
  10. }
  11. this.changeIngredientCount(categories);
  12. document.getElementById("ingredientCount").onchange = ()=>{this.changeIngredientCount(categories)};
  13. document.getElementById("submitNewRecipe").onclick = ()=>{this.submit(Recipe)};
  14. },
  15. //Updates the number of ingredient inputs displayed for new recipes
  16. changeIngredientCount: function(categories){
  17. let newCount = document.getElementById("ingredientCount").value;
  18. let ingredientsDiv = document.getElementById("recipeInputIngredients");
  19. let template = document.getElementById("recipeInputIngredient").content.children[0];
  20. let oldCount = ingredientsDiv.children.length;
  21. if(newCount > oldCount){
  22. let newDivs = newCount - oldCount;
  23. for(let i = 0; i < newDivs; i++){
  24. let newNode = template.cloneNode(true);
  25. newNode.children[0].innnerText = `INGREDIENT ${i + oldCount}`;
  26. newNode.children[2].children[0].value = 0;
  27. for(let j = 0; j < categories.length; j++){
  28. let optgroup = document.createElement("optgroup");
  29. optgroup.label = categories[j].name;
  30. for(let k = 0; k < categories[j].ingredients.length; k++){
  31. let option = document.createElement("option");
  32. option.innerText = categories[j].ingredients[k].ingredient.getNameAndUnit();
  33. option.ingredient = categories[j].ingredients[k];
  34. optgroup.appendChild(option);
  35. }
  36. newNode.children[1].children[0].appendChild(optgroup);
  37. }
  38. ingredientsDiv.appendChild(newNode);
  39. }
  40. for(let i = 0; i < newCount; i++){
  41. ingredientsDiv.children[i].children[0].innerText = `INGREDIENT ${i + 1}`;
  42. }
  43. }else if(newCount < oldCount){
  44. let newDivs = oldCount - newCount;
  45. for(let i = 0; i < newDivs; i++){
  46. ingredientsDiv.removeChild(ingredientsDiv.children[ingredientsDiv.children.length-1]);
  47. }
  48. }
  49. },
  50. submit: function(Recipe){
  51. let newRecipe = {
  52. name: document.getElementById("newRecipeName").value,
  53. price: document.getElementById("newRecipePrice").value,
  54. ingredients: []
  55. }
  56. let inputs = document.getElementById("recipeInputIngredients").children;
  57. for(let i = 0; i < inputs.length; i++){
  58. let sel = inputs[i].children[1].children[0];
  59. let ingredient = sel.options[sel.selectedIndex].ingredient;
  60. newRecipe.ingredients.push({
  61. ingredient: ingredient.ingredient.id,
  62. quantity: ingredient.convertToBase(inputs[i].children[2].children[0].value)
  63. });
  64. }
  65. let loader = document.getElementById("loaderContainer");
  66. loader.style.display = "flex";
  67. fetch("/recipe/create", {
  68. method: "POST",
  69. headers: {
  70. "Content-Type": "application/json;charset=utf-8"
  71. },
  72. body: JSON.stringify(newRecipe)
  73. })
  74. .then((response) => response.json())
  75. .then((response)=>{
  76. if(typeof(response) === "string"){
  77. banner.createError(response);
  78. }else{
  79. let ingredients = [];
  80. for(let i = 0; i < response.ingredients.length; i++){
  81. for(let j = 0; j < merchant.ingredients.length; j++){
  82. if(merchant.ingredients[j].ingredient.id === response.ingredients[i].ingredient){
  83. ingredients.push({
  84. ingredient: merchant.ingredients[j].ingredient,
  85. quantity: response.ingredients[i].quantity
  86. });
  87. break;
  88. }
  89. }
  90. }
  91. merchant.addRecipe(new Recipe(
  92. response._id,
  93. response.name,
  94. response.price,
  95. ingredients,
  96. merchant
  97. ));
  98. banner.createNotification("RECIPE CREATED");
  99. controller.openStrand("recipeBook");
  100. }
  101. })
  102. .catch((err)=>{
  103. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  104. })
  105. .finally(()=>{
  106. loader.style.display = "none";
  107. });
  108. },
  109. }
  110. module.exports = newRecipe;