-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
347 lines (295 loc) · 10.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
'use strict';
/////////////////////////////////////////////////
/////////////////////////////////////////////////
// BANKIST APP
/////////////////////////////////////////////////
// Data
// DIFFERENT DATA! Contains movement dates, currency and locale
const account1 = {
owner: 'Jonas Schmedtmann',
movements: [200, 455.23, -306.5, 25000, -642.21, -133.9, 79.97, 1300],
interestRate: 1.2, // %
pin: 1111,
movementsDates: [
'2019-11-18T21:31:17.178Z',
'2019-12-23T07:42:02.383Z',
'2020-01-28T09:15:04.904Z',
'2020-04-01T10:17:24.185Z',
'2022-11-20T14:11:59.604Z',
'2022-11-16T17:01:17.194Z',
'2022-11-18T23:36:17.929Z',
'2022-11-21T10:51:36.790Z',
],
currency: 'EUR',
locale: 'pt-PT', // de-DE
};
const account2 = {
owner: 'Jessica Davis',
movements: [5000, 3400, -150, -790, -3210, -1000, 8500, -30],
interestRate: 1.5,
pin: 2222,
movementsDates: [
'2019-11-01T13:15:33.035Z',
'2019-11-30T09:48:16.867Z',
'2019-12-25T06:04:23.907Z',
'2020-01-25T14:18:46.235Z',
'2020-02-05T16:33:06.386Z',
'2020-04-10T14:43:26.374Z',
'2020-06-25T18:49:59.371Z',
'2020-07-26T12:01:20.894Z',
],
currency: 'USD',
locale: 'en-US',
};
const accounts = [account1, account2];
/////////////////////////////////////////////////
// Elements
// 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 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');
/////////////////////////////////////////////////
/////////////////////////////////////////////////
// LECTURES
const currencies = new Map([
['USD', 'United States dollar'],
['EUR', 'Euro'],
['GBP', 'Pound sterling'],
]);
const movements = [200, 450, -400, 3000, -650, -130, 70, 1300];
const formatMovementDate = function (typeDate, locale) {
const calcDaysPassed = (date1, date2) =>
Math.round(Math.abs(date1 - date2) / (1000 * 24 * 60 * 60));
const daysPassed = calcDaysPassed(new Date(), new Date(typeDate));
if (daysPassed === 0) return 'Today';
if (daysPassed === 1) return 'Yesterday';
if (daysPassed <= 7) return `${daysPassed} days ago`;
// const now = new Date(typeDate); //the date on which exchange was done
// const date = `${now.getDate()}`.padStart(2, '0');
// const month = `${now.getMonth() + 1}`.padStart(2, '0');
// const year = now.getFullYear();
// return `${date}/${month}/${year}`;
return new Intl.DateTimeFormat(locale).format(typeDate);
};
const formatCurr = function (amount, locale, currency) {
const option = {
style: 'currency',
currency,
// currency: currency,
};
return new Intl.NumberFormat(locale, option).format(amount);
};
const displayMovements = function (acc, sort = false) {
containerMovements.innerHTML = '';
const movs = sort
? acc.movements.slice().sort((a, b) => a - b)
: acc.movements;
movs.forEach(function (amount, i) {
const type = amount > 0 ? 'deposit' : 'withdrawal';
const typeDate = new Date(acc.movementsDates[i]);
const displayDate = formatMovementDate(typeDate, acc.locale);
const html = `
<div class="movements__row">
<div class="movements__type movements__type--${type}">${i + 1} ${type}</div>
<div class="movements__date">${displayDate}</div>
<div class="movements__value">${formatCurr(
amount,
acc.locale,
acc.currency
)}</div>
</div>
`;
containerMovements.insertAdjacentHTML('afterbegin', html);
});
};
const calcDisplayBalance = function (acc) {
acc.balance = acc.movements.reduce(
(accumulator, currentElement, index) => accumulator + currentElement,
0
);
labelBalance.textContent = formatCurr(acc.balance, acc.locale, acc.currency);
};
const calcDisplaySummary = function (acc) {
const incomes = acc.movements
.filter(mov => mov > 0)
.reduce((acc, mov) => acc + mov, 0);
labelSumIn.textContent = formatCurr(incomes, acc.locale, acc.currency);
const out = acc.movements
.filter(mov => mov < 0)
.reduce((acc, mov) => acc + Math.abs(mov), 0);
labelSumOut.textContent = formatCurr(out, acc.locale, acc.currency);
const interest = acc.movements
.filter(mov => mov > 0)
.map(mov => (mov * acc.interestRate) / 100)
.filter(mov => mov >= 1) //exclude less than 1 interest value
.reduce((acc, int) => acc + int, 0);
labelSumInterest.textContent = formatCurr(interest, acc.locale, acc.currency);
};
const createUserNames = function (accs) {
accs.forEach(function (acc) {
acc.username = acc.owner
.toLowerCase()
.split(' ')
.map(function (word) {
return word[0];
})
.join('');
});
};
createUserNames(accounts);
const updateUI = function (acc) {
//display balance
calcDisplayBalance(acc);
//display summary
calcDisplaySummary(acc);
//display movements
displayMovements(acc);
};
const startLogOutTimer = function () {
const tick = function () {
const min = String(Math.trunc(timeLimit / 60)).padStart(2, '0');
const sec = String(timeLimit % 60).padStart(2, '0');
//update the time in ui each second
labelTimer.textContent = `${min}:${sec}`;
//on zero log out
if (timeLimit === 0) {
clearInterval(timer);
containerApp.style.opacity = 100;
labelWelcome.textContent = `Welcome back, ${
currentAccount.owner.split(' ')[0]
}`;
}
//decrease time limit every sec
timeLimit--;
};
//time limit
let timeLimit = 100;
//display time
tick();
const timer = setInterval(tick, 1000);
return timer;
};
let currentAccount, timer;
// currentAccount = account1;
// updateUI(currentAccount);
// containerApp.style.opacity = 100;
btnLogin.addEventListener('click', function (e) {
e.preventDefault();
//fetching account
currentAccount = accounts.find(
acc => acc.username === inputLoginUsername.value
); // inputLoginUsername.inputLoginPin;
//check credentials
if (currentAccount?.pin === Number(inputLoginPin.value)) {
//display welcome message
labelWelcome.textContent = `Log in to get started`;
//display date
// const now = new Date();
// const date = `${now.getDate()}`.padStart(2, '0');
// const month = `${now.getMonth() + 1}`.padStart(2, '0');
// const year = now.getFullYear();
// const hours = `${now.getHours()}`.padStart(2, '0');
// const mins = `${now.getMinutes()}`.padStart(2, '0');
// labelDate.textContent = `${date}/${month}/${year}, ${hours}:${mins}`;
const now = new Date();
labelDate.textContent = `${new Intl.DateTimeFormat(
currentAccount.locale
).format(now)}`;
//delete input info
inputLoginUsername.value = inputLoginPin.value = ''; // similar to: a=b=c;
//display ui
containerApp.style.opacity = 100;
//starting timer
if (timer) clearInterval(timer);
timer = startLogOutTimer();
//updating UI
updateUI(currentAccount);
}
});
btnTransfer.addEventListener('click', function (e) {
e.preventDefault(); //to avoid the bydefault reloading of form bcz we are using form tag
const reciever = accounts.find(acc => acc.username === inputTransferTo.value);
const transferAmount = Number(inputTransferAmount.value);
//clear input info
inputTransferAmount.value = inputTransferTo.value = '';
if (
transferAmount > 0 &&
currentAccount.balance >= transferAmount &&
reciever &&
currentAccount.username !== reciever.username
) {
//doing the transfer
currentAccount.movements.push(-transferAmount);
reciever.movements.push(transferAmount);
currentAccount.movementsDates.push(new Date().toISOString());
reciever.movementsDates.push(new Date().toISOString());
//updating the ui
updateUI(currentAccount);
//resetting the timer
clearInterval(timer);
timer = startLogOutTimer();
}
});
btnLoan.addEventListener('click', function (e) {
e.preventDefault();
const loanAmount = Math.floor(inputLoanAmount.value);
inputLoanAmount.value = '';
if (
loanAmount > 0 &&
currentAccount.movements.some(mov => mov >= loanAmount * 0.1)
) {
//checking is there any amount which is 10% of loan amount
//giving loan
setTimeout(() => {
currentAccount.movements.push(loanAmount);
currentAccount.movementsDates.push(new Date().toISOString());
//update UI now
updateUI(currentAccount);
//resetting the timer
clearInterval(timer);
timer = startLogOutTimer();
}, 2500);
}
});
btnClose.addEventListener('click', function (e) {
e.preventDefault();
//check credentials
if (
currentAccount.username === inputCloseUsername.value &&
currentAccount.pin === Number(inputClosePin.value)
) {
//clear input fields
inputCloseUsername.value = inputClosePin.value = '';
//access which index element to be deleted using findIndex() method
const deleteIndex = accounts.findIndex(acc => acc.username === confirmUser);
//deleting the element using the index via splice() method
accounts.splice(deleteIndex, 1);
//hide the UI again
containerApp.style.opacity = 0;
}
});
let sorted = false;
btnSort.addEventListener('click', function (e) {
e.preventDefault();
displayMovements(currentAccount.movements, !sorted);
sorted = !sorted;
});
``;