recipeBook.js 4.4 KB

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