-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
218 lines (197 loc) · 5.7 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
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
207
208
209
210
211
212
213
214
215
216
217
218
let transactions = load() || []
const NODE_ELEMENT_SELECTORS = {
historyList: '[data-js~=js-history] ul',
summary: '[data-js~=js-summary]',
newTransaction: '[data-js~=js-add-transaction]',
triggerSelector: '[data-js~=js-trigger]',
togglerSelector: '[data-js~=js-toggle]',
}
const render = () => {
renderTransactions(NODE_ELEMENT_SELECTORS.historyList)
renderSummary(NODE_ELEMENT_SELECTORS.summary)
}
const init = () => {
addToggleLogicToSections()
addSubmitEventListenerToForm(NODE_ELEMENT_SELECTORS.newTransaction)
render()
}
init()
function addSubmitEventListenerToForm(formContainerSelector, target, index) {
const formEl = getEl(formContainerSelector + ' form', target)
formEl.addEventListener('submit', (event) =>
onSubmitTransaction(event, formEl, index)
)
}
//submit form handler
function onSubmitTransaction(event, formEl, index) {
event.preventDefault()
formSubmitDataHandler(formEl, index)
render()
}
function formSubmitDataHandler(formEl, index, inputSelector = 'input') {
const inputs = getAll(inputSelector, formEl)
const newValue = inputs.reduce(
(acc, input) => ({ ...acc, ...extractFormatedValue(input) }),
{}
)
index = index > -1 ? index : transactions.length
transactions = [
...transactions.slice(0, index),
newValue,
...transactions.slice(index + 1),
]
save()
}
function extractFormatedValue(input) {
const { value, name, type } = input
switch (type) {
case 'number':
input.value = 0
return { [name]: Number(value) }
default:
input.value = ''
return name ? { [name]: value } : {}
}
}
// render summary
function renderSummary(sel) {
const { expense, income } = getSummaryValues()
const summaryElement = getEl(sel)
const summaryHtml = `
<h2 class="summary__heading" >
<div>Your Balance</div>
<div class="banlance__result">$${income + expense}</div>
</h2>
<div class="summary__details">
<div class="summary__income">
<h4>Income</h4>
<div class="summary__total summary__total--positive">$${income}</div>
</div>
<div class="summary__expense">
<h4>Expense</h4>
<div class="summary__total summary__total--negative">$${expense}</div>
</div>
</div>
`
summaryElement.innerHTML = summaryHtml
}
function getSummaryValues() {
return transactions.reduce(
(acc, { amount }) => {
switch (true) {
case amount > 0: {
return { ...acc, income: acc.income + amount }
}
case amount < 0:
return { ...acc, expense: acc.expense + amount }
break
default:
return acc
}
},
{ income: 0, expense: 0 }
)
}
/// toggle
function addToggleLogicToSections(target) {
const { triggerSelector } = NODE_ELEMENT_SELECTORS
const parents = getAll(NODE_ELEMENT_SELECTORS.togglerSelector, target)
parents.forEach((parent) => {
const trigger = getEl(triggerSelector, parent)
trigger &&
trigger.addEventListener('click', () => {
toggleElementSiblings(triggerSelector, parent)
})
})
}
function toggleElementSiblings(sel, parent) {
const siblings = getAll(sel + '~*', parent)
siblings.forEach(toggleElement)
}
function toggleElement(el) {
el.classList.toggle('hidden')
}
//renderTransactionsHistory
function renderTransactions(sel) {
const parent = getEl(sel)
parent.innerHTML = ''
transactions.forEach(({ label, amount }, index) =>
renderTransaction(parent, label, amount, index)
)
}
function renderTransaction(target = document, label = '', amount = 0, index) {
const historyElementHtml = `
<div data-js="js-toggle">
<div class="history__records ${
amount > 0
? 'history__records__income'
: amount
? 'history__records__expense'
: ''
}" data-js="js-trigger">
<button type="button" data-js="js-delete"></button>
<span>${label}</span>
<span class="history_records__summary">${amount}</span>
</div>
<div class="hidden" data-js="js-${index}-transaction">
<form>
<label class="edit-form__field">
<div>Text</div>
<input
type="text"
placeholder="Enter a this entry label..."
name="label"
value="${label}"
/>
</label>
<label class="edit-form__field">
<div>
Amount
<p>(negative - expense, positive - income)</p>
</div>
<input
type="number"
name="amount"
value="${amount}"
placeholder="Enter a number..."
/>
</label>
<button type="submit" class="edit-form__submit-button">
update transaction
</button>
</form>
</div>
</div>
`
const historyEl = document.createElement('li')
historyEl.innerHTML = historyElementHtml
addToggleLogicToSections(historyEl)
const deleteButtonElement = getEl('[data-js~=js-delete]', historyEl)
deleteButtonElement.addEventListener('click', () => removeTransaction(index))
addSubmitEventListenerToForm(
`[data-js~=js-${index}-transaction]`,
historyEl,
index
)
target.insertAdjacentElement('beforeend', historyEl)
}
function removeTransaction(index) {
transactions = [
...transactions.slice(0, index),
...transactions.slice(index + 1),
]
save()
render()
}
function save() {
localStorage.setItem('transactions', JSON.stringify(transactions))
}
function load() {
return JSON.parse(localStorage.getItem('transactions'))
}
function getEl(sel, target = document) {
return target.querySelector(sel)
}
function getAll(sel, target = document) {
return Array.from(target.querySelectorAll(sel))
}