-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
105 lines (93 loc) · 2.98 KB
/
index.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
const BASE_URL = "https://xp41-soundgarden-api.herokuapp.com";
const evento = document.querySelector("#eventos");
const modal = document.querySelector("#modal");
const botaoX = document.querySelector("#btn-x");
const botaoClose = document.querySelector("#btn-fechar");
const tituloModal = document.querySelector("#titulo-modal");
const tickets = document.querySelector("#tickets");
const form = document.querySelector("#form");
const email = document.querySelector("#email");
const ingresso = document.querySelector("#ingresso");
const nome = document.querySelector("#name");
const inputId = document.querySelector("#inputId");
const dataCorreta = (date) => {
let data = date.split("");
let dataArrumada =
data.slice(8, 10).join("") +
"/" +
data.slice(5, 7).join("") +
"/" +
data.slice(0, 4).join("");
return dataArrumada;
};
async function abreModal(id) {
// modal.style.display = "flex";
modal.setAttribute("style", "display:flex");
inputId.value = id;
const resposta = await fetch(`${BASE_URL}/events/${id}`, {
method: "GET",
redirect: "follow",
headers: { "Content-Type": "application/json" },
});
const conteudoResposta = await resposta.json();
tituloModal.innerHTML = `Reserve seu ingresso para ${conteudoResposta.name}`;
tickets.innerHTML = `Tickets disponíveis: (${conteudoResposta.number_tickets})`;
ingresso.max = conteudoResposta.number_tickets;
}
function fechaModal() {
modal.setAttribute("style", "display:none");
nome.value = "";
email.value = "";
ingresso.value = "";
inputId.value = "";
}
async function listarEventos() {
const tabela = document.querySelector("tbody");
const resposta = await fetch(`${BASE_URL}/events`, {
method: "GET",
redirect: "follow",
headers: { "Content-Type": "application/json" },
});
console.log(resposta);
const conteudoResposta = await resposta.json();
const bandas = conteudoResposta.slice(0, 3);
bandas.forEach((item) => {
evento.innerHTML += ` <article class="evento card p-5 m-3">
<h2>${item.name} - ${dataCorreta(item.scheduled)}</h2>
<h4>${item.attractions}</h4>
<p>${item.description}</p>
<button class="btn btn-primary" onclick="abreModal('${
item._id
}')" >reservar ingresso</button>
</article>`;
});
}
listarEventos();
botaoClose.onclick = () => {
fechaModal();
};
botaoX.onclick = () => {
fechaModal();
};
form.onsubmit = async (evento) => {
evento.preventDefault();
const reservarTicket = {
owner_name: nome.value,
owner_email: email.value,
number_tickets: parseInt(ingresso.value),
event_id: inputId.value,
};
const options = {
method: "POST",
body: JSON.stringify(reservarTicket),
headers: { "Content-Type": "application/json" },
redirect: "follow",
};
const resposta = await fetch(`${BASE_URL}/bookings`, options);
const conteudoResposta = await resposta.json();
console.log(conteudoResposta);
if (resposta.status == 201) {
alert("Reserva realizada com sucesso");
fechaModal();
}
};