forked from fabioaraujoqa/OMDB-Movies-tests
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathscript.js
119 lines (104 loc) · 4.39 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
119
const API_KEY = 'f7f22d30';
const localURL = 'http://localhost:3000/cadastro';
const productionURL = 'https://8i5962r1e8.execute-api.us-east-1.amazonaws.com/cadastro';
const API_URL = window.location.hostname === 'localhost' ? localURL : productionURL;
document.addEventListener('DOMContentLoaded', fetchRecommendations);
document.getElementById('search-button').addEventListener('click', searchMovies);
document.getElementById('clear-button').addEventListener('click', clearSearchResults);
document.getElementById('signup-button').addEventListener('click', registerUser);
function fetchRecommendations() {
const recommendations = ['The Matrix', 'Inception', 'Interstellar', 'The Godfather', 'Jurassic Park'];
const recommendationsElement = document.getElementById('recommendations');
recommendations.forEach(title => {
const url = `https://www.omdbapi.com/?t=${encodeURIComponent(title)}&apikey=${API_KEY}`;
fetch(url)
.then(response => response.json())
.then(data => {
const movieElement = document.createElement('div');
movieElement.innerHTML = `<img src="${data.Poster}" alt="${data.Title}">
<p>${data.Title}</p>`;
recommendationsElement.appendChild(movieElement);
})
.catch(error => console.error('Erro ao buscar recomendações:', error));
});
}
function searchMovies() {
const searchTerm = document.getElementById('search-input').value;
if (!searchTerm) {
alert('Por favor, digite o nome de um filme');
return;
}
const url = `https://www.omdbapi.com/?s=${encodeURIComponent(searchTerm)}&apikey=${API_KEY}`;
fetch(url)
.then(response => response.json())
.then(data => displaySearchResults(data))
.catch(error => console.error('Erro ao buscar filmes:', error));
}
function displaySearchResults(data) {
const resultsSection = document.getElementById('results-section');
if (data.Response === 'False') {
resultsSection.innerHTML = '<p>Filme não encontrado.</p>';
return;
}
const resultsMarkup = data.Search.map(movie => `<div>
<h3>${movie.Title}</h3>
<p>${movie.Year}</p>
<img src="${movie.Poster}" alt="Poster">
</div>`).join('');
resultsSection.innerHTML = resultsMarkup;
}
function clearSearchResults() {
document.getElementById('results-section').innerHTML = '';
document.getElementById('search-input').value = '';
}
function registerUser(event) {
event.preventDefault();
const formData = {
nome: document.getElementById('signup-firstname').value,
sobrenome: document.getElementById('signup-lastname').value,
email: document.getElementById('signup-email').value,
telefone: document.getElementById('signup-phone').value,
senha: document.getElementById('signup-password').value
};
const responseDiv = document.getElementById('signup-response');
fetch(API_URL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(formData)
})
.then(response => {
if (response.ok) {
return response.json();
} else {
// Tratamento de erro HTTP
return response.text().then(text => {
throw new Error(text || 'Erro desconhecido');
});
}
})
.then(data => {
// Sucesso
responseDiv.textContent = data.message;
})
.catch(error => {
// Erro de rede ou resposta de erro não-JSON
console.error('Erro ao cadastrar:', error);
responseDiv.textContent = error.message || 'Falha: ocorreu um erro, tente novamente';
});
}
function validateForm(formData, responseDiv) {
const { nome, sobrenome, email, senha } = formData;
if (!nome || !sobrenome || !email || !senha) {
responseDiv.textContent = 'Por favor, preencha todos os campos obrigatórios.';
return false;
}
if (!validateEmail(email)) {
responseDiv.textContent = 'O formato do email é inválido.';
return false;
}
return true;
}
function validateEmail(email) {
const re = /\S+@\S+\.\S+/;
return re.test(email);
}