-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
206 lines (165 loc) · 4.55 KB
/
scripts.js
File metadata and controls
206 lines (165 loc) · 4.55 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
const Modal = {
modal: document.querySelector(".modal-overlay"),
open() {
this.modal.classList.add("active");
},
close() {
this.modal.classList.remove("active");
},
};
const StorageTransactions = {
get() {
return JSON.parse(localStorage.getItem("@devFinances:transactions")) || [];
},
set(transactions) {
localStorage.setItem(
"@devFinances:transactions",
JSON.stringify(transactions),
);
},
};
const Transaction = {
transactions: StorageTransactions.get(),
add(transaction) {
this.transactions.push(transaction);
App.reload();
},
remove(index) {
this.transactions.splice(index, 1);
App.reload();
},
incomes() {
const income = this.transactions.reduce((accumulator, transaction) => {
if (transaction.amount > 0) {
accumulator += transaction.amount;
}
return accumulator;
}, 0);
return income;
},
expenses() {
const expense = this.transactions.reduce((accumulator, transaction) => {
if (transaction.amount < 0) {
accumulator += transaction.amount;
}
return accumulator;
}, 0);
return expense;
},
total() {
return this.incomes() + this.expenses();
},
};
const Utils = {
formatDate(date) {
const [year, month, day] = date.split("-");
return `${day}/${month}/${year}`;
},
formatAmount(value) {
return Number(value.replace(/\,\./g, "")) * 100;
},
formatCurrency(value) {
const signal = Number(value) < 0 ? "-" : "";
value = String(value).replace(/\D/g, ""); // Achar apenas números
value = Number(value) / 100;
value = Number(value).toLocaleString("pt-BR", {
style: "currency",
currency: "BRL",
});
return `${signal} ${value}`;
},
};
const DOM = {
transactionsContainer: document.querySelector("#data-table tbody"),
innerHTMLTransaction(transaction, index) {
const CSSClassValue = transaction.amount > 0 ? "income" : "expense";
const amountFormatted = Utils.formatCurrency(transaction.amount);
const html = `
<td class="description">${transaction.description}</td>
<td class="value ${CSSClassValue}">${amountFormatted}</td>
<td>${transaction.date}</td>
<td>
<img src="./assets/minus.svg" alt="Remover transação" onclick="Transaction.remove(${index})"/>
</td>
`;
return html;
},
addTransaction(transaction, index) {
const tr = document.createElement("tr");
tr.innerHTML = this.innerHTMLTransaction(transaction, index);
this.transactionsContainer.appendChild(tr);
},
updateBalance() {
const incomeDisplay = document.getElementById("incomeDisplay");
incomeDisplay.innerHTML = Utils.formatCurrency(Transaction.incomes());
const expenseDisplay = document.getElementById("expenseDisplay");
expenseDisplay.innerHTML = Utils.formatCurrency(Transaction.expenses());
const totalDisplay = document.getElementById("totalDisplay");
totalDisplay.innerHTML = Utils.formatCurrency(Transaction.total());
},
clearTransactions() {
this.transactionsContainer.innerHTML = "";
},
};
const Form = {
description: document.querySelector("#description"),
amount: document.querySelector("#amount"),
date: document.querySelector("#date"),
getValues() {
return {
description: this.description.value,
amount: this.amount.value,
date: this.date.value,
};
},
clearFields() {
this.description.value = "";
this.amount.value = "";
this.date.value = "";
},
validateFields() {
const { description, amount, date } = this.getValues();
if (!description.trim() || !amount.trim() || !date.trim()) {
throw new Error("Por favor, preencha todos os campos");
}
},
formatValues() {
let { description, amount, date } = this.getValues();
amount = Utils.formatAmount(amount);
date = Utils.formatDate(date);
return {
description,
amount,
date,
};
},
saveTransaction(transaction) {
Transaction.add(transaction);
},
submit(event) {
event.preventDefault();
try {
this.validateFields();
const transaction = this.formatValues();
this.saveTransaction(transaction);
this.clearFields();
Modal.close();
} catch (error) {
alert(error.message);
}
},
};
const App = {
init() {
Transaction.transactions.forEach((transaction, index) => {
DOM.addTransaction(transaction, index);
});
DOM.updateBalance();
StorageTransactions.set(Transaction.transactions);
},
reload() {
DOM.clearTransactions();
App.init();
},
};
App.init();