-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
498 lines (427 loc) · 14.3 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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
'use strict';
/////////////////////////////////////////////////////////////
// Data
/////////////////////////////////////////////////////////////
const accounts = [
{
owner: 'Shohanur Rahman',
movements: [2500, 500, -750, 1200, 3200, -1500, 500, 1200, -1750, 1800],
interestRate: 1.5, // %
password: 1234,
movementsDates: [
'2021-11-18T21:31:17.178Z',
'2021-12-23T07:42:02.383Z',
'2022-05-28T09:15:04.904Z',
'2022-07-01T10:17:24.185Z',
'2022-08-08T14:11:59.604Z',
'2022-09-10T17:01:17.194Z',
'2022-10-12T23:36:17.929Z',
'2022-10-15T12:51:31.398Z',
'2022-10-18T19:41:26.190Z',
'2022-10-19T18:11:36.678Z',
],
currency: 'USD',
locale: 'en-US',
},
{
owner: 'Sunerah Binte Ayesha',
movements: [5000, 3400, -150, -790, -3210, -1000, 8500, -300, 1500, -1850],
interestRate: 1.3, // %
password: 5678,
movementsDates: [
'2021-12-11T21:31:17.671Z',
'2021-12-27T07:42:02.184Z',
'2022-01-05T09:15:04.805Z',
'2022-02-14T10:17:24.687Z',
'2022-03-12T14:11:59.203Z',
'2022-09-16T17:01:17.392Z',
'2022-10-11T23:36:17.929Z',
'2022-10-15T12:51:31.398Z',
'2022-10-18T19:41:26.190Z',
'2022-10-19T08:11:36.678Z',
],
currency: 'EUR',
locale: 'en-GB',
},
{
owner: 'Masud Rana Shawon',
movements: [500, 340, -150, -900, -100, -1000, 800, -300, 1500, -500],
interestRate: 1.1, // %
password: 9999,
movementsDates: [
'2021-12-11T21:31:17.671Z',
'2021-12-27T07:42:02.184Z',
'2022-01-05T09:15:04.805Z',
'2022-02-14T10:17:24.687Z',
'2022-03-12T14:11:59.203Z',
'2022-05-16T17:01:17.392Z',
'2022-08-10T23:36:17.522Z',
'2022-10-03T12:51:31.491Z',
'2022-10-19T06:41:26.394Z',
'2022-10-19T08:12:53.276Z',
],
currency: 'BDT',
locale: 'bn-BD',
},
{
owner: 'Hriday Hrisikhes',
movements: [5000, 3400, -1500, -9000, -1000, -1000, 800, -300, 15000, -500],
interestRate: 1.1, // %
password: 1122,
movementsDates: [
'2021-12-11T21:31:17.671Z',
'2021-12-27T07:42:02.184Z',
'2022-01-05T09:15:04.805Z',
'2022-02-14T10:17:24.687Z',
'2022-03-12T14:11:59.203Z',
'2022-05-16T17:01:17.392Z',
'2022-09-30T23:36:17.522Z',
'2022-10-03T12:51:31.491Z',
'2022-10-19T06:41:26.394Z',
'2022-10-20T03:45:36.276Z',
],
currency: 'EUR',
locale: 'en-GB',
},
];
/////////////////////////////////////////////////////////////
// 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 app = 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-username');
const inputLoginPassword = document.querySelector('.login-input-password');
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-username');
const inputClosePassword = document.querySelector('.form-input-password');
/////////////////////////////////////////////////////////////////////
// Update UI
/////////////////////////////////////////////////////////////////////
function updateUI(currentAccount) {
displayMovements(currentAccount);
displaySummary(currentAccount);
displayBalance(currentAccount);
}
/////////////////////////////////////////////////////////////////////
//formatting currency
/////////////////////////////////////////////////////////////////////
function formateCurrency(value, locale, currency) {
return Intl.NumberFormat(locale, {
style: 'currency',
currency: currency,
}).format(value);
}
/////////////////////////////////////////////////////////////////////
// Days calculation
/////////////////////////////////////////////////////////////////////
function formatMoveDate(date, locale) {
const calculateDays = (date1, date2) =>
Math.round(Math.abs(date2 - date1) / (24 * 60 * 60 * 1000));
const daysPassed = calculateDays(new Date(), date);
if (daysPassed === 0) return 'Today';
if (daysPassed === 1) return 'Yesterday';
if (daysPassed <= 7) return `${daysPassed} days ago`;
return new Intl.DateTimeFormat(locale, {
year: 'numeric',
month: 'short',
day: 'numeric',
}).format(date);
}
/////////////////////////////////////////////////////////////////////
// Username
/////////////////////////////////////////////////////////////////////
function createUsernames(accounts) {
accounts.forEach((account) => {
account.username = account.owner
.toLowerCase()
.split(' ')
.map((uNameWord) => uNameWord.at(0))
.join('');
});
}
createUsernames(accounts);
/////////////////////////////////////////////////////////////////////
// Login
/////////////////////////////////////////////////////////////////////
let currentAccount, timer;
btnLogin.addEventListener('click', function (e) {
e.preventDefault();
currentAccount = accounts.find(
(account) => account.username == inputLoginUsername.value
);
if (currentAccount?.password === Number(inputLoginPassword.value)) {
setTimeout(() => {
// Display UI and welcome
labelWelcome.innerHTML = `Welcome back, <span style="color:#42a8ff">${currentAccount.owner
.split(' ')
.at(0)}</span>`;
app.style.opacity = '1';
// Display date and time
const nowTime = new Date();
const options = {
year: 'numeric',
month: 'numeric',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
};
labelDate.textContent = Intl.DateTimeFormat(
currentAccount.locale,
options
).format(nowTime);
// Log out timer
if (timer) clearInterval(timer);
timer = logout();
// Update UI
updateUI(currentAccount);
}, 2000);
} else {
setTimeout(() => {
// Hide UI and warning sms
labelWelcome.innerHTML = `<span style="color:#ff000d">Ops, Login Faild!</span>`;
app.style.opacity = '0';
}, 2000);
}
// Clear fields
inputLoginUsername.value = inputLoginPassword.value = '';
inputLoginPassword.blur();
});
/////////////////////////////////////////////////////////////////////
// Movements
/////////////////////////////////////////////////////////////////////
function displayMovements(account, sort = false) {
containerMovements.innerHTML = '';
const moves = sort
? account.movements.slice(0).sort((a, b) => a - b)
: account.movements;
moves.forEach((move, i) => {
const transType = move > 0 ? 'deposit' : 'withdrawal';
const formatedMove = formateCurrency(
move,
account.locale,
account.currency
);
const date = new Date(account.movementsDates[i]);
const displayDate = formatMoveDate(date, account.locale);
const movementsHtml = `
<div class="movements-row">
<div class="movements-type movements-type-${transType}">${
i + 1
} ${transType}</div>
<div class="movements-date">${displayDate}</div>
<div class="movements-value">${formatedMove}</div>
</div>`;
containerMovements.insertAdjacentHTML('afterbegin', movementsHtml);
});
}
/////////////////////////////////////////////////////////////////////
// Summary
/////////////////////////////////////////////////////////////////////
function displaySummary(account) {
//Incomes
const incomes = account.movements
.filter((move) => move > 0)
.reduce((accu, deposit) => accu + deposit, 0);
labelSumIn.textContent = formateCurrency(
incomes,
account.locale,
account.currency
);
//Outcomes
const outcomes = account.movements
.filter((move) => move < 0)
.reduce((accu, withdrawal) => accu + withdrawal, 0);
labelSumOut.textContent = formateCurrency(
Math.abs(outcomes),
account.locale,
account.currency
);
// Interest
const interest = account.movements
.filter((move) => move > 0)
.map((deposit) => (deposit * account.interestRate) / 100)
.filter((interest) => interest >= 1)
.reduce((accu, interest) => accu + interest);
labelSumInterest.textContent = formateCurrency(
interest,
account.locale,
account.currency
);
}
/////////////////////////////////////////////////////////////////////
// Balance
/////////////////////////////////////////////////////////////////////
function displayBalance(account) {
account.balance = account.movements.reduce((accu, move) => accu + move);
labelBalance.textContent = formateCurrency(
account.balance,
account.locale,
account.currency
);
}
/////////////////////////////////////////////////////////////////////
// Transfer
// /////////////////////////////////////////////////////////////////////
btnTransfer.addEventListener('click', function (e) {
e.preventDefault();
const receiverAccount = accounts.find(
(account) => account.username === inputTransferTo.value
);
const reciverAmount = Number(inputTransferAmount.value);
// Clear fields
inputTransferTo.value = inputTransferAmount.value = '';
inputTransferAmount.blur();
if (
reciverAmount > 0 &&
reciverAmount <= currentAccount.balance &&
currentAccount.username !== receiverAccount?.username &&
receiverAccount
) {
setTimeout(() => {
// Transfer moneyoperation-loan
currentAccount.movements.push(-reciverAmount);
receiverAccount.movements.push(reciverAmount);
// Add current date and time
currentAccount.movementsDates.push(new Date().toISOString());
receiverAccount.movementsDates.push(new Date().toISOString());
// Update UI
updateUI(currentAccount);
// Show message
labelWelcome.innerHTML = `Hurray,<span style="color:#00b79f"> Transaction successful!</span>`;
}, 1500);
// Log out timer
if (timer) clearInterval(timer);
timer = logout();
} else {
setTimeout(() => {
labelWelcome.innerHTML = `<span style="color:#ff000d"> Transaction failed!</span>`;
}, 1500);
// Log out timer
if (timer) clearInterval(timer);
timer = logout();
}
});
// /////////////////////////////////////////////////////////////////////
// // Loan
// /////////////////////////////////////////////////////////////////////
btnLoan.addEventListener('click', function (e) {
e.preventDefault();
const loanAmount = Number(inputLoanAmount.value);
if (
loanAmount > 0 &&
currentAccount.movements.some((move) => move >= loanAmount * 0.1)
) {
setTimeout(() => {
// Add positive movement into current account
currentAccount.movements.push(loanAmount);
//Add current time
currentAccount.movementsDates.push(new Date().toISOString());
//updateUI
updateUI(currentAccount);
// Show message
labelWelcome.innerHTML = `Hurray,<span style="color:#00b79f"> Loan Request successful!</span>`;
}, 1500);
// Log out timer
if (timer) clearInterval(timer);
timer = logout();
} else {
setTimeout(() => {
labelWelcome.innerHTML = `Damn,<span style="color:#ff000d"> Loan Request declined! </span>`;
}, 1500);
// Log out timer
if (timer) clearInterval(timer);
timer = logout();
}
// Clear
inputLoanAmount.value = '';
inputLoanAmount.blur();
});
/////////////////////////////////////////////////////////////////////
// Close account
/////////////////////////////////////////////////////////////////////
btnClose.addEventListener('click', function (e) {
e.preventDefault();
const closeUserName = inputCloseUsername.value;
const closeUserPassowrd = Number(inputClosePassword.value);
if (
currentAccount.username === closeUserName &&
currentAccount.password === closeUserPassowrd
) {
setTimeout(() => {
const accountIndex = accounts.findIndex(
(account) => account.username === currentAccount.username
);
// Delete account
accounts.splice(accountIndex, 1);
// Hide UI
app.style.opacity = 0;
// Show message
labelWelcome.innerHTML = `<span style="color:#00b79f">Account closed successfully </span>${currentAccount.owner
.toLowerCase()
.split(' ')
.at(0)}`;
}, 1500);
// Log out timer
if (timer) clearInterval(timer);
timer = logout();
} else {
setTimeout(() => {
labelWelcome.innerHTML = `<span style="color:#ff000d">Account close can not be done! </span>`;
}, 1500);
// Log out timer
if (timer) clearInterval(timer);
timer = logout();
}
// Clear fields
inputCloseUsername.value = inputClosePassword.value = '';
inputCloseUsername.blur();
inputClosePassword.blur();
});
///////////////////////////////////////////////////////////////////
// Sort
////////////////////////////////////////////////////////////////
let sortedMoves = false;
btnSort.addEventListener('click', function (e) {
e.preventDefault();
setTimeout(() => {
displayMovements(currentAccount, !sortedMoves);
sortedMoves = !sortedMoves;
}, 500);
});
///////////////////////////////////////////////////////////////////
// Timer
////////////////////////////////////////////////////////////////
function logout() {
labelTimer.textContent = '';
let time = 120;
const clock = () => {
const min = String(Math.trunc(time / 60)).padStart(2, 0);
const sec = String(time % 60).padEnd(2, 0);
labelTimer.textContent = `${min}:${sec}`;
if (time === 0) {
clearInterval(timer);
labelWelcome = `<span style="color:#ff000d">You've been logged out! </span>${currentAccount.owner
.toLowerCase()
.split(' ')
.at(0)}`;
}
time--;
};
clock();
timer = setInterval(clock, 1000);
return timer;
}