recipeBook.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. openSidebar(document.querySelector("#recipeDetails"));
  36. document.querySelector("#recipeDetails h1").innerText = recipe.name;
  37. let ingredientList = document.querySelector("#recipeIngredients");
  38. while(ingredientList.children.length > 0){
  39. ingredientList.removeChild(ingredientList.firstChild);
  40. }
  41. for(let ingredient of recipe.ingredients){
  42. let ingredientDiv = document.createElement("div");
  43. ingredientDiv.classList = "recipeIngredient";
  44. ingredientList.appendChild(ingredientDiv);
  45. let ingredientName = document.createElement("p");
  46. ingredientName.innerText = ingredient.ingredient.name;
  47. ingredientDiv.appendChild(ingredientName);
  48. let ingredientQuantity = document.createElement("p");
  49. ingredientQuantity.innerText = `${ingredient.quantity} ${ingredient.ingredient.unit}`;
  50. ingredientDiv.appendChild(ingredientQuantity);
  51. }
  52. document.querySelector("#recipePrice p").innerText = `$${(recipe.price / 100).toFixed(2)}`;
  53. },
  54. displayAddRecipe: function(){
  55. openSidebar(document.querySelector("#addRecipe"));
  56. },
  57. changeRecipeCount: function(){
  58. let newCount = document.querySelector("#ingredientCount").value;
  59. let ingredientsDiv = document.querySelector("#recipeInputIngredients");
  60. let oldCount = ingredientsDiv.children.length;
  61. if(newCount > oldCount){
  62. let newDivs = newCount - oldCount;
  63. for(let i = 0; i < newDivs; i++){
  64. let newNode = ingredientsDiv.children[0].cloneNode(true);
  65. newNode.children[2].children[0].value = "";
  66. ingredientsDiv.appendChild(newNode);
  67. }
  68. for(let i = 0; i < newCount; i++){
  69. ingredientsDiv.children[i].children[0].innerText = `Ingredient ${i + 1}`;
  70. }
  71. }else if(newCount < oldCount){
  72. let newDivs = oldCount - newCount;
  73. for(let i = 0; i < newDivs; i++){
  74. ingredientsDiv.removeChild(ingredientsDiv.children[ingredientsDiv.children.length-1]);
  75. }
  76. }
  77. },
  78. submitNewRecipe: function(){
  79. let newRecipe = {
  80. name: document.querySelector("#newRecipeName").value,
  81. price: document.querySelector("#newRecipePrice").value,
  82. ingredients: []
  83. }
  84. let inputs = document.querySelectorAll("#recipeInputIngredients > div");
  85. for(let input of inputs){
  86. newRecipe.ingredients.push({
  87. ingredient: input.children[1].children[0].value,
  88. quantity: input.children[2].children[0].value
  89. });
  90. }
  91. if(!validator.recipe(newRecipe)){
  92. return;
  93. }
  94. fetch("/recipe/create", {
  95. method: "POST",
  96. headers: {
  97. "Content-Type": "application/json;charset=utf-8"
  98. },
  99. body: JSON.stringify(newRecipe)
  100. })
  101. .then((response)=>{
  102. if(typeof(response.data) === "string"){
  103. banner.createError(response.data);
  104. }else{
  105. banner.createNotification("New recipe successfully created")
  106. }
  107. })
  108. .catch((err)=>{
  109. banner.createError("Refresh page to update data");
  110. });
  111. }
  112. }