-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
165 lines (139 loc) · 5.57 KB
/
index.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
const inAddBtn = document.getElementById("income-addBtn");
const outAddBtn = document.getElementById("outcome-addBtn");
let inOutData = [];
let inOutDataLS = JSON.parse(localStorage.getItem("inOutArr"));
if (inOutDataLS?.length > 0) {
inOutData = JSON.parse(localStorage.getItem("inOutArr"));
inOutDataElem = [...inOutData];
updateMainContentnListUI();
sum();
}
let lastId = 0;
function onInAddBtnClicked() {
const incomeName = document.getElementById("income-name");
const incomeValue = document.getElementById("income-val");
if (incomeName.value.trim() === '' || incomeValue.value.trim() === '') {
alert("Uzupełnij nazwę i/lub kwotę");
return false;
}
inOutDataElem = {
name: incomeName.value,
value: incomeValue.value,
id: lastId,
type: "income"
}
inOutData.push(inOutDataElem);
lastId++;
updateMainContentnListUI();
sum();
}
function onOutAddBtnClicked() {
const outcomeName = document.getElementById("outcome-name");
const outcomeValue = document.getElementById("outcome-val");
if (outcomeName.value.trim() === '' || outcomeValue.value.trim() === '') {
alert("Uzupełnij nazwę i/lub kwotę");
return false;
}
inOutDataElem = {
name: outcomeName.value,
value: outcomeValue.value,
id: lastId,
type: "outcome"
}
inOutData.push(inOutDataElem);
lastId++;
updateMainContentnListUI();
sum();
}
function onDeleteBtnClicked(event) {
inOutData = inOutData.filter(function(elem){
return elem.id !== Number(event.target.id.split('-')[1]);
});
localStorage.setItem("inOutArr", JSON.stringify(inOutData));
updateMainContentnListUI();
sum();
}
function onEditBtnClicked(event){
indexElem = inOutData.findIndex(function(elem){
return elem.id === Number(event.target.id.split('-')[1]);
});
let container = document.getElementById(`container-${event.target.id.split('-')[1]}`);
// container.classList = ("z-index");
// shadow = document.createElement("div");
// shadow.classList = ("shadow");
// document.body.appendChild(shadow);
container.innerHTML =`
<input class="edited-name" id="edited-name" type="text" value="${inOutData[indexElem].name}" placeholder="nazwa przychodu">
<input class="edited-val" id="edited-val" type="number" value="${inOutData[indexElem].value}" placeholder="kwota przychodu">
<button class="addBtn" id="edited-addBtn">Dodaj</button>
`;
const editedAddBtn = document.getElementById("edited-addBtn")
editedAddBtn.addEventListener("click", function() {
const editedName = document.getElementById("edited-name");
const editedVal = document.getElementById("edited-val");
inOutData[indexElem].name = editedName.value;
inOutData[indexElem].value = editedVal.value;
updateMainContentnListUI()
sum();
// shadow.remove();
});
}
function updateMainContentnListUI () {
const mainContentIncomeList = document.getElementById("mainContent-income-list");
const mainContentOutcomeList = document.getElementById("mainContent-outcome-list");
mainContentIncomeList.innerText = " ";
mainContentOutcomeList.innerText = " ";
inOutData.forEach(function(inOutDataElem) {
const container = document.createElement("div");
container.classList = "list-element"
container.id = `container-${inOutDataElem.id}`;
const paragraph = document.createElement("p");
paragraph.innerText = `${inOutDataElem.name}: ${inOutDataElem.value} zł`
const editBtn = document.createElement("button");
editBtn.innerText = "Edytuj"
editBtn.id = `edit-${inOutDataElem.id}`;
editBtn.addEventListener("click", onEditBtnClicked);
const deleteBtn = document.createElement("button");
deleteBtn.innerText = "Usuń"
deleteBtn.id = `edit-${inOutDataElem.id}`;
deleteBtn.addEventListener("click", onDeleteBtnClicked)
container.appendChild(paragraph);
container.appendChild(editBtn);
container.appendChild(deleteBtn);
if (inOutDataElem.type === "income") {
mainContentIncomeList.appendChild(container)
} else {
mainContentOutcomeList.appendChild(container)
}
localStorage.setItem("inOutArr", JSON.stringify(inOutData));
});
}
function sum() {
document.getElementById("balance")?.remove();
let incomeSum = 0;
let outcomeSum = 0;
const incomeSumElem = document.getElementById("incomeSum");
const outcomeSumElem = document.getElementById("outcomeSum");
const topContainer = document.getElementById("topContainer");
inOutData.forEach(function(inOutDataElem) {
if (inOutDataElem.type === "income") {
incomeSum += parseInt(inOutDataElem.value);
}
else if (inOutDataElem.type === "outcome"){
outcomeSum += parseInt(inOutDataElem.value);
}
});
incomeSumElem.innerText = incomeSum.toString();
outcomeSumElem.innerText = outcomeSum.toString();
let sumAll = incomeSum - outcomeSum;
const sumAllContent = document.createElement("h1");
sumAllContent.id = "balance";
if (sumAll < 0) {
sumAllContent.innerText = `Bilans jest ujemny. Jesteś na minusie ${sumAll*(-1)} złotych`;
} else if (sumAll == 0) {
sumAllContent.innerText = `Bilans wynosi zero`;
} else {
sumAllContent.innerText = `Możesz jeszcze wydać ${sumAll} złotych`;
}
topContainer.appendChild(sumAllContent);
}