-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
151 lines (119 loc) · 4 KB
/
main.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
'use strict'
const openModal = () => document.getElementById('modal')
.classList.add('active')
const closeModal = () =>{
clearFields()
document.getElementById('modal').classList.remove('active')
}
const getLocalstorage = () => JSON.parse(localStorage.getItem('db_client')) ?? []
const setLocalstorage = (dbClient) => localStorage.setItem("db_client" , JSON.stringify(dbClient))
const deleteClient = (index) => {
const dbClient = readClient()
dbClient.splice(index, 1)
setLocalstorage(dbClient)
}
const updateClient = (index, client) => {
const dbClient = readClient()
dbClient[index] = client
setLocalstorage(dbClient)
}
const readClient = () => getLocalstorage()
const createClient = (client) => {
const dbClient = getLocalstorage()
dbClient.push (client)
setLocalstorage(dbClient)
}
const isValidFields = () => {
return document.getElementById('form').reportValidity()
}
const clearFields = () => {
const fields = document.querySelectorAll('.modal-field')
fields.forEach(field => field.value = "")
document.getElementById('nome').dataset.index = 'new'
document.querySelector(".modal-header>h2").textContent = 'novo cliente'
}
const saveClient = () => {
if (isValidFields()) {
const client = {
nome: document.getElementById('nome').value,
email: document.getElementById('email').value,
celular: document.getElementById('celular').value,
cep: document.getElementById('cep').value
}
const index = document.getElementById('nome').dataset.index
if (index == 'new') {
createClient(client)
updateTable()
closeModal()
}
else{
updateClient(index, client)
updateTable()
closeModal()
}
}
}
const createRow = (client, index) => {
const newRow = document.createElement('tr')
newRow.innerHTML = `
<td>${client.nome}</td>
<td>${client.email}</td>
<td>${client.celular}</td>
<td>${client.cep}</td>
<td>
<button type="button" class="button green" id="edit-${index}" >Editar</button>
<button type="button" class="button red" id="delete-${index}" >Excluir</button>
</td>
`
document.querySelector('#tableClient>tbody').appendChild(newRow)
}
const clearTable = () => {
const rows = document.querySelectorAll('#tableClient>tbody tr')
rows.forEach(row => row.parentNode.removeChild(row))
}
const updateTable = () => {
const dbClient = readClient()
clearTable()
dbClient.forEach(createRow)
}
const fillFields = (client) => {
document.getElementById('nome').value = client.nome
document.getElementById('email').value = client.email
document.getElementById('celular').value = client.celular
document.getElementById('cep').value = client.cep
document.getElementById('nome').dataset.index = client.index
}
const editClient = (index) => {
const client = readClient()[index]
client.index = index
fillFields(client)
document.querySelector(".modal-header>h2").textContent = `editando ${client.nome}`
openModal()
}
const editDelete = (event) => {
if (event.target.type == 'button') {
const [action, index] = event.target.id.split('-')
if (action == 'edit') {
editClient(index)
}
else{
const client = readClient()[index]
const response = confirm(`realmente deletar ${client.nome}`)
if (response) {
deleteClient(index)
updateTable()
}
}
}
}
updateTable()
document.getElementById('cadastrocliente')
.addEventListener('click', openModal)
document.getElementById('modalClose')
.addEventListener('click', closeModal)
document.getElementById('salvar')
.addEventListener('click', saveClient)
document.querySelector('#tableClient>tbody')
.addEventListener('click', editDelete)
document.getElementById('cancelar')
.addEventListener('click', closeModal)