recipeBook.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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(merchant.inventory);
  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. //Updates the number of ingredient inputs displayed for new recipes
  61. changeRecipeCount: function(){
  62. let newCount = document.querySelector("#ingredientCount").value;
  63. let ingredientsDiv = document.querySelector("#recipeInputIngredients");
  64. let oldCount = ingredientsDiv.children.length;
  65. if(newCount > oldCount){
  66. let newDivs = newCount - oldCount;
  67. for(let i = 0; i < newDivs; i++){
  68. let newNode = ingredientsDiv.children[0].cloneNode(true);
  69. newNode.children[2].children[0].value = "";
  70. ingredientsDiv.appendChild(newNode);
  71. }
  72. for(let i = 0; i < newCount; i++){
  73. ingredientsDiv.children[i].children[0].innerText = `Ingredient ${i + 1}`;
  74. }
  75. }else if(newCount < oldCount){
  76. let newDivs = oldCount - newCount;
  77. for(let i = 0; i < newDivs; i++){
  78. ingredientsDiv.removeChild(ingredientsDiv.children[ingredientsDiv.children.length-1]);
  79. }
  80. }
  81. },
  82. submitNewRecipe: function(){
  83. let newRecipe = {
  84. name: document.querySelector("#newRecipeName").value,
  85. price: document.querySelector("#newRecipePrice").value,
  86. ingredients: []
  87. }
  88. let inputs = document.querySelectorAll("#recipeInputIngredients > div");
  89. for(let input of inputs){
  90. newRecipe.ingredients.push({
  91. ingredient: input.children[1].children[0].value,
  92. quantity: input.children[2].children[0].value
  93. });
  94. }
  95. if(!validator.recipe(newRecipe)){
  96. return;
  97. }
  98. fetch("/recipe/create", {
  99. method: "POST",
  100. headers: {
  101. "Content-Type": "application/json;charset=utf-8"
  102. },
  103. body: JSON.stringify(newRecipe)
  104. })
  105. .then((response)=>{
  106. if(typeof(response.data) === "string"){
  107. banner.createError(response.data);
  108. }else{
  109. banner.createNotification("New recipe successfully created")
  110. }
  111. })
  112. .catch((err)=>{
  113. banner.createError("Refresh page to update data");
  114. });
  115. }
  116. }