-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspa.js
243 lines (190 loc) · 6.26 KB
/
spa.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
const API_BASE_URL = 'https://api.themoviedb.org/3';
const api_key = '05b8df1631c15f6dd003660574cc1e13';
const getPopularMovieAndTv = async () => {
const resp = await fetch(`${API_BASE_URL}/trending/movie,tv/day?api_key=${api_key}&language=en-US&page=1`)
const json = await resp.json();
return json;
}
const getMovieTv = async ({ id, type }) => {
if (!getMovieTv.answers) {
getMovieTv.answers = {};
}
const key = JSON.stringify({ id, type })
if (getMovieTv.answers[key] !== undefined) {
return getMovieTv.answers[key];
}
const resp = await fetch(`${API_BASE_URL}/${type}/${id}?api_key=${api_key}&language=en-US`)
const json = await resp.json();
return getMovieTv.answers[key] = json;
}
const getMovieRecommendations = async ({ id, type }) => {
if (!getMovieRecommendations.answers) {
getMovieRecommendations.answers = {};
}
const key = JSON.stringify({ id, type })
if (getMovieRecommendations.answers[key] !== undefined) {
return getMovieRecommendations.answers[key];
}
const resp = await fetch(`${API_BASE_URL}/${type}/${id}/recommendations?api_key=${api_key}&language=en-US&page=1`)
const json = await resp.json();
return getMovieRecommendations.answers[key] = json;
}
const searchByTitle = async (title) => {
if (!searchByTitle.answers) {
searchByTitle.answers = {};
}
if (searchByTitle.answers[title] !== undefined) {
return searchByTitle.answers[title];
}
const resp = await fetch(`${API_BASE_URL}/search/multi?api_key=${api_key}&language=en-US&query=${title}&page=1&include_adult=false`)
const json = await resp.json();
return searchByTitle.answers[title] = json;
}
const createHyperlink = ({ text, href, id, type }) => {
const a = document.createElement('a')
a.href = href;
a.appendChild(document.createTextNode(text))
a.onclick = (e) => {
e.preventDefault()
window.location.hash = `${type}=${id}`
}
return a;
}
const appendToBody = (node) => {
const body = document.body;
body.appendChild(node)
}
const createMovieList = (array, ulId) => {
const list = array.map(x => {
return {
id: x.id,
title: x.title || x.name,
type: x.title ? 'movie' : 'tv'
}
})
const ul = document.createElement('ul');
ul.id = ulId;
list.forEach(i => {
const li = document.createElement('li')
li.appendChild(createHyperlink({
text: `${i.title}`,
href: `movie${i.id}`,
id: i.id,
type: i.type,
}))
ul.appendChild(li)
})
return ul;
}
const MOVIE_FRAGMENT = 'movie_fragment'
const TRENDING_LIST = 'trending_list';
const FOUND_MOVIES_LIST = 'found_movies_list';
const SEARCH_FORM = 'search_form'
const clear = () => {
removeById(FOUND_MOVIES_LIST);
removeById(MOVIE_FRAGMENT);
removeById(TRENDING_LIST);
}
const removeById = (id) => {
try {
const oldPage = document.querySelector(`#${id}`);
oldPage.parentNode.removeChild(oldPage);
} catch (e) {
//console.log(e)
}
}
const renderSearch = () => {
const form = document.createElement('form');
form.id = `${SEARCH_FORM}`
form.innerHTML = `<input type='text' name='movie'>`
form.innerHTML += `<input type='submit' value='Search'>`
form.onsubmit = onSearchFormSubmit;
appendToBody(form)
return form;
}
const onSearchFormSubmit = (form) => {
form.preventDefault();
const thingsToSearch = form.target.movie.value;
if (thingsToSearch === '') {
window.location.hash = ''
return;
}
window.location.hash = `search=${thingsToSearch}`
form.target.movie.value = ''
}
const getMoviePage = ({ poster_path, title, name, overview, id }, type) => {
const page = document.createElement('div');
page.id = `${MOVIE_FRAGMENT}`
page.innerHTML += `<img src='https://image.tmdb.org/t/p/w342${poster_path}'/>`
page.innerHTML += `<h1>${title || name}</h1>`
page.innerHTML += `<span>${overview} </span>`
page.innerHTML += `<h2> Recommendations: </h2>`
const recommendationList = document.createElement('span')
recommendationList.innerHTML = 'Loading...'
page.appendChild(recommendationList)
getMovieRecommendations({ id, type })
.then(x => {
const list = x.results.slice(0, 3)
if (list.length === 0) {
recommendationList.innerHTML = 'Sorry, there are no recommendations';
} else {
const movList = createMovieList(list)
recommendationList.parentNode.replaceChild(movList, recommendationList);
}
})
return page;
}
const getFoundMovies = (title) => {
const foundMovieList = document.createElement('div')
foundMovieList.id = FOUND_MOVIES_LIST;
const info = document.createElement('span')
info.innerHTML = 'Loading...'
foundMovieList.appendChild(info)
searchByTitle(title)
.then(resp => resp.results.filter(x => x.media_type === 'movie' || x.media_type === 'tv'))
.then(arr => {
if (arr.length === 0) {
info.innerHTML = 'Sorry, nothing found';
} else {
info.remove();
const movList = createMovieList(arr)
foundMovieList.appendChild(movList);
}
})
return foundMovieList;
}
const renderTrends = () => {
getPopularMovieAndTv()
.then(resp => createMovieList(resp.results, TRENDING_LIST))
.then(resp => appendToBody(resp))
}
const hashchangeHandler = () => {
clear();
const hash = location.hash
if (hash === '') {
renderTrends();
return;
}
const regex = /(tv|movie|search)=(\d+|\w+)$/
const matched = hash.match(regex)
const type = matched[1]
const id = matched[2]
if (type === 'tv' || type === 'movie') {
getMovieTv({ type, id })
.then(resp => {
appendToBody(getMoviePage(resp, type))
return;
})
}
if (type === 'search') {
appendToBody(getFoundMovies(id))
}
}
const spa = () => {
window.addEventListener('hashchange', hashchangeHandler);
renderSearch()
hashchangeHandler()
}
window.onload = () => {
spa();
}