recipeBook.js 4.9 KB

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