newRecipe.js 4.0 KB

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