Skip to content

Commit b5a5239

Browse files
Merge pull request #69 from Srijan1878/movie-website
Added the Movie-Website
2 parents 2040a19 + 1ecb947 commit b5a5239

File tree

7 files changed

+497
-0
lines changed

7 files changed

+497
-0
lines changed

Movie-Website/Icon.ico

104 KB
Binary file not shown.

Movie-Website/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Movie App
2+
Built with Vanilla JavaScript. Below is a list of the methods and properties use
3+
4+
* querySelector
5+
* createElement
6+
* innerHTML
7+
* appendChild
8+
* insertBefore
9+
* preventDefault
10+
* dataset
11+
* parentElement
12+
* nextElementSibling
13+
* classList
14+
* add
15+
* remove
16+
* fetch
17+
* bind
18+
* setAttribute
19+
* onClick
20+
* toLowerCase
21+
* target
22+
23+
24+
### Video coming soon!!!

Movie-Website/apiTransaction.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Initial Values
2+
const MOVIE_DB_API = '6bf3b3542f9ff12290736047042a751d';
3+
const MOVIE_DB_ENDPOINT = 'https://api.themoviedb.org';
4+
const MOVIE_DB_IMAGE_ENDPOINT = 'https://image.tmdb.org/t/p/w500';
5+
const DEFAULT_POST_IMAGE = 'https://via.placeholder.com/150';
6+
7+
function requestMovies(url, onComplete, onError) {
8+
fetch(url)
9+
.then((res) => res.json())
10+
.then(onComplete)
11+
.catch(onError);
12+
}
13+
14+
function generateMovieDBUrl(path) {
15+
const url = `${MOVIE_DB_ENDPOINT}/3${path}?api_key=${MOVIE_DB_API}`;
16+
return url;
17+
}
18+
19+
20+
function getTopRatedMovies() {
21+
const url = generateMovieDBUrl(`/movie/top_rated`);
22+
const render = renderMovies.bind({ title: 'Top Rated Movies' })
23+
requestMovies(url, render, handleGeneralError);
24+
}
25+
26+
function getTrendingMovies() {
27+
const url = generateMovieDBUrl('/trending/movie/day');
28+
const render = renderMovies.bind({ title: 'Trending Movies' })
29+
requestMovies(url, render, handleGeneralError);
30+
}
31+
32+
33+
function searchUpcomingMovies() {
34+
const url = generateMovieDBUrl('/movie/upcoming');
35+
const render = renderMovies.bind({ title: 'Upcoming Movies' })
36+
requestMovies(url, render, handleGeneralError);
37+
}
38+
39+
function searchPopularMovie() {
40+
const url = generateMovieDBUrl('/movie/popular');
41+
const render = renderMovies.bind({ title: 'Popular Movies' });
42+
requestMovies(url, render, handleGeneralError);
43+
}
44+
45+
// Invoke a different function for search movies
46+
function searchMovie(value) {
47+
const url = generateMovieDBUrl('/search/movie') + '&query=' + value;
48+
requestMovies(url, renderSearchMovies, handleGeneralError);
49+
}
50+
51+
52+
function getVideosByMovieId(movieId, content) {
53+
const url = generateMovieDBUrl(`/movie/${movieId}/videos`);
54+
const render = createVideoTemplate.bind({ content });
55+
requestMovies(url, render, handleGeneralError);
56+
}

Movie-Website/app.js

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
// Initial Values
2+
const INITIAL_SEARCH_VALUE = 'spiderman';
3+
const log = console.log;
4+
5+
// Selecting elements from the DOM
6+
const searchButton = document.querySelector('#search');;
7+
const searchInput = document.querySelector('#exampleInputEmail1');
8+
const moviesContainer = document.querySelector('#movies-container');
9+
const moviesSearchable = document.querySelector('#movies-searchable');
10+
11+
function createImageContainer(imageUrl, id) {
12+
const tempDiv = document.createElement('div');
13+
tempDiv.setAttribute('class', 'imageContainer');
14+
tempDiv.setAttribute('data-id', id);
15+
16+
const movieElement = `
17+
<img src="${imageUrl}" alt="" data-movie-id="${id}">
18+
`;
19+
tempDiv.innerHTML = movieElement;
20+
21+
return tempDiv;
22+
}
23+
24+
function resetInput() {
25+
searchInput.value = '';
26+
}
27+
28+
function handleGeneralError(error) {
29+
log('Error: ', error.message);
30+
alert(error.message || 'Internal Server');
31+
}
32+
33+
function createIframe(video) {
34+
const videoKey = (video && video.key) || 'No key found!!!';
35+
const iframe = document.createElement('iframe');
36+
iframe.src = `https://www.youtube.com/embed/${videoKey}`;
37+
iframe.width = 360;
38+
iframe.height = 315;
39+
iframe.allowFullscreen = true;
40+
return iframe;
41+
}
42+
43+
function insertIframeIntoContent(video, content) {
44+
const videoContent = document.createElement('div');
45+
const iframe = createIframe(video);
46+
47+
videoContent.appendChild(iframe);
48+
content.appendChild(videoContent);
49+
}
50+
51+
52+
function createVideoTemplate(data) {
53+
const content = this.content;
54+
content.innerHTML = '<p id="content-close">X</p>';
55+
56+
const videos = data.results || [];
57+
58+
if (videos.length === 0) {
59+
content.innerHTML = `
60+
<p id="content-close">X</p>
61+
<p>No Trailer found for this video id of ${data.id}</p>
62+
`;
63+
return;
64+
}
65+
66+
for (let i = 0; i < 4; i++) {
67+
const video = videos[i];
68+
insertIframeIntoContent(video, content);
69+
}
70+
}
71+
72+
function createSectionHeader(title) {
73+
const header = document.createElement('h2');
74+
header.innerHTML = title;
75+
76+
return header;
77+
}
78+
79+
80+
function renderMovies(data) {
81+
const moviesBlock = generateMoviesBlock(data);
82+
const header = createSectionHeader(this.title);
83+
moviesBlock.insertBefore(header, moviesBlock.firstChild);
84+
moviesContainer.appendChild(moviesBlock);
85+
}
86+
87+
88+
89+
function renderSearchMovies(data) {
90+
moviesSearchable.innerHTML = '';
91+
const moviesBlock = generateMoviesBlock(data);
92+
moviesSearchable.appendChild(moviesBlock);
93+
}
94+
95+
function generateMoviesBlock(data) {
96+
const movies = data.results;
97+
const section = document.createElement('section');
98+
section.setAttribute('class', 'section');
99+
100+
for (let i = 0; i < movies.length; i++) {
101+
const { poster_path, id } = movies[i];
102+
103+
if (poster_path) {
104+
const imageUrl = MOVIE_DB_IMAGE_ENDPOINT + poster_path;
105+
106+
const imageContainer = createImageContainer(imageUrl, id);
107+
section.appendChild(imageContainer);
108+
}
109+
}
110+
111+
const movieSectionAndContent = createMovieContainer(section);
112+
return movieSectionAndContent;
113+
}
114+
115+
116+
117+
// Inserting section before content element
118+
function createMovieContainer(section) {
119+
const movieElement = document.createElement('div');
120+
movieElement.setAttribute('class', 'movie');
121+
122+
const template = `
123+
<div class="content">
124+
<p id="content-close">X</p>
125+
</div>
126+
`;
127+
128+
movieElement.innerHTML = template;
129+
movieElement.insertBefore(section, movieElement.firstChild);
130+
return movieElement;
131+
}
132+
133+
searchButton.onclick = function (event) {
134+
event.preventDefault();
135+
const value = searchInput.value
136+
if (value) {
137+
searchMovie(value);
138+
}
139+
resetInput();
140+
}
141+
142+
// Click on any movies
143+
// Event Delegation
144+
document.onclick = function (event) {
145+
log('Event: ', event);
146+
const { tagName, id } = event.target;
147+
if (tagName.toLowerCase() === 'img') {
148+
const movieId = event.target.dataset.movieId;
149+
const section = event.target.parentElement.parentElement;
150+
const content = section.nextElementSibling;
151+
content.classList.add('content-display');
152+
getVideosByMovieId(movieId, content);
153+
}
154+
155+
if (id === 'content-close') {
156+
const content = event.target.parentElement;
157+
content.classList.remove('content-display');
158+
}
159+
}
160+
161+
// Initialize the search
162+
searchMovie(INITIAL_SEARCH_VALUE);
163+
searchUpcomingMovies();
164+
getTopRatedMovies();
165+
searchPopularMovie();
166+
getTrendingMovies();

Movie-Website/index.html

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
7+
<link rel="stylesheet" href="style.css" />
8+
<link rel="shortcut icon" href="Icon.ico" type="image/x-icon" />.
9+
<title>BingeNation</title>
10+
</head>
11+
<body>
12+
<div class="container">
13+
<div class="header">
14+
<img src="logo.png" class="poster" alt="logo">
15+
<h1>BingeNation</h1>
16+
</div>
17+
<form>
18+
<div class="form-group">
19+
<input
20+
type="text"
21+
class="form-control"
22+
id="exampleInputEmail1"
23+
aria-describedby="emailHelp"
24+
placeholder="Search a Movie.."
25+
/>
26+
</div>
27+
<button type="submit" class="btn" id="search">
28+
Search
29+
</button>
30+
</form>
31+
32+
<div id="movies-searchable"></div>
33+
<div id="movies-container"></div>
34+
</div>
35+
36+
<script src="apiTransaction.js"></script>
37+
<script src=" app.js"></script>
38+
</body>
39+
</html>

Movie-Website/logo.png

283 KB
Loading

0 commit comments

Comments
 (0)