recipes.js 2.3 KB

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