Giter Site home page Giter Site logo

jacobkosmart / 28.may.21_randomrecipe_vanilla-js Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 144 KB

Random recipe with vanilla JS, fetch data from mealDB API

Home Page: https://recipe.jacobko.info/

JavaScript 58.39% HTML 10.11% CSS 31.50%
vanilla-javascript mealdb-api

28.may.21_randomrecipe_vanilla-js's Introduction

🍚 Recipe App

Demo

πŸ’» 1.ν”„λ‘œμ νŠΈ μ†Œκ°œ

πŸ“ μ‚¬μš©κΈ°μˆ  및 μ–Έμ–΄

  • Vanilla JS
  • CSS
  • HTML

⏰ 개발 κΈ°κ°„

2021-05-19 ~ 2021-05-28

πŸ—’ 2.ν”„λ‘œμ νŠΈ λ‚΄μš©

μ£Όμš” κΈ°λŠ₯

  • mobile frame 에 맞게 app μ œμž‘

  • The maal DB 의 random recipe 좜λ ₯

  • μ’‹μ•„μš” λ²„νŠΌμ„ λˆ„λ₯΄λ©΄ favorite meals 에 μΆ”κ°€

  • recipe 클릭 μ‹œ, 상세 정보 좜λ ₯ (youtube, ingredients, details)

  • 검색창에 (예: rice) μž…λ ₯ μ‹œ, ν•΄λ‹Ήλ˜λŠ” recipe 검색됨

πŸ“Œ 3.μ£Όμš” μ½”λ“œ

1. API μ—°κ²°

  • 비동기 (async, await 을 ν†΅ν•΄μ„œ getRandomMeal, getMealById, getMEalBySerch) λ₯Ό retun 함
async function getRandomMeal() {
  const resp = await fetch("https://www.themealdb.com/api/json/v1/1/random.php");
  const respData = await resp.json();
  const randomMeal = respData.meals[0];

  console.log(randomMeal)

  addMeal(randomMeal, true);
}

async function getMealById(id) {
  const resp = await fetch("https://www.themealdb.com/api/json/v1/1/lookup.php?i=" + id);

  const respData = await resp.json();

  const meal = respData.meals[0];

  return meal
}

async function getMealBySearch(term) {
  const resp = await fetch("https://www.themealdb.com/api/json/v1/1/search.php?s=" + term);

  const respData = await resp.json();
  const meals = respData.meals;

  return meals;
}

2. favorite Meals

  • like λ²„νŠΌμ„ λˆ„λ₯΄λ©΄ favorite 창에 메뉴 μΆ”κ°€ ν•˜κΈ°
function addMealFav(mealData) {

  const favMeal = document.createElement("li");

  favMeal.innerHTML = `
    <img src="${mealData.strMealThumb}" alt="${mealData.strMeal}">
    <span>${mealData.strMeal}</span>
    <button class= "clear"><i class="fas fa-window-close"></i></button>
  `;

  const btn = favMeal.querySelector('.clear');

  btn.addEventListener('click', () => {
    removeMealLS(mealData.idMeal);

    fetchFavMeals();
  });

  favMeal.addEventListener('click', () => {
    showMealInfo(mealData);
  });

3. recipe information modal

  • modal 창으둜 youtube, ingredients, details 등을 API 둜 λΆ€ν„° κ°€μ Έμ™€μ„œ ν•΄λ‹Ή 정보λ₯Ό return

  • JS μ •κ·œμ‹μ„ ν†΅ν•΄μ„œ ν•΄λ‹Ή youtube URL μ—μ„œ ν•΄λ‹Ήλ˜λŠ” ID λΆ€λΆ„λ§Œ match()λ₯Ό μ‚¬μš©ν•˜μ—¬ return

function showMealInfo(mealData) {
  // clean it up

  mealInfoEl.innerHTML = '';

  // update the Meal info
  const mealEl = document.createElement('div');

  const ingredients = [];

  // get ingredients and measures
  for(let i=1; i<=20; i++) {
    if(mealData['strIngredient' + i]) {
      ingredients.push(`${mealData['strIngredient' + i]} - ${mealData['strMeasure' + i]}`)
    } else {
      break;
    }
  }

  // JS regular expressions (get ID)
  const youtubeEl = mealData.strYoutube
  const selectURL = youtubeEl.match(/(?<=\=).{1,}/g)

  mealEl.innerHTML = `
    <h1>${mealData.strMeal}</h1>
    <iframe class="video-wrap" width="100%" height="315" src="https://www.youtube.com/embed/${selectURL}" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
    <h3>Ingredients: </h3>
    <ul>
      ${ingredients.map(ing => `<li>${ing}</li>`).join('')}
    </ul>
    <p>${mealData.strInstructions}</p>
    <img src="${mealData.strMealThumb}" alt="">
  `

  mealInfoEl.appendChild(mealEl);

  // show the popup
  mealPopup.classList.remove('hidden')
}

4. λŠλ‚€μ 

  • 비동기 κ΄€λ ¨ μ™ΈλΆ€ API와 μ—°κ²° κ΄€λ ¨ν•΄μ„œ Json μ΄λΌλ˜μ§€, 비동기 κ΄€λ ¨ ν•™μŠ΅λŸ‰ κ³Ό μ—λŸ¬ μ½”λ“œκ°€ 뢀뢄이 λ§Žμ•„μ„œ 어렀움을 λŠλ‚Œ

  • ν•¨μˆ˜κ°€ 정리가 λ˜μ§€ μ•Šμ•„ λ‚˜μ€‘μ— clean code λ₯Ό 톡해 js 파일 refactoring ν•„μš”μ„± 있음

  • HTML μ½”λ“œλ₯Ό 주둜 JS μ—μ„œ innerHTML 을 ν†΅ν•΄μ„œ μž‘μ„± ν•˜λŠ” 것에 λŒ€ν•΄μ„œ μ΅μˆ™ν•˜μ§€κ°€ μ•Šμ•„ API μ—μ„œ dataλ₯Ό κ°€μ Έ 올 경우 많이 μ‚¬μš©λ˜κΈ° λ•Œλ¬Έμ—, 더 λ§Žμ€ μ—°μŠ΅μ΄ ν•„μš” ν•  κ±° κ°™μŒ

  • κ°œμ„  사항:

    • λΈŒλΌμš°μ € test μ‹œ, 아이폰, μ•„μ΄νŒ¨λ“œ ν™˜κ²½μ—μ„œ ꡬ동이 잘 μ•ˆλ˜λŠ”κ²ƒ 확인됨 (μ™œ μ•ˆλ˜λŠ”μ§€ μΆ”ν›„ κ°œμ„  μ˜ˆμ •)

    • λ‹€μŒ random 이미지 return μ‹œ runtime이 생각보닀 였래 κ±Έλ €μ„œ μ½”λ“œ μ΅œμ ν™”λ₯Ό ν†΅ν•œ ꡬ동 속도 ν–₯상 ν•„μš”

Reference

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.