[miniProjects] 17_Movie App

๋ณด๋ฆฌยท2023๋…„ 6์›” 16์ผ
0

miniProjects

๋ชฉ๋ก ๋ณด๊ธฐ
18/47

17_Movie App

๐Ÿ’ป ์ฃผ์ œ : API๋ฅผ ํ™œ์šฉํ•œ ์˜ํ™” ์†Œ๊ฐœ ํŽ˜์ด์ง€.

  • TMDB์˜ API๋ฅผ ์‚ฌ์šฉํ•ด ์˜ํ™” ์ œ๋ชฉ, ๋“ฑ๊ธ‰, ์ด๋ฏธ์ง€๊ฐ€ ์žˆ๋Š” ์˜ํ™” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ๋ฐ›๋Š”๋‹ค.

    โ— https://www.themoviedb.org/settings/api
  • UI(HTML, CSS)๋ฅผ ๋งŒ๋“ค๊ณ , API ํ‚ค๋ฅผ IMDB ์„œ๋น„์Šค์— ๋“ฑ๋กํ•œ๋‹ค.
  • JavaScript๋ฅผ ์ถ”๊ฐ€ํ•ด Fetch ์š”์ฒญ์„ ๋งŒ๋“ค์–ด ๋‹น์‹œ ๊ฐ€์žฅ ์ธ๊ธฐ ์žˆ์—ˆ๋˜ ์˜ํ™”๋ฅผ ๋ณด์—ฌ์ค„ ๋ฐ์ดํ„ฐ๋ฅผ ์–ป๋Š”๋‹ค.
  • 8 ์ด์ƒ : ์ดˆ๋ก | 5 ~ 8 : ์ฃผํ™ฉ | 5 ์ดํ•˜ : ๋นจ๊ฐ•
  • ๊ฒ€์ƒ‰๊ธฐ๋Šฅ์„ ํ†ตํ•ด ์ œ๋ชฉ ๊ฒ€์ƒ‰ ๊ฐ€๋Šฅ


const API_URL = 'https://api.themoviedb.org/3/discover/movie?sort_by=popularity.desc&api_key=ae312a16848ed5ab44faf6aeb5640fa5&page=1';
const IMG_PATH = 'https://image.tmdb.org/t/p/w500';
const SEARCH_API = 'https://api.themoviedb.org/3/search/movie?api_key=ae312a16848ed5ab44faf6aeb5640fa5&query="';

const main = document.getElementById('main');
const form = document.getElementById('form');
const search = document.getElementById('search');

// Get initial movies
getMovies(API_URL);

async function getMovies(url) {
    const res = await fetch(url);
    const data = await res.json();

    showMovies(data.results);
}

function showMovies(movies) {
    main.innerHTML = ''

    movies.forEach((movie) => {
        const { title, poster_path, vote_average, overview } = movie;

        const movieEl = document.createElement('div');
        movieEl.classList.add('movie');

        movieEl.innerHTML = `
            <img src="${IMG_PATH + poster_path}" alt="${title}">
            <div class="movie-info">
          <h3>${title}</h3>
          <span class="${getClassByRate(vote_average)}">${vote_average}</span>
            </div>
            <div class="overview">
          <h3>Overview</h3>
          ${overview}
        </div>
        `
        main.appendChild(movieEl);
    })
}

function getClassByRate(vote) {
    if(vote >= 8) {
        return 'green';
    } else if(vote >= 5) {
        return 'orange';
    } else {
        return 'red';
    }
}

form.addEventListener('submit', (e) => {
    e.preventDefault()

    const searchTerm = search.value

    if(searchTerm && searchTerm !== '') {
        getMovies(SEARCH_API + searchTerm)

        search.value = '';
    } else {
        window.location.reload();
    }
})
profile
์ •์‹ ์ฐจ๋ ค ์ด ๊ฐ๋ฐ•ํ•œ ์„ธ์ƒ์†์—์„œ

0๊ฐœ์˜ ๋Œ“๊ธ€