-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
33 lines (26 loc) · 1.07 KB
/
Copy pathscript.js
File metadata and controls
33 lines (26 loc) · 1.07 KB
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
// registros na memória
const form = document.querySelector("#form-habits")
const nlwSetup = new NLWSetup(form)
const button = document.querySelector('header button')
// registrou na memória um evento de click
button.addEventListener("click", add)
// sempre que o botão for clicado ele vai registrar a mudança com o save, para salvar a data no localstorage do navegador
form.addEventListener("change", save)
function add(){
const today = new Date().toLocaleDateString('pt-br').slice(0, -5)
const dayExists = nlwSetup.dayExists(today)
if(dayExists){
alert("Dia já incluso! ❌")
return
}
alert("Dia adicionado com Sucesso! ✔")
nlwSetup.addDay(today)
}
// save transforma os dados em objetos para array de string
function save(){
localStorage.setItem("NLWSetup@habits", JSON.stringify(nlwSetup.data))
}
// transforma o array de string em objetos novamente, o OU é porquê na primeira vez o localstorage estará vazio
const data = JSON.parse(localStorage.getItem("NLWSetup@habits")) || {}
nlwSetup.setData(data)
nlwSetup.load()