recipes.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. while(body.children.length > 0){
  14. body.removeChild(body.firstChild);
  15. }
  16. for(let recipe of merchant.recipes){
  17. let recipeDiv = document.createElement("div");
  18. recipeDiv.classList = "recipe-card";
  19. recipeDiv.onclick = ()=>{singleRecipeObj.display(recipe)};
  20. body.appendChild(recipeDiv);
  21. if(recipe.ingredients.length === 0){
  22. recipeDiv.classList = "recipe-card empty-recipe";
  23. }else{
  24. recipeDiv.classList = "recipe-card";
  25. }
  26. let title = document.createElement("h2");
  27. title.innerText = recipe.name;
  28. recipeDiv.appendChild(title);
  29. let ingredientList = document.createElement("ul");
  30. recipeDiv.appendChild(ingredientList);
  31. for(let ingredient of recipe.ingredients){
  32. let ul = document.createElement("li");
  33. ul.innerText = ingredient.ingredient.name;
  34. ingredientList.appendChild(ul);
  35. }
  36. }
  37. },
  38. updateRecipes: function(){
  39. axios.get("/merchant/recipes/update")
  40. .then((result)=>{
  41. if(typeof(result.data) === "string"){
  42. banner.createError(result.data);
  43. }else{
  44. merchant = result.data.merchant;
  45. this.populateRecipes();
  46. banner.createNotification("Your recipes have been updated successfully");
  47. if(result.data.count > 0){
  48. banner.createError(`You have ${result.data.count} recipes with no ingredients. Please update them.`);
  49. }
  50. }
  51. })
  52. .catch((err)=>{
  53. banner.createError("There was an error and your recipes could not be updated");
  54. });
  55. }
  56. }