recipes.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. let recipesObj = {
  2. isPopulated: false,
  3. display: function(){
  4. controller.clearScreen();
  5. controller.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. }