georgiev955

4. Cookbook – Part 1

Oct 28th, 2023
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. window.addEventListener('load', solve);
  2.  
  3. async function solve() {
  4.     const url = 'http://localhost:3030/jsonstore/cookbook/recipes';
  5.  
  6.     try {
  7.         const response = await fetch(url);
  8.         if (response.status !== 200) {
  9.             console.error(response.status);
  10.             return;
  11.         }
  12.         const data = await response.json();
  13.  
  14.         const main = document.querySelector('main');
  15.         main.innerHTML = '';
  16.  
  17.         for (const recipeId in data) {
  18.             const recipe = data[recipeId];
  19.  
  20.             const article = document.createElement('article');
  21.             article.classList.add('preview');
  22.             article.id = recipeId;
  23.  
  24.             const divTitle = document.createElement('div');
  25.             divTitle.classList.add('title');
  26.             divTitle.innerHTML = `<h2>${recipe.name}</h2>`;
  27.  
  28.             const divImage = document.createElement('div');
  29.             divImage.classList.add('small');
  30.             divImage.innerHTML = `<img src="${recipe.img}">`;
  31.  
  32.             article.appendChild(divTitle);
  33.             article.appendChild(divImage);
  34.  
  35.             article.addEventListener('click', changeVisibility);
  36.  
  37.             main.appendChild(article);
  38.  
  39.             function changeVisibility() {
  40.                 const articleInfo = article.nextSibling;
  41.                 articleInfo.style.display = (articleInfo.style.display === 'block') ? 'none' : 'block';
  42.             }
  43.  
  44.             const infoUrl = `http://localhost:3030/jsonstore/cookbook/details/${recipeId}`;
  45.             const infoResponse = await fetch(infoUrl);
  46.  
  47.             if (infoResponse.status !== 200) {
  48.                 console.error(infoResponse.status);
  49.                 continue;
  50.             }
  51.  
  52.             const infoData = await infoResponse.json();
  53.  
  54.             const articleInfo = createArticleInfo(infoData);
  55.             main.insertBefore(articleInfo, article.nextSibling);
  56.         }
  57.     } catch (err) {
  58.         console.error(err);
  59.     }
  60. }
  61.  
  62. function createArticleInfo(infoData) {
  63.     const articleInfo = document.createElement('article');
  64.     articleInfo.style.display = 'none';
  65.  
  66.     const h2El = document.createElement('h2');
  67.     h2El.textContent = infoData.name;
  68.  
  69.     const divBand = document.createElement('div');
  70.     divBand.classList.add('band');
  71.  
  72.     const divThumb = document.createElement('div');
  73.     divThumb.classList.add('thumb');
  74.     divThumb.innerHTML = `<img src="${infoData.img}">`;
  75.  
  76.     const divIngred = document.createElement('div');
  77.     divIngred.classList.add('ingredients');
  78.     divIngred.innerHTML = '<h3>Ingredients:</h3>';
  79.  
  80.     const divDescr = document.createElement('div');
  81.     divDescr.classList.add('description');
  82.     divDescr.innerHTML = '<h3>Preparation:</h3>';
  83.  
  84.     const ingrdUl = document.createElement('ul');
  85.     infoData.ingredients.forEach(ingredient => {
  86.         const liEl = document.createElement('li');
  87.         liEl.textContent = ingredient;
  88.         ingrdUl.appendChild(liEl);
  89.     });
  90.  
  91.     infoData.steps.forEach(step => {
  92.         const pEl = document.createElement('p');
  93.         pEl.textContent = step;
  94.         divDescr.appendChild(pEl);
  95.     });
  96.  
  97.     articleInfo.appendChild(h2El);
  98.     articleInfo.appendChild(divBand);
  99.     divBand.appendChild(divThumb);
  100.     divBand.appendChild(divIngred);
  101.     divIngred.appendChild(ingrdUl);
  102.     articleInfo.appendChild(divDescr);
  103.  
  104.     return articleInfo;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment