Advertisement
AngelKejov

Untitled

Nov 22nd, 2022
718
-1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. async function getRecipes() {
  2.     const response = await fetch('http://localhost:3030/jsonstore/cookbook/recipes');
  3.     const recipes = await response.json();
  4.  
  5.     return Object.values(recipes);
  6. }
  7.  
  8. async function getRecipeById(id) {
  9.     const response = await fetch('http://localhost:3030/jsonstore/cookbook/details/' + id);
  10.     const recipe = await response.json();
  11.  
  12.     return recipe;
  13. }
  14. function createRecipePreview(recipe) {
  15.     const result = e('article', { className: 'preview', onClick: toggleCard },
  16.         e('div', { className: 'title' }, e('h2', {}, recipe.name)),
  17.         e('div', { className: 'small' }, e('img', { src: recipe.img })),
  18.     );
  19.  
  20.     return result;
  21.  
  22.     async function toggleCard() {
  23.         const fullRecipe = await getRecipeById(recipe._id);
  24.  
  25.         result.replaceWith(createRecipeCard(fullRecipe));
  26.     }
  27. }
  28.  
  29. function createRecipeCard(recipe) {
  30.     const result = e('article', {},
  31.         e('h2', {}, recipe.name),
  32.         e('div', { className: 'band' },
  33.             e('div', { className: 'thumb' }, e('img', { src: recipe.img })),
  34.             e('div', { className: 'ingredients' },
  35.                 e('h3', {}, 'Ingredients:'),
  36.                 e('ul', {}, recipe.ingredients.map(i => e('li', {}, i))),
  37.             )
  38.         ),
  39.         e('div', { className: 'description' },
  40.             e('h3', {}, 'Preparation:'),
  41.             recipe.steps.map(s => e('p', {}, s))
  42.         ),
  43.     );
  44.  
  45.     return result;
  46. }
  47.  
  48. window.addEventListener('load', async () => {
  49.     const main = document.querySelector('main');
  50.  
  51.     const recipes = await getRecipes();
  52.     const cards = recipes.map(createRecipePreview);
  53.  
  54.     main.innerHTML = '';
  55.     cards.forEach(c => main.appendChild(c));
  56. });
  57.  
  58. function e(type, attributes, ...content) {
  59.     const result = document.createElement(type);
  60.  
  61.     for (let [attr, value] of Object.entries(attributes || {})) {
  62.         if (attr.substring(0, 2) == 'on') {
  63.             result.addEventListener(attr.substring(2).toLocaleLowerCase(), value);
  64.         } else {
  65.             result[attr] = value;
  66.         }
  67.     }
  68.  
  69.     content = content.reduce((a, c) => a.concat(Array.isArray(c) ? c : [c]), []);
  70.  
  71.     content.forEach(e => {
  72.         if (typeof e == 'string' || typeof e == 'number') {
  73.             const node = document.createTextNode(e);
  74.             result.appendChild(node);
  75.         } else {
  76.             result.appendChild(e);
  77.         }
  78.     });
  79.  
  80.     return result;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement