-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
473 lines (398 loc) · 12.4 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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
'use strict';
/*
changed by Masen on Aug 2024
GitHub : https://github.com/DevMasen
main project by Jonas Schmedtman Udemy
*/
//! Data
const account1 = {
owner: 'Jonas Schmedtmann',
movements: [
{
price: 200,
date: '2019-11-01T13:15:33.035Z',
},
{
price: 450,
date: '2019-11-30T09:48:16.867Z',
},
{
price: -400,
date: '2019-12-25T06:04:23.907Z',
},
{
price: 3000,
date: '2020-01-25T14:18:46.235Z',
},
{
price: -650,
date: '2020-02-05T16:33:06.386Z',
},
{
price: 1300,
date: '2020-07-26T12:01:20.894Z',
},
],
interestRate: 1.2,
pin: 1111,
currency: 'EUR',
exchangeRate: 0.92,
locale: 'pt-PT',
};
const account2 = {
owner: 'Jessica Davis',
movements: [
{ price: 5000, date: '2019-11-01T13:15:33.035Z' },
{ price: 3400, date: '2019-11-30T09:48:16.867Z' },
{ price: -150, date: '2019-12-25T06:04:23.907Z' },
{ price: -790, date: '2020-01-25T14:18:46.235Z' },
{ price: -3210, date: '2020-02-05T16:33:06.386Z' },
{ price: -1000, date: '2020-04-10T14:43:26.374Z' },
{ price: 8500, date: '2020-06-25T18:49:59.371Z' },
{ price: -30, date: '2020-07-26T12:01:20.894Z' },
],
interestRate: 1.5,
pin: 2222,
currency: 'EUR',
exchangeRate: 0.92,
locale: 'en-UK',
};
const account3 = {
owner: 'Steven Thomas Williams',
movements: [
{ price: 200, date: '2019-11-01T13:15:33.035Z' },
{ price: -200, date: '2019-11-30T09:48:16.867Z' },
{ price: 340, date: '2019-12-25T06:04:23.907Z' },
{ price: -300, date: '2020-01-25T14:18:46.235Z' },
{ price: -20, date: '2020-02-05T16:33:06.386Z' },
{ price: 50, date: '2020-04-10T14:43:26.374Z' },
{ price: 400, date: '2020-06-25T18:49:59.371Z' },
{ price: -460, date: '2020-07-26T12:01:20.894Z' },
],
interestRate: 0.7,
pin: 3333,
currency: 'EUR',
exchangeRate: 0.92,
locale: 'en-GB',
};
const account4 = {
owner: 'Sarah Smith',
movements: [
{ price: 430, date: '2019-11-01T13:15:33.035Z' },
{ price: 1000, date: '2019-11-30T09:48:16.867Z' },
{ price: 700, date: '2019-12-25T06:04:23.907Z' },
{ price: 50, date: '2020-01-25T14:18:46.235Z' },
{ price: 90, date: '2020-02-05T16:33:06.386Z' },
],
interestRate: 1,
pin: 4444,
currency: 'USD',
exchangeRate: 1,
locale: 'en-US',
};
const account5 = {
owner: 'Masen Mohseni',
movements: [
{ price: 600, date: '2024-08-01T14:43:26.374Z' },
{ price: -700, date: '2024-08-02T18:49:59.371Z' },
{ price: -800, date: '2024-08-03T12:01:20.894Z' },
{ price: 900, date: '2024-08-04T23:15:03.782Z' },
{ price: 1000, date: '2024-08-05T16:20:33.363Z' },
],
interestRate: 1.5,
pin: 5555,
currency: 'IRR',
exchangeRate: 42105,
locale: 'fa-IR',
};
const accounts = [account1, account2, account3, account4, account5];
//! HTML Elements
const labelWelcome = document.querySelector('.welcome');
const labelDate = document.querySelector('.date');
const labelBalance = document.querySelector('.balance__value');
const labelSumIn = document.querySelector('.summary__value--in');
const labelSumOut = document.querySelector('.summary__value--out');
const labelSumInterest = document.querySelector('.summary__value--interest');
const labelTimer = document.querySelector('.timer');
const labelTime = document.querySelector('.time');
const labelLoan = document.querySelector('.loan-label');
const containerApp = document.querySelector('.app');
const containerMovements = document.querySelector('.movements');
const btnLogin = document.querySelector('.login__btn');
const btnTransfer = document.querySelector('.form__btn--transfer');
const btnLoan = document.querySelector('.form__btn--loan');
const btnClose = document.querySelector('.form__btn--close');
const btnSort = document.querySelector('.btn--sort');
const inputLoginUsername = document.querySelector('.login__input--user');
const inputLoginPin = document.querySelector('.login__input--pin');
const inputTransferTo = document.querySelector('.form__input--to');
const inputTransferAmount = document.querySelector('.form__input--amount');
const inputLoanAmount = document.querySelector('.form__input--loan-amount');
const inputCloseUsername = document.querySelector('.form__input--user');
const inputClosePin = document.querySelector('.form__input--pin');
//! Variables
let currentAccount,
timer,
sorted = false;
//! Functons
//* IIFE setInterval function for running current time
(function () {
setInterval(function () {
const now = new Date();
labelTime.textContent = new Intl.DateTimeFormat(navigator.language, {
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
}).format(now);
}, 1000);
})();
//* IIFE function for computing usernames from account owners's name
(function (accounts = []) {
accounts.forEach(acc => {
acc.username = acc.owner
.toLowerCase()
.split(' ')
.map(word => word[0])
.join('');
});
})(accounts);
// * function for reset logout timer
const resetLogoutTimer = function () {
//* set timer in minutes
let totalSeconds = 120,
min,
second;
const tick = function () {
min = String(Math.trunc(totalSeconds / 60)).padStart(2, 0);
second = String(totalSeconds % 60).padStart(2, 0);
labelTimer.textContent = `${min}:${second}`;
if (totalSeconds === 0) {
clearInterval(timer);
containerApp.style.opacity = 0;
labelWelcome.textContent = 'Log in to get started';
}
totalSeconds--;
};
tick();
const timer = setInterval(tick, 1000);
return timer;
};
//* function for calculating days passed of a movement
const formatDate = function (acc = {}, isoDate = new Date(0).toISOString()) {
const calcDaysPassed = (date1, date2) =>
Math.round(Math.abs(date1 - date2) / (1000 * 60 * 60 * 24));
const date = new Date(isoDate);
const now = new Date();
const daysPassed = calcDaysPassed(date, now);
if (daysPassed === 0) return 'Today';
if (daysPassed === 1) return 'Yesterday';
if (daysPassed < 7) return `${daysPassed} Days Ago`;
//* setting options for date
const options = {
day: '2-digit',
month: '2-digit',
year: 'numeric',
};
//* return formatted date
return new Intl.DateTimeFormat(acc.locale, options).format(date);
};
//* function for calculating currect format of movements
const formatPrice = function (acc = {}, value = 0) {
const options = {
style: 'currency',
currency: acc.currency,
minimumFractionDigits: 2,
maximumFractionDigits: 2,
};
return new Intl.NumberFormat(acc.locale, options).format(
value * acc.exchangeRate
);
};
//* function for displaying movements of an account on screen
const displayMovements = function (account = {}, sort = false) {
let html, type, displayedDate, displayedMov;
containerMovements.innerHTML = '';
const movs = sort
? account.movements.slice().sort((a, b) => a.price - b.price)
: account.movements;
movs.forEach(function (mov, i) {
type = mov.price > 0 ? 'deposit' : 'withdrawal';
displayedDate = formatDate(account, mov.date);
displayedMov = formatPrice(account, mov.price);
html = `
<div class="movements__row">
<div class="movements__type movements__type--${type}">${
i + 1
} ${type}</div>
<div class="movements__date">${displayedDate}</div>
<div class="movements__value">${displayedMov}</div>
</div>
`;
containerMovements.insertAdjacentHTML('afterbegin', html);
});
};
//* function for displaying final balance
const calcDisplayBalance = function (account = {}) {
account.balance = account.movements
.map(mov => mov.price)
.reduce((sum, prc) => sum + prc, 0);
labelBalance.textContent = formatPrice(account, account.balance);
};
//* function for displaying summary
const calcDisplaySummary = function (account = {}) {
const income = account.movements
.map(mov => mov.price)
.filter(prc => prc > 0)
.reduce((sum, dep) => sum + dep, 0);
labelSumIn.textContent = formatPrice(account, income);
const outlet = account.movements
.map(mov => mov.price)
.filter(prc => prc < 0)
.reduce((sum, wit) => sum + wit, 0);
labelSumOut.textContent = formatPrice(account, Math.abs(outlet));
const rate = account.interestRate / 100;
const interest = account.movements
.map(mov => mov.price)
.filter(prc => prc > 0)
.map(dep => dep * rate)
.filter(int => int >= 1)
.reduce((sum, int) => sum + int, 0);
labelSumInterest.textContent = formatPrice(account, interest);
};
//* function for updating UI
const updateUI = function (account = {}) {
//* Displaying Current Balance
calcDisplayBalance(account);
//* Displaying Movements
displayMovements(account);
//* Displaying Summary
calcDisplaySummary(account);
};
//! Main
//* Adding Event Listener to Login Button
btnLogin.addEventListener('click', function (e) {
//* Prevent form from refresh after submit
e.preventDefault();
//* find account by user input
currentAccount = accounts.find(
acc => acc.username === inputLoginUsername.value
);
//* setting options for login date
const date = new Date();
const options = {
day: '2-digit',
month: '2-digit',
year: 'numeric',
hour: '2-digit',
minute: '2-digit',
};
//* check password and login
if (currentAccount?.pin === +inputLoginPin.value) {
//* set label date to formatted date
labelDate.textContent = new Intl.DateTimeFormat(
currentAccount.locale,
options
).format(date);
//* displaying user wellcome message
labelWelcome.textContent = `Welcome Back, ${currentAccount.owner
.split(' ')
.at(0)}`;
//* desplaying main UI
containerApp.style.opacity = '1';
//* Clear Fields
inputLoginUsername.value = inputLoginPin.value = '';
inputLoginPin.blur();
//* reset timer
if (timer) clearInterval(timer);
timer = resetLogoutTimer();
//* Update UI
updateUI(currentAccount);
}
});
//* Adding Event Listener to Transfer Button
btnTransfer.addEventListener('click', function (e) {
e.preventDefault();
const transferAccount = accounts.find(
acc => acc.username === inputTransferTo.value
);
const transferAmount = +inputTransferAmount.value;
//* check conditions
if (
transferAccount &&
transferAccount.username !== currentAccount.username &&
transferAmount > 0 &&
transferAmount <= currentAccount.balance
) {
currentAccount.movements.push({
price: -transferAmount,
date: new Date().toISOString(),
});
transferAccount.movements.push({
price: transferAmount,
date: new Date().toISOString(),
});
//* reset timer
if (timer) clearInterval(timer);
timer = resetLogoutTimer();
updateUI(currentAccount);
}
//* clear the inputs
inputTransferTo.value = inputTransferAmount.value = '';
});
//* Adding Event Listener to Close Account Button
btnClose.addEventListener('click', function (e) {
e.preventDefault();
//* check for currect credential
if (
currentAccount.username === inputCloseUsername.value &&
currentAccount.pin === +inputClosePin.value
) {
//* delete account
const index = accounts.findIndex(
acc => acc.username === currentAccount.username
);
accounts.splice(index, 1);
//* Hide UI
containerApp.style.opacity = 0;
labelWelcome.textContent = 'Log in to get started';
}
//* clear the inputs
inputCloseUsername.value = inputClosePin.value = '';
});
//* Adding Event Listener to Loan Button
btnLoan.addEventListener('click', function (e) {
e.preventDefault();
const amount = Math.floor(inputLoanAmount.value);
//* check the conditions for getting loan
if (
amount > 0 &&
currentAccount.movements
.map(mov => mov.price)
.some(prc => Math.abs(prc) > amount * 0.1)
) {
labelLoan.textContent = 'Please Wait...';
//* setting Timeout for requesting loan
setTimeout(function () {
//* add loan to movements
currentAccount.movements.push({
price: amount,
date: new Date().toISOString(),
});
//* reset timer
if (timer) clearInterval(timer);
timer = resetLogoutTimer();
//* update UI
updateUI(currentAccount);
//* change label
labelLoan.textContent = 'Request loan';
}, 3000);
}
inputLoanAmount.value = '';
});
//* Adding Event Listener to Sort Button
btnSort.addEventListener('click', function (e) {
e.preventDefault();
//* sort and unsort movements with boolean currentAccount.sorted
displayMovements(currentAccount, !sorted);
sorted = !sorted;
});