-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.ts
286 lines (249 loc) · 8.08 KB
/
script.ts
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
interface Account {
owner: string;
username?: string;
movements: number[];
interestRate: number;
pin: number;
balance?: number;
}
// Data
const account1: Account = {
owner: 'Mathias Quist Michaelsen',
movements: [200, 450, -400, 3000, -650, -130, 70, 1300],
interestRate: 1.2,
pin: 1111,
};
const account2: Account = {
owner: 'John Doe',
movements: [5000, 3400, -150, -790, -3210, -1000, 8500, -30],
interestRate: 1.5,
pin: 2222,
};
const account3: Account = {
owner: 'Malte Krog',
movements: [200, -200, 340, -300, -20, 50, 400, -460],
interestRate: 0.7,
pin: 3333,
};
const account4: Account = {
owner: 'Christian Thrige',
movements: [430, 1000, 700, 50, 90],
interestRate: 1,
pin: 4444,
};
const accounts: Account[] = [account1, account2, account3, account4];
// Elements
const labelWelcome = document.querySelector<HTMLParagraphElement>('.welcome')!;
const labelDate = document.querySelector<HTMLDivElement | HTMLSpanElement>(
'.date'
)!;
const labelBalance =
document.querySelector<HTMLParagraphElement>('.balance__value')!;
const labelSumIn = document.querySelector<HTMLParagraphElement>(
'.summary__value--in'
)!;
const labelSumOut = document.querySelector<HTMLParagraphElement>(
'.summary__value--out'
)!;
const labelSumInterest = document.querySelector<HTMLParagraphElement>(
'.summary__value--interest'
)!;
const containerApp = document.querySelector<HTMLElement>('.app')!;
const containerMovements =
document.querySelector<HTMLDivElement>('.movements')!;
const btnLogin = document.querySelector<HTMLButtonElement>('.login__btn')!;
const btnTransfer = document.querySelector<HTMLButtonElement>(
'.form__btn--transfer'
)!;
const btnLoan = document.querySelector<HTMLButtonElement>('.form__btn--loan')!;
const btnClose =
document.querySelector<HTMLButtonElement>('.form__btn--close')!;
const btnSort = document.querySelector<HTMLButtonElement>('.btn--sort')!;
const inputLoginUsername = document.querySelector<HTMLInputElement>(
'.login__input--user'
)!;
const inputLoginPin =
document.querySelector<HTMLInputElement>('.login__input--pin')!;
const inputTransferTo =
document.querySelector<HTMLInputElement>('.form__input--to')!;
const inputTransferAmount = document.querySelector<HTMLInputElement>(
'.form__input--amount'
)!;
const inputLoanAmount = document.querySelector<HTMLInputElement>(
'.form__input--loan-amount'
)!;
const inputCloseUsername =
document.querySelector<HTMLInputElement>('.form__input--user')!;
const inputClosePin =
document.querySelector<HTMLInputElement>('.form__input--pin')!;
const displayMovements = function (movements: number[], sort = false): void {
containerMovements.innerHTML = '';
// Sort
const movs = sort ? movements.slice().sort((a, b) => a - b) : movements;
movs.forEach(function (mov, i) {
const type = mov > 0 ? 'deposit' : 'withdrawal';
const html = `
<div class="movements__row" style=" padding: 2.25rem 4rem;
display: flex;
align-items: center;
border-bottom: 1px solid #eee;">
<div class="movements__type movements__type--${type}" style=" font-size: 1.1rem;
text-transform: uppercase;
font-weight: 500;
color: #fff;
padding: 0.1rem 1rem;
border-radius: 10rem;
margin-right: 2rem;">${i + 1} ${type}</div>
<div class="movements__value" style=" font-size: 1.7rem;
margin-left: auto;">${mov} €</div>
</div>`;
containerMovements.insertAdjacentHTML('afterbegin', html);
});
};
// Calculate Account balance
const calcDisplayBalance = function (currentAccount: Account): void {
currentAccount.balance = currentAccount.movements.reduce(
(acc, mov) => acc + mov,
0
);
labelBalance.textContent = `${currentAccount.balance} €`;
};
// Calculate Summary
const calcDisplaySummary = function (currentAccount: Account): void {
const incomes = currentAccount.movements
.filter(mov => mov > 0)
.reduce((acc, mov) => acc + mov, 0);
labelSumIn.textContent = `${incomes} €`;
const outcome = currentAccount.movements
.filter(mov => mov < 0)
.reduce((acc, mov) => acc + mov, 0);
labelSumOut.textContent = `${Math.abs(outcome)} €`;
const interest = currentAccount.movements
.filter(mov => mov > 0)
.map(deposit => (deposit * currentAccount.interestRate) / 100)
.filter((int, i, arr) => {
return int >= 1;
})
.reduce((acc, int) => acc + int, 0);
labelSumInterest.textContent = `${Math.abs(interest)} €`;
};
// Create usernames from accounts
function computeUserName(accs: Account[]): void {
accs.forEach(function (acc) {
acc.username = acc.owner
.toLowerCase()
.split(' ')
.map(name => {
return name[0];
})
.join('');
});
}
computeUserName(accounts);
const updateUi = function (account: Account): void {
// display movements
displayMovements(account.movements);
// display balance
calcDisplayBalance(account);
// display summary
calcDisplaySummary(account);
};
// Event handler
let currentAccount: Account | undefined;
btnLogin.addEventListener('click', function (e) {
e.preventDefault();
currentAccount = accounts.find(
acc => acc.username === inputLoginUsername.value
);
if (currentAccount?.pin === Number(inputLoginPin.value)) {
// Dispaly UI and Welcome message!
labelWelcome.textContent = `Welcome back ${
currentAccount.owner.split(' ')[0]
}`;
inputLoginUsername.value = inputLoginPin.value = '';
inputLoginPin.blur();
containerApp.style.opacity = '1';
updateUi(currentAccount);
}
});
btnTransfer.addEventListener('click', function (e) {
e.preventDefault();
const amount = Number(inputTransferAmount.value);
const receiverAccount = accounts.find(
acc => acc.username === inputTransferTo.value
);
console.log(amount);
if (
amount > 0 &&
receiverAccount &&
currentAccount?.balance! >= amount &&
receiverAccount?.username !== currentAccount?.username
) {
inputTransferAmount.style.color = inputTransferTo.style.color = 'black';
currentAccount?.movements.push(-amount);
receiverAccount?.movements.push(amount);
updateUi(currentAccount!);
inputTransferAmount.value = inputTransferTo.value = '';
} else {
inputTransferAmount.style.color = inputTransferTo.style.color = 'red';
}
});
btnLoan.addEventListener('click', function (e) {
e.preventDefault();
const amount = Number(inputLoanAmount.value);
if (
amount > 0 &&
currentAccount?.movements.some(mov => mov >= amount * 0.1)
) {
currentAccount.movements.push(amount);
updateUi(currentAccount);
inputLoanAmount.value = '';
}
});
btnClose.addEventListener('click', function (e) {
e.preventDefault();
const accountToClose = inputCloseUsername.value;
const accountToClosePin = Number(inputClosePin.value);
if (
accountToClose === currentAccount?.username &&
accountToClosePin === currentAccount.pin
) {
const accountIndex = accounts.findIndex(
acc => acc.username === currentAccount?.username
);
console.log(accountIndex);
accounts.splice(accountIndex, 1);
containerApp.style.opacity = '0';
}
inputCloseUsername.value = inputClosePin.value = '';
});
let sorted = false;
btnSort.addEventListener('click', function (e) {
e.preventDefault();
displayMovements(currentAccount?.movements!, !sorted);
sorted = !sorted;
});
// const movements: number[] = [200, 450, -400, 3000, -650, -130, 70, 1300];
// Sort
// ascending
// movements.sort((a, b) => a - b);
// console.log(movements);
// descending
// movements.sort((a, b) => b - a);
// console.log(movements);
// const currencies = new Map([
// ['USD', 'United States dollar'],
// ['EUR', 'Euro'],
// ['GBP', 'Pound sterling'],
// ]);
// const euroToUsd = 1.1;
// const movementsUsd: number[] = movements.map((mov): number => mov * euroToUsd);
// const movementsDescriptions = movements.map(
// (mov, i) =>
// `Movement ${i + 1}: You ${mov > 0 ? 'deposited' : 'withdrew'} ${Math.abs(
// mov
// )}`
// );
// Filter for deposits & withdrawals
// const deposits: number[] = movements.filter(mov => mov > 0);
// const withdrawal: number[] = movements.filter(mov => mov < 0);