-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
148 lines (124 loc) · 4.15 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
'use strict'
const openModal = () => document.getElementById('modal')
.classList.add('active')
const closeModal = () =>{
document.getElementById('modal').classList.remove('active')
cleanInputs()
}
const getLocalStorage = () => JSON.parse(localStorage.getItem('db_product')) ?? []
const setLocalStorage = (dbProduct) => localStorage.setItem("db_product", JSON.stringify(dbProduct))
//CRUD CREATE
const createProduto = (product) => {
const dbProduct = getLocalStorage()
dbProduct.push(product)
setLocalStorage(dbProduct)
}
//CRUD READ
const readProduct = () => getLocalStorage()
const updateProduct = (index, product) => {
const dbProduct = readProduct()
dbProduct[index] = product
setLocalStorage(dbProduct)
}
//CRUD DELETE
const deleteProduct = (index) => {
const dbProduct = readProduct()
dbProduct.splice(index, 1)
setLocalStorage(dbProduct)
}
//inputs modal form
const cleanInputs = () => {
const fields = document.querySelectorAll('.modal-field')
fields.forEach(field => field.value = "")
}
const isValidFields = () => {
return document.getElementById('form').reportValidity()
}
//Interação
const saveProduct = () => {
if (isValidFields()){
const product = {
nome: document.getElementById('name').value,
compra: Number(document.getElementById('compra').value),
venda: Number(document.getElementById('venda').value),
qnt: Number(document.getElementById('qnt').value),
lucro: Number(venda.value - compra.value),
subt: (venda.value - compra.value)* qnt.value
}
const index = document.getElementById('name').dataset.index
if(index == 'new'){
createProduto(product)
updateTable()
closeModal()
location.reload()
}else{
updateProduct(index, product)
updateTable()
closeModal()
location.reload()
}
}
}
//inserindo produtos na tabela
const createRow = (product, index) => {
const newRow = document.createElement('tr')
const subt = product.lucro * product.qnt
const subt2 = product.venda * product.qnt
newRow.innerHTML = `<td data-title="Nome">${product.nome}</td>
<td data-title="Lucro Unidade">${product.lucro}</td>
<td data-title="Qnt vendida">${product.qnt}
</td>
<td data-title="Sub lucro">R$ ${subt}</td>
<td data-title="Subt Bruto">R$ ${subt2}</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('#tbProduct>tbody').appendChild(newRow)
}
//table functions
const clearTable = () => {
const rows = document.querySelectorAll('#tbProduct>tbody tr')
rows.forEach(row => row.parentNode.removeChild(row))
}
const updateTable = () => {
const dbProduct = readProduct()
clearTable()
dbProduct.forEach(createRow)
}
updateTable()
const fillFields = (product) => {
document.getElementById('name').value = product.nome
document.getElementById('venda').value = product.venda
document.getElementById('compra').value = product.compra
document.getElementById('qnt').value = product.qnt
document.getElementById('name').dataset.index = product.index
}
const editProduct = (index) => {
const product = readProduct()[index]
product.index = index
fillFields(product)
openModal()
}
const editDelete = (event) => {
if(event.target.type == 'button'){
const [action, index] = event.target.id.split('-')
if(action == 'edit'){
editProduct(index)
}else{
deleteProduct(index)
updateTable()
}
}
}
//Eventos
document.getElementById('cadastro')
.addEventListener('click', openModal)
document.getElementById('modalClose')
.addEventListener('click', closeModal)
document.getElementById('salvar')
.addEventListener('click', saveProduct)
document.getElementById('cancelar')
.addEventListener('click', closeModal)
document.querySelector('#tbProduct>tbody')
.addEventListener('click', editDelete)