-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserAPI.js
More file actions
50 lines (35 loc) · 1.3 KB
/
UserAPI.js
File metadata and controls
50 lines (35 loc) · 1.3 KB
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
export default class User {
constructor( firstName, lastName, dateOfBirth, password ) {
this.firstName = firstName;
this.lastName = lastName;
this.dateOfBirth = dateOfBirth;
this.password = password;
}
static getAllUsers() {
const usersArray = localStorage.getItem('users') ?
JSON.parse(localStorage.getItem('users')) : [];
return usersArray
}
static saveUser(userToSave) {
const users = this.getAllUsers();
userToSave.id = Math.trunc(Math.random() * 1000000)
users.push(userToSave);
localStorage.setItem("users", JSON.stringify(users))
}
static deleteUser( userIndex ) {
const savedUsers = this.getAllUsers();
const IndexInSaved = savedUsers.length - (userIndex + 1);
savedUsers.splice( IndexInSaved, 1 );
localStorage.setItem("users", JSON.stringify(savedUsers));
}
_calcAge() {
const dob = this.dateOfBirth;
const Bday = +new Date((dob));
if (dob == 0){ console.log("dob is 0") }
return ~~((Date.now() - Bday) / 31557600000);
}
_passwordCheck() {
const retypePassword = document.getElementById("re-password");
return this.password === retypePassword.value
}
}