recipeBook.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. window.recipeBookStrandObj = {
  2. isPopulated: false,
  3. display: function(){
  4. if(!this.isPopulated){
  5. let recipeList = document.querySelector("#recipeList");
  6. for(let recipe of merchant.recipes){
  7. let recipeDiv = document.createElement("div");
  8. recipeDiv.classList = "recipeItem";
  9. recipeDiv.onclick = ()=>{this.displayRecipe(recipe)};
  10. recipeList.appendChild(recipeDiv);
  11. let recipeName = document.createElement("p");
  12. recipeName.innerText = recipe.name;
  13. recipeDiv.appendChild(recipeName);
  14. let recipePrice = document.createElement("p");
  15. recipePrice.innerText = `$${(recipe.price / 100).toFixed(2)}`;
  16. recipeDiv.appendChild(recipePrice);
  17. }
  18. let ingredientsSelect = document.querySelector("#recipeInputIngredients select");
  19. let categories = categorizeIngredients();
  20. for(let category of categories){
  21. let optgroup = document.createElement("optgroup");
  22. optgroup.label = category.name;
  23. ingredientsSelect.appendChild(optgroup);
  24. for(let ingredient of category.ingredients){
  25. let option = document.createElement("option");
  26. option.value = ingredient.id;
  27. option.innerText = ingredient.name;
  28. optgroup.appendChild(option);
  29. }
  30. }
  31. this.isPopulated = true;
  32. }
  33. },
  34. displayRecipe: function(recipe){
  35. closeSidebar();
  36. document.querySelector("#recipeDetails").classList = "sidebar";
  37. document.querySelector("#recipeDetails h1").innerText = recipe.name;
  38. let ingredientList = document.querySelector("#recipeIngredients");
  39. while(ingredientList.children.length > 0){
  40. ingredientList.removeChild(ingredientList.firstChild);
  41. }
  42. for(let ingredient of recipe.ingredients){
  43. let ingredientDiv = document.createElement("div");
  44. ingredientDiv.classList = "recipeIngredient";
  45. ingredientList.appendChild(ingredientDiv);
  46. let ingredientName = document.createElement("p");
  47. ingredientName.innerText = ingredient.ingredient.name;
  48. ingredientDiv.appendChild(ingredientName);
  49. let ingredientQuantity = document.createElement("p");
  50. ingredientQuantity.innerText = `${ingredient.quantity} ${ingredient.ingredient.unit}`;
  51. ingredientDiv.appendChild(ingredientQuantity);
  52. }
  53. document.querySelector("#recipePrice p").innerText = `$${(recipe.price / 100).toFixed(2)}`;
  54. },
  55. displayAddRecipe: function(){
  56. closeSidebar();
  57. document.querySelector("#addRecipe").classList = "sidebar";
  58. },
  59. changeRecipeCount: function(){
  60. let newCount = document.querySelector("#ingredientCount").value;
  61. let ingredientsDiv = document.querySelector("#recipeInputIngredients");
  62. let oldCount = ingredientsDiv.children.length;
  63. if(newCount > oldCount){
  64. let newDivs = newCount - oldCount;
  65. for(let i = 0; i < newDivs; i++){
  66. let newNode = ingredientsDiv.children[0].cloneNode(true);
  67. newNode.children[2].children[0].value = "";
  68. ingredientsDiv.appendChild(newNode);
  69. }
  70. for(let i = 0; i < newCount; i++){
  71. ingredientsDiv.children[i].children[0].innerText = `Ingredient ${i + 1}`;
  72. }
  73. }else if(newCount < oldCount){
  74. let newDivs = oldCount - newCount;
  75. for(let i = 0; i < newDivs; i++){
  76. ingredientsDiv.removeChild(ingredientsDiv.children[ingredientsDiv.children.length-1]);
  77. }
  78. }
  79. },
  80. submitNewRecipe: function(){
  81. let newRecipe = {
  82. name: document.querySelector("#newRecipeName").value,
  83. price: document.querySelector("#newRecipePrice").value,
  84. ingredients: []
  85. }
  86. let inputs = document.querySelectorAll("#recipeInputIngredients > div");
  87. for(let input of inputs){
  88. newRecipe.ingredients.push({
  89. ingredient: input.children[1].children[0].value,
  90. quantity: input.children[2].children[0].value
  91. });
  92. }
  93. if(!validator.recipe(newRecipe)){
  94. return;
  95. }
  96. fetch("/recipe/create", {
  97. method: "POST",
  98. headers: {
  99. "Content-Type": "application/json;charset=utf-8"
  100. },
  101. body: JSON.stringify(newRecipe)
  102. })
  103. .then((response)=>{
  104. console.log(response.data)
  105. if(typeof(response.data) === "string"){
  106. banner.createError(response.data);
  107. }else{
  108. banner.createNotification("New recipe successfully created")
  109. }
  110. })
  111. .catch((err)=>{
  112. banner.createError("Refresh page to update data");
  113. });
  114. }
  115. }