-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
291 lines (265 loc) · 8.9 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
'use strict';
/////////////////////////////////////////////////
/////////////////////////////////////////////////
// BANKIST APP
// Data
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',
'2020-05-08T14:11:59.604Z',
'2020-05-27T17:01:17.194Z',
'2021-05-20T23:36:17.929Z',
'2021-05-25T10:51:36.790Z',
],
currency: 'EUR',
locale: 'en-uk', // 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',
'2021-05-25T18:49:59.371Z',
'2021-05-26T12:01:20.894Z',
],
currency: 'USD',
locale: 'en-US',
};
const accounts = [account1, account2];
let currentAccount;
let sort=false;
// various doc elements
const inputLoginSubmit=document.querySelector('.login__btn');
const inputLoginId=document.querySelector('.login__input--user')
const inputLoginPin=document.querySelector('.login__input--pin')
const containerApp=document.querySelector('.app');
const MovementsContainer=document.querySelector('.movements');
const sortBtn=document.querySelector('.btn--sort');
const tranferBtnSubmit=document.querySelector('.form__btn--transfer');
const transferToPerson=document.querySelector('.form__input--to');
const tranferAmount=document.querySelector('.form__input--amount');
const accountBalance=document.querySelector('.balance__value');
const introMsg=document.querySelector('.welcome');
const loanAmount=document.querySelector('.form__input--loan-amount');
const loanSubmit=document.querySelector('.form__btn--loan');
const moneyIn=document.querySelector('.summary__value--in');
const moneyOut=document.querySelector('.summary__value--out');
const IntrestMoney=document.querySelector('.summary__value--interest');
const closeAccountUser=document.querySelector('.form__input--user');
const closeAccountUserPin=document.querySelector('.form__input--pin');
const closeSubmit=document.querySelector('.form__btn--close');
const loginDate=document.querySelector('.date');
const labelTimer=document.querySelector('.timer')
// UI UPDATE
function updateUI(currentAccount,acc=accounts){
displayMovments(currentAccount);
calculateBalance(currentAccount);
displayMoneySummary(currentAccount);
// SetlocalStorage(acc);
if(timeToLogout) clearInterval(timeToLogout);
timeToLogout=startLogoutTimer();
}
// USERNAME
accounts.forEach(acc=>{
const name=acc.owner.toLowerCase().split(' ')
acc.userName=name[0][0]+name[1][0];
})
// DATE
function date(time){
const timePassed=new Date(time);
const dateOn=String(timePassed.getDate()).padStart(2,'0');
const monthOn=String((timePassed.getMonth()+1)).padStart(2,'0');
const yearOn=timePassed.getFullYear();
function calcDays(time){
return Math.floor((new Date()-timePassed)/(1000*60*60*24));
}
const daysPassed= calcDays(time);
if(daysPassed==0) return 'Today';
else if(daysPassed==1) return 'Yesterday';
else if(daysPassed>1 && daysPassed<=7) return `${daysPassed} days ago`
else return `${dateOn}/${monthOn}/${yearOn}`
}
// LOGIN DATE
const options={
hour:'numeric',
minute:'numeric',
day:'numeric',
month:'numeric',
year: 'numeric',
};
function login(currentAccount){
const now=new Date();
const locale=currentAccount.locale;
const format=Intl.DateTimeFormat(locale,options).format(now);
loginDate.textContent=`${format}`
}
// LOCAL STORAGE
function SetlocalStorage(accounts){
accounts.forEach(acc=>{
localStorage.setItem(`${acc.owner}`,JSON.stringify(acc));
})
}
function getLocalStorage(acc){
const owner=JSON.parse(localStorage.getItem(`${acc.owner}`));
return owner;
}
// LOGOUT TIMER
let timeToLogout;
// there was a problem that it gets called first time after 1 sec so alag function
function startLogoutTimer(){
// set time to 5 minutes
// set intrval call timer evrysecond
function tick(){
const min=String(Math.floor(time/60)).padStart(2,'0');
const sec=String(time%60).padStart(2,'0');
labelTimer.textContent=`${min}:${sec}`;
if(time===0) {
clearInterval(timeToLogout);
containerApp.style.opacity='0%';
introMsg.textContent='Login to get started';
}
time--;
}
let time=10;
tick();
timeToLogout=setInterval(tick, 1000);
return timeToLogout;
}
// NOTE SORT MI DATES CHANGE
console.log(accounts);
// DISPLAY MOVEMENTS
function displayMovments(account,sort=false){
MovementsContainer.innerHTML='';
const movs= sort? account.movements.slice().sort((a,b)=>a-b):account.movements
console.log(`Movements = ${account.movements}`);
movs.forEach((mov,i)=>{
const type=mov>0? 'deposit':'withdrawal';
const html=`<div class="movements__row">
<div class="movements__type movements__type--${type}">${i+1} ${type}</div>
<div class="movements__date">${date(currentAccount.movementsDates[account.movements.findIndex(mo=> mo===mov)])}</div>
<div class="movements__value">${mov}€</div>
</div>`
MovementsContainer.insertAdjacentHTML('afterbegin',html);
})
}
// CALCULATE BALANCE
function calculateBalance(account){
const balance=account.movements.reduce((acc,curr)=>acc+curr,0);
console.log(balance);
accountBalance.textContent=`${balance.toFixed(2)}€`
}
function calcIn(account){
moneyIn.textContent=`${account.movements.filter(mov=>mov>0).reduce((acc,cur)=>acc+cur,0).toFixed(2)}€`
}
function calcOut(account){
moneyOut.textContent=`${account.movements.filter(mov=>mov<0).reduce((acc,cur)=>acc+cur,0).toFixed(2)}€`
}
function calcIntrest(account){
const intrest=account.movements.filter(mov=>mov>0).reduce((acc,cur)=>acc+cur,0)*0.1;
IntrestMoney.textContent=`${intrest.toFixed(2)}€`
}
function displayMoneySummary(account){
calcIn(account);
calcOut(account);
calcIntrest(account);
}
// LOGIN AND OTHER EVENT LISTNERS
inputLoginSubmit.addEventListener('click',function(e){
e.preventDefault();
currentAccount=accounts.find(acc=>acc.userName===inputLoginId.value);
if(!currentAccount) return ;
if(currentAccount.pin===+inputLoginPin.value){
containerApp.style.opacity='1';
inputLoginId.value=inputLoginPin.value='';
inputLoginPin.blur();
// currentAccount=getLocalStorage(currentAccount);
updateUI(currentAccount);
console.log(currentAccount);
login(currentAccount);
introMsg.textContent=`Welcome back, ${currentAccount.owner.split(' ')[0]}`
}
else{
alert('Account not found');
inputLoginId.value=inputLoginPin.value='';
inputLoginPin.blur();
}
})
sortBtn.addEventListener('click',function(){
sort=!sort;
displayMovments(currentAccount,sort);
})
tranferBtnSubmit.addEventListener('click',function(e){
e.preventDefault();
const reciverPerson=accounts.find(acc=>acc.userName===transferToPerson.value);
if(!reciverPerson){
alert(`Invalid Credentials`);
tranferAmount.value=transferToPerson.value='';
tranferAmount.blur();
return ;
}
if(currentAccount.movements.reduce((acc,curr)=>acc+curr,0)>(+tranferAmount.value) && (reciverPerson.owner!==currentAccount.owner)){
console.log(typeof tranferAmount.value);
reciverPerson.movements.push(+tranferAmount.value);
currentAccount.movements.push(-(Number(tranferAmount.value)));
currentAccount.movementsDates.push(new Date().toISOString());
reciverPerson.movementsDates.push(new Date().toISOString());
console.log(currentAccount);
tranferAmount.value=transferToPerson.value='';
tranferAmount.blur();
updateUI(currentAccount);
console.log(accounts);
}
else{
alert('Insufficent balance');
tranferAmount.value=transferToPerson.value='';
tranferAmount.blur();
}
})
loanSubmit.addEventListener('click',function(e){
e.preventDefault();
if(currentAccount.movements.filter(mov=>mov>0).some(mov=>mov>0.1*(+loanAmount.value))){
setTimeout(()=>{
console.log(typeof +loanAmount.value);
currentAccount.movements.push(+loanAmount.value);
currentAccount.movementsDates.push(new Date().toISOString());
loanAmount.value='';
loanAmount.blur();
updateUI(currentAccount);
},2500)
}
else{
alert('Loan Not Approved')
loanAmount.value='';
loanAmount.blur();
}
})
closeSubmit.addEventListener('click',function(e){
e.preventDefault();
if(closeAccountUser.value===currentAccount.userName && closeAccountUserPin.value==currentAccount.pin){
const index=accounts.findIndex(acc=>closeAccountUser.value===acc.userName);
console.log(index);
accounts.splice(index,1);
containerApp.style.opacity='0';
closeAccountUserPin.value=closeAccountUser.value=''
closeAccountUserPin.blur();
}
else{
alert('Wrong details');
closeAccountUserPin.value=closeAccountUser.value=''
closeAccountUserPin.blur();
}
})