-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
142 lines (115 loc) · 3.88 KB
/
script.js
File metadata and controls
142 lines (115 loc) · 3.88 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
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
const openModal = () => document.getElementById('modal')
.classList.add('active')
const closeModal = () => {
clearFields()
document.getElementById('modal')
.classList.remove('active')
}
const getLocalStorage = () => JSON.parse(localStorage.getItem('database_client')) ?? []
const setLocalStorage = (databaseClient) => localStorage.setItem('database_client', JSON.stringify(databaseClient))
// CRUD -create read update delete
const deleteClient = (index) => {
const databaseClient = readClient()
databaseClient.splice(index, 1)
setLocalStorage(databaseClient)
}
const updateClient = (index, client) => {
const dataBaseClient = readClient()
dataBaseClient[index] = client
setLocalStorage(dataBaseClient)
}
const readClient = () => getLocalStorage()
const createClient = (client) => {
const databaseClient = getLocalStorage()
databaseClient.push(client)
setLocalStorage(databaseClient)
}
const isValidFields = () => {
return document.getElementById('form').reportValidity()
}
const clearFields = () => {
const fields = document.querySelectorAll('.modal-field')
fields.forEach(field => field.value = '')
}
const saveClient = () => {
if (isValidFields()) {
const client = {
name: document.getElementById('name').value,
email: document.getElementById('email').value,
cellphone: document.getElementById('cellphone').value,
city: document.getElementById('city').value,
}
const index = document.getElementById('name').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.name}</td>
<td>${client.email}</td>
<td>${client.cellphone}</td>
<td>${client.city}</td>
<td>
<button type="button" class="button green" id="edit-${index}">Editar</button>
<button type="button" class="button red" id="delet-${index}">Excluir</button>
</td>
`
document.querySelector('#table-client>tbody').appendChild(newRow)
}
const clearTable = () => {
const rows = document.querySelectorAll('#table-client>tbody tr')
rows.forEach(row => row.parentNode.removeChild(row))
}
const updateTable = () => {
const databaseClient = readClient()
clearTable()
databaseClient.forEach(createRow)
}
const fillFields = (client) => {
document.getElementById('name').value = client.name
document.getElementById('email').value = client.email
document.getElementById('cellphone').value = client.cellphone
document.getElementById('city').value = client.city
document.getElementById('name').dataset.index = client.index
openModal()
}
const editClient = (index) => {
const client = readClient()[index]
client.index = index
fillFields(client)
openModal()
}
const editAndDelete = (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(`Deseja realmente excluir o cliente ${client.name}`)
if(response) {
deleteClient(index)
updateTable()
}
}
}
}
updateTable()
// Events
document.getElementById('registerCustomers')
.addEventListener('click', openModal)
document.getElementById('modalClose')
.addEventListener('click', closeModal)
document.getElementById('save')
.addEventListener('click', saveClient)
document.querySelector('#table-client>tbody')
.addEventListener('click', editAndDelete)