recipes.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. window.recipesObj = {
  2. isPopulated: false,
  3. display: function(){
  4. clearScreen();
  5. document.querySelector("#recipesStrand").style.display = "flex";
  6. if(!this.isPopulated){
  7. this.populateRecipes();
  8. this.isPopulated = true;
  9. }
  10. },
  11. populateRecipes: function(){
  12. let body = document.querySelector("#recipesContainer");
  13. let blankRecipes = 0;
  14. while(body.children.length > 0){
  15. body.removeChild(body.firstChild);
  16. }
  17. merchant.recipes.sort((a, b) => (a.name > b.name)? 1 : -1);
  18. for(let recipe of merchant.recipes){
  19. let recipeDiv = document.createElement("div");
  20. recipeDiv.classList = "recipe-card";
  21. recipeDiv.onclick = ()=>{singleRecipeObj.display(recipe)};
  22. body.appendChild(recipeDiv);
  23. if(recipe.ingredients.length === 0){
  24. recipeDiv.classList = "recipe-card empty-recipe";
  25. blankRecipes++;
  26. }else{
  27. recipeDiv.classList = "recipe-card";
  28. }
  29. let title = document.createElement("h2");
  30. title.innerText = recipe.name;
  31. recipeDiv.appendChild(title);
  32. let ingredientList = document.createElement("ul");
  33. recipeDiv.appendChild(ingredientList);
  34. for(let ingredient of recipe.ingredients){
  35. let ul = document.createElement("li");
  36. ul.innerText = ingredient.ingredient.name;
  37. ingredientList.appendChild(ul);
  38. }
  39. }
  40. if(blankRecipes > 0){
  41. banner.createError(`You have ${blankRecipes} recipes without ingredients, please add ingredients to all recipes`);
  42. }
  43. },
  44. updateRecipes: function(){
  45. axios.get("/merchant/recipes/update")
  46. .then((result)=>{
  47. if(typeof(result.data) === "string"){
  48. banner.createError(result.data);
  49. }else{
  50. merchant = result.data;
  51. this.populateRecipes();
  52. banner.createNotification("Your recipes have been updated successfully");
  53. }
  54. })
  55. .catch((err)=>{
  56. banner.createError("There was an error and your recipes could not be updated");
  57. });
  58. },
  59. showInput: function(){
  60. document.querySelector("#newRecipe").style.display = "block";
  61. },
  62. cancelAdd: function(){
  63. document.querySelector("#newName").value = "";
  64. document.querySelector("#newPrice").value = 0;
  65. document.querySelector("#newRecipe").style.display = "none";
  66. },
  67. submitNew: function(){
  68. let inputDiv = document.querySelector("#newRecipe");
  69. let nameInput = document.querySelector("#newName");
  70. let priceInput = document.querySelector("#newPrice");
  71. let data = {
  72. name: nameInput.value,
  73. price: priceInput.value
  74. };
  75. nameInput.value = "";
  76. priceInput.value = 0;
  77. inputDiv.style.display = "none";
  78. axios.post("/recipe/create", data)
  79. .then((response)=>{
  80. if(typeof(response.data) === "string"){
  81. banner.createError(response.data);
  82. }else{
  83. merchant.recipes.push(response.data);
  84. this.populateRecipes();
  85. }
  86. })
  87. .catch((err)=>{
  88. banner.createError("Error: Unable to display new data");
  89. });
  90. }
  91. }