newRecipe.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. module.exports = {
  2. display: function(Recipe){
  3. let ingredientsSelect = document.querySelector("#recipeInputIngredients select");
  4. let categories = merchant.categorizeIngredients();
  5. while(ingredientsSelect.children.length > 0){
  6. ingredientsSelect.removeChild(ingredientsSelect.firstChild);
  7. }
  8. for(let i = 0; i < categories.length; i++){
  9. let optgroup = document.createElement("optgroup");
  10. optgroup.label = categories[i].name;
  11. ingredientsSelect.appendChild(optgroup);
  12. for(let j = 0; j < categories[i].ingredients.length; j++){
  13. let option = document.createElement("option");
  14. option.value = categories[i].ingredients[j].ingredient.id;
  15. option.innerText = `${categories[i].ingredients[j].ingredient.name} (${categories[i].ingredients[j].ingredient.unit})`;
  16. optgroup.appendChild(option);
  17. }
  18. }
  19. document.getElementById("ingredientCount").onclick = ()=>{this.changeRecipeCount()};
  20. document.getElementById("submitNewRecipe").onclick = ()=>{this.submit(Recipe)};
  21. },
  22. //Updates the number of ingredient inputs displayed for new recipes
  23. changeRecipeCount: function(){
  24. let newCount = document.getElementById("ingredientCount").value;
  25. let ingredientsDiv = document.getElementById("recipeInputIngredients");
  26. let oldCount = ingredientsDiv.children.length;
  27. if(newCount > oldCount){
  28. let newDivs = newCount - oldCount;
  29. for(let i = 0; i < newDivs; i++){
  30. let newNode = ingredientsDiv.children[0].cloneNode(true);
  31. newNode.children[2].children[0].value = "";
  32. ingredientsDiv.appendChild(newNode);
  33. }
  34. for(let i = 0; i < newCount; i++){
  35. ingredientsDiv.children[i].children[0].innerText = `INGREDIENT ${i + 1}`;
  36. }
  37. }else if(newCount < oldCount){
  38. let newDivs = oldCount - newCount;
  39. for(let i = 0; i < newDivs; i++){
  40. ingredientsDiv.removeChild(ingredientsDiv.children[ingredientsDiv.children.length-1]);
  41. }
  42. }
  43. },
  44. submit: function(Recipe){
  45. let newRecipe = {
  46. name: document.getElementById("newRecipeName").value,
  47. price: document.getElementById("newRecipePrice").value,
  48. ingredients: []
  49. }
  50. let inputs = document.querySelectorAll("#recipeInputIngredients > div");
  51. for(let i = 0; i < inputs.length; i++){
  52. for(let j = 0; j < merchant.ingredients.length; j++){
  53. if(merchant.ingredients[j].ingredient.id === inputs[i].children[1].children[0].value){
  54. newRecipe.ingredients.push({
  55. ingredient: inputs[i].children[1].children[0].value,
  56. quantity: controller.convertToMain(merchant.ingredients[j].ingredient.unit, inputs[i].children[2].children[0].value)
  57. });
  58. break;
  59. }
  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;charset=utf-8"
  68. },
  69. body: JSON.stringify(newRecipe)
  70. })
  71. .then((response) => response.json())
  72. .then((response)=>{
  73. if(typeof(response) === "string"){
  74. banner.createError(response);
  75. }else{
  76. let recipe = new Recipe(
  77. response._id,
  78. response.name,
  79. response.price,
  80. response.ingredients,
  81. merchant,
  82. );
  83. merchant.editRecipes([recipe]);
  84. banner.createNotification("RECIPE CREATED");
  85. }
  86. })
  87. .catch((err)=>{
  88. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  89. })
  90. .finally(()=>{
  91. loader.style.display = "none";
  92. });
  93. },
  94. }