-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilms.js
99 lines (84 loc) · 2.78 KB
/
films.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
let params = new URLSearchParams(document.location.search);
let filmId = params.get("id");
let url = `https://swapi.dev/api/films/${filmId}`;
fetch(url)
.then((res) => res.json())
.then((data) => {
// console.log(data.results);
// for (let i = 0; i < data.length; i++) {
// console.log(data)
let film_Title = document.querySelector(".filmTitle")
film_Title.innerText = data['title']
let date = document.querySelector(".film-date")
date.innerText = data['release_date']
let epNumber = document.querySelector(".episode")
epNumber.innerText = data['episode_id']
// "Masking" is disastrous!
// for (let filmURL of data.title) {
// console.log("HELLO!!")
// let filmId = getIdFromUrl(filmURL)
// fetch(filmURL)
// .then((film) => film.json())
// console.log("GOODBYE!")
// .then((filmData) => {
// // console.log("GOODBYE!")
// console.log(filmData.title)
// let filmName = document.querySelector(".featuredFilms")
// let filmTag = document.createElement('a')
// filmTag.setAttribute('href', `films.html?id=${filmId}`)
// filmTag.innerText = filmData.title
// let liFilm = document.createElement('li')
// liFilm.appendChild(filmTag)
// filmName.appendChild(liFilm)
//
// })
//switch case
//if filmData.title="A New Hope" "The Empire Strikes Back" "Return of the Jedi" "The Phantom Menace" "Attack of the Clones" "Revenge of the Sith"
function displayImg(title) {
let src;
switch (title) {
case "A New Hope":
src = "new_hope.jpg";
break;
case "The Empire Strikes Back":
src = "empire-strikes.png";
break;
case "Return of the Jedi":
src = "return-jedi.png";
break;
case "The Phantom Menace":
src = "phantom-menace.png";
break;
case "Attack of the Clones":
src = "clone-attack.png";
break;
case "Revenge of the Sith":
src = "sith-revenge.png";
break;
default:
src = "SW_Uni.jpg";
}
return src;
}
let filmImage = document.querySelector(".film_image")
let SRC = displayImg(data.title)
console.log(data.title)
console.log(displayImg(data.title))
filmImage.src = `img/${SRC}`
console.log(filmImage.src)
})
// }
// });
function getPathArrayFromUrl(url) {
const parsedUrl = new URL(url);
return parsedUrl.pathname.split("/");
}
function getIdFromUrl(url) {
const pathParts = getPathArrayFromUrl(url).reverse();
for (let part of pathParts) {
const candidateId = Number.parseInt(part);
if (typeof candidateId === "number" && candidateId) {
return candidateId.toString();
}
}
}