-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
118 lines (110 loc) · 4.72 KB
/
script.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
window.onload = () => {
const addMovieButton = document.getElementById("addMovieButton")
const myMovies = document.getElementById("mymovies")
const addArea = document.getElementById("addArea")
const addForm = document.getElementById("addForm")
const searchBar = document.getElementById("searchBar")
const searchMoviesDiv = document.getElementById("searchMovies")
const movies = document.getElementById("movies")
const storedMoviesFromStorage = localStorage.getItem("movies")
if(storedMoviesFromStorage != null) {
const storedMoviesJSON = JSON.parse(storedMoviesFromStorage)
Object.keys(storedMoviesJSON).forEach(key => {
const movie = storedMoviesJSON[key]
if(movie.movieImage != null && movie.movieName != null) {
movies.innerHTML += `<div class="card">
<div style="text-align: center;">
<img src="https://image.tmdb.org/t/p/w300${movie.movieImage}" width="200">
</div>
<h3 class="title">
${movie.movieName}
</h3>
<p class="small-text">
Rating given: ${movie.movieRating}/10
</p>
<p class="small-text">
${movie.movieReview}
</p>
<div style="text-align: center;">
<button class="btn addMovie-btn" onclick="deleteMovie('${movie.movieName}')">Delete Movie</button>
</div>
</div>`
}
})
myMovies.style.marginTop = "7rem"
myMovies.style.height = "fit-content"
}
addMovieButton.addEventListener('click', () => {
myMovies.style.display = "none"
addArea.style.display = "flex"
})
addForm.addEventListener('submit', (e) => {
e.preventDefault()
const searchQuery = searchBar.value
fetch(`https://api.themoviedb.org/3/search/movie?api_key=c467ddab940734b55111748d4ef3b4da&query=${searchQuery}&page=1`, {method: 'GET'})
.then(async (response) => {
const responseJSON = await response.json()
const searchMovies = responseJSON.results
console.log(searchMovies)
searchMovies.forEach(movie => {
if(movie.poster_path != null && movie.title != null) {
searchMoviesDiv.innerHTML += `<div class="card">
<div style="text-align: center;">
<img src="https://image.tmdb.org/t/p/w300${movie.poster_path}" width="200">
</div>
<h3 class="title">
${movie.title}
</h3>
<div style="text-align: center;">
<button class="btn addMovie-btn" onclick="addMovie('${movie.title}', '${movie.poster_path}')">Add Movie</button>
</div>
</div>`
}
})
addArea.style.height = "fit-content"
addForm.style.marginTop = "7rem"
})
})
}
function addMovie(title, poster_path) {
const storedMovies = localStorage.getItem("movies")
const rating = prompt("Rate this movie out of 10")
const review = prompt("Give a review")
if(storedMovies == null) {
const intialJSON = {}
intialJSON["1"] = {movieName: title, movieImage: poster_path, movieRating: rating, movieReview: review}
localStorage.setItem("movies", JSON.stringify(intialJSON))
} else {
let giveAlert = false
const storedMoviesJSON = JSON.parse(storedMovies)
Object.keys(storedMoviesJSON).forEach(key => {
if(storedMoviesJSON[key].movieName == title) {
giveAlert = true
}
})
if(giveAlert == false) {
let biggestKey = 1
Object.keys(storedMoviesJSON).forEach(key => {
if(Number(key) > biggestKey) {
biggestKey = Number(key)
}
})
storedMoviesJSON[biggestKey+1] = {movieName: title, movieImage: poster_path, movieRating: rating, movieReview: review}
localStorage.setItem("movies", JSON.stringify(storedMoviesJSON))
} else {
alert("You have already logged that movie")
}
}
window.location.reload()
}
function deleteMovie(movieName) {
const storedMovies = localStorage.getItem("movies")
const storedMoviesJSON = JSON.parse(storedMovies)
Object.keys(storedMoviesJSON).forEach(key => {
if(storedMoviesJSON[key].movieName == movieName) {
delete storedMoviesJSON[key]
localStorage.setItem("movies", JSON.stringify(storedMoviesJSON))
window.location.reload()
}
})
}