-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
184 lines (152 loc) · 4.78 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const openModal = () => document.getElementById('modal').classList.add('active')
const closeModal = () => {
clearFields()
document.getElementById('modal').classList.remove('active')
}
const getLocalStorage = () => JSON.parse(localStorage.getItem('dbClient')) ?? []
const setLocalStorage = dbClient =>
localStorage.setItem('dbClient', JSON.stringify(dbClient))
//----CRUD----
//CREATE
const createClient = client => {
const dbClient = getLocalStorage()
dbClient.push(client)
setLocalStorage(dbClient)
}
//READ
const readClient = () => getLocalStorage()
//UPDATE
const updateClient = (index, client) => {
const dbClient = readClient()
dbClient[index] = client
setLocalStorage(dbClient)
}
//DELETE
const deleteClient = index => {
const dbClient = readClient()
dbClient.splice(index, 1)
setLocalStorage(dbClient)
}
const isValidFields = () => {
return document.getElementById('form').reportValidity()
}
//Interação com o Layout
const clearFields = () => {
const fields = document.querySelectorAll('.modal-field')
fields.forEach(field => (field.value = ''))
document.getElementById('nome').dataset.index = 'new'
}
// Botão salvar cliente
const saveClient = () => {
if (isValidFields()) {
const client = {
nome: document.getElementById('nome').value,
cpf: document.getElementById('cpf').value,
email: document.getElementById('email').value,
telefone: document.getElementById('telefone').value,
endereco: document.getElementById('endereco').value
}
const index = document.getElementById('nome').dataset.index
if (index == 'new') {
createClient(client)
updateTable()
closeModal()
} else {
updateClient(index, client)
updateTable()
closeModal()
}
}
}
// Criando as colunas dos clientes adicionados
const createRow = (client, index) => {
const newRow = document.createElement('tr')
newRow.innerHTML = `
<td>${client.nome}</td>
<td>${client.cpf}</td>
<td>${client.email}</td>
<td>${client.telefone}</td>
<td>${client.endereco}</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('cpf').value = client.cpf
document.getElementById('email').value = client.email
document.getElementById('telefone').value = client.telefone
document.getElementById('endereco').value = client.endereco
document.getElementById('nome').dataset.index = client.index
}
// Botão editar cliente
const editClient = index => {
const client = readClient()[index]
client.index = index
fillFields(client)
openModal()
}
// Botão deletar cliente
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(
`Você tem certeza que deseja deletar o cliente ${client.nome}?`
)
if (response) {
deleteClient(index)
updateTable()
}
}
}
}
updateTable()
//-----Eventos-----
document.getElementById('cadastrarCliente').addEventListener('click', openModal)
document.getElementById('modalClose').addEventListener('click', closeModal)
document.getElementById('salvar').addEventListener('click', saveClient)
document.getElementById('cancelar').addEventListener('click', clearFields)
document
.querySelector('#tableClient>tbody')
.addEventListener('click', editDelete)
// DARK MODE
const html = document.querySelector('html')
const checkbox = document.querySelector('input[name=theme]')
const getStyle = (element, style) =>
window.getComputedStyle(element).getPropertyValue(style)
const initialColors = {
bgColor: getStyle(html, '--bg-color'),
textColor: getStyle(html, '--text-color'),
secundaryColor: getStyle(html, ' --secundary-color')
}
const darkMode = {
bgColor: '#222222',
textColor: '#FFFFFF',
secundaryColor: '#a04d00'
}
const transformKey = key =>
"--" + key.replace(/([A-Z])/, "-$1").toLowerCase()
const changeColors = colors => {
Object.keys(colors).map(key =>
html.style.setProperty(transformKey(key), colors[key])
)
}
checkbox.addEventListener("change", ({ target }) => {
target.checked ? changeColors(darkMode) : changeColors(initialColors)
})