newRecipe.js 4.1 KB

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