-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
164 lines (148 loc) · 4.19 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
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
// Criando arrays (lista de participantes)
let participantes = [
// Posição 0
{
nome: "Diego Fernandes",
email: "[email protected]",
dataInscricao: new Date(2024, 2, 1, 19, 23),
dataCheckIn: new Date(2024, 2, 1, 20, 20)
},
// Posição 1
{
nome: "Mayk Brito",
email: "[email protected]",
dataInscricao: new Date(2024, 2, 23, 19, 23),
dataCheckIn: new Date(2024, 2, 25, 20, 20)
},
// Posição 2 ...
{
nome: "Ana Souza",
email: "[email protected]",
dataInscricao: new Date(2024, 0, 3, 19, 23),
dataCheckIn: new Date(2024, 0, 4, 20, 20)
},
{
nome: "João Silva",
email: "[email protected]",
dataInscricao: new Date(2023, 11, 4, 19, 23),
dataCheckIn: new Date(2023, 11, 5, 20, 20)
},
{
nome: "Maria Oliveira",
email: "[email protected]",
dataInscricao: new Date(2023, 10, 5, 19, 23),
dataCheckIn: new Date(2023, 10, 6, 20, 20)
},
{
nome: "Hugo Herrera",
email: "[email protected]",
dataInscricao: new Date(2023, 9, 6, 19, 23),
dataCheckIn: new Date(2023, 9, 7, 20, 20)
},
{
nome: "Carla Lima",
email: "[email protected]",
dataInscricao: new Date(2023, 8, 7, 19, 23),
dataCheckIn: new Date(2023, 8, 8, 20, 20)
},
{
nome: "Lucas Sousa",
email: "[email protected]",
dataInscricao: new Date(2023, 7, 8, 19, 23),
dataCheckIn: new Date(2023, 7, 9, 20, 20)
},
{
nome: "Paula Costa",
email: "[email protected]",
dataInscricao: new Date(2023, 6, 9, 19, 23),
dataCheckIn: new Date(2023, 6, 10, 20, 20)
},
{
nome: "Gabriel Almeida",
email: "[email protected]",
dataInscricao: new Date(2023, 5, 10, 19, 23),
dataCheckIn: new Date(2023, 5, 11, 20, 20)
}
];
// Criando funções com arrow (=>)
const criarNovoParticipante = (participante) => {
const dataInscricao = dayjs(Date.now()).to(participante.dataInscricao)
let dataCheckIn = dayjs(Date.now()).to(participante.dataCheckIn)
// Estrutura Condicional
if(participante.dataCheckIn == null){
dataCheckIn = `
<button
data-email="${participante.email}"
onClick="fazerCheckin(event)"
>
Confirmar check-in
</button>
`
}
return `
<tr>
<td>
<strong>
${participante.nome}
</strong>
<br>
<small>
${participante.email}
</small>
</td>
<td>${dataInscricao}</td>
<td>${dataCheckIn}</td>
</tr>
`
}
// Função atualizarLista
const atualizarLista = (participantes) => {
let output = ""
// Estrutura de repetição
for(let participante of participantes){
output = output + criarNovoParticipante(participante)
}
// Substituindo informação do HTML
document
.querySelector('tbody')
.innerHTML = output
}
atualizarLista(participantes)
const adicionarParticipante = (event) => {
event.preventDefault()
const dadosDoFormulario = new FormData(event.target)
const participante = {
nome: dadosDoFormulario.get('nome'),
email: dadosDoFormulario.get('email'),
dataInscricao: new Date(),
dataCheckIn: null
}
// Verificando se o participante já existe antes de adiciona-lo
const participanteExiste = participantes.find(
(p) => p.email == participante.email
)
if(participanteExiste) {
alert ('Email já cadastrado')
return
}
participantes = [participante, ...participantes]
atualizarLista(participantes)
// Limpando o formulário após adicionar um participante
event.target.querySelector('[name="nome"]').value = ""
event.target.querySelector('[name="email"]').value = ""
}
const fazerCheckin = (event) => {
// Confirmando se realmente quer o check-in
const mensagemConfirmacao = 'Deseja fazer o check-in?'
if(confirm(mensagemConfirmacao) == false){
return
}
// Encontrando o participante na (lista)
const participante = participantes.find(
(p) => p.email == event.target.dataset.email
)
// Atualizando o check-in do participante
participante.dataCheckIn = new Date()
// Atualizando a lista de participante
atualizarLista(participantes)
}