-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
62 lines (50 loc) · 2.15 KB
/
script.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
const incomeSection = document.querySelector('.income-area');
const expensesSection = document.querySelector('.expenses-area');
const availableMoney = document.querySelector('.available-money');
const addTransactionPanel = document.querySelector('.add-transaction-panel');
const nameInput = document.querySelector('#name');
const amountInput = document.querySelector('#amount');
const categorySelect = document.querySelector('#category');
const addTransactionBtn = document.querySelector('.add-transaction');
const saveBtn = document.querySelector('.save');
const cancelBtn = document.querySelector('.cancel');
const deleteBtn = document.querySelector('.delete');
const deleteAllBtn = document.querySelector('.delete-all');
let root = document.documentElement;
let ID = 0;
let selectedCategory;
let moneyArr = [0];
const showPanel = () => {
addTransactionPanel.style.display = 'flex';
}
const closePanel = () => {
addTransactionPanel.style.display = 'none';
clearForms();
}
const checkForm = () => {
if (nameInput.value !== '' && amountInput.value !== '' && categorySelect.value !== 'none') {
createNewTransaction();
} else {
alert('Fill in all the blanks!')
}
}
const clearForms = () => {
nameInput.value = '';
amountInput.value = '';
categorySelect.value = 'none';
}
const createNewTransaction = () => {
const newTransaction = document.createElement('div');
newTransaction.classList.add('transaction');
newTransaction.setAttribute('id', ID);
newTransaction.innerHTML = `<p class="transaction-name">${nameInput.value}</p>
<p class="transaction-amount">${amountInput.value} zł<button class="delete" onclick="deleteTransaction(${ID})">X</button></p>`
amountInput.value > 0 ? incomeSection.appendChild(newTransaction) && newTransaction.classList.add('income') : expensesSection.appendChild(newTransaction) && newTransaction.classList.add('expense');
moneyArr.push(parseFloat(amountInput.value));
closePanel();
ID++;
clearForms();
}
addTransactionBtn.addEventListener('click', showPanel);
cancelBtn.addEventListener('click', closePanel);
saveBtn.addEventListener('click', checkForm, createNewTransaction)