-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
171 lines (150 loc) · 4.48 KB
/
index.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
const p = document.querySelector("#para");
const btn = document.querySelector("#generate-btn");
const refreshBtn = document.querySelector("#refresh-btn");
const userFullName = document.querySelector(".user-full-name");
const userProfession = document.querySelector(".user-occupation");
const userImg = document.querySelector("#user-avatar");
const userEmailEl = document.querySelector(".user-email");
const userIdEl = document.querySelector('.user-id')
const modalContainer = document.querySelector("#modal-container");
const card = document.querySelector(".card");
const usersNames = [
"Amir",
"Ivan",
"Jorg",
"Kevin",
"John",
"Mustafa",
"Adrian",
];
const usersLastNames = [
"Doe",
"Boe",
"Coe",
"Koe",
"Joe",
"Hoe",
"Loe",
"Goe",
"Poe",
];
const usersOccupation = [
"doctor",
"driver",
"lawyer",
"teacher",
"houskeeper",
"architect",
"backer",
"sales manager",
"artist",
"musician",
"pilot",
"constructor",
"engineer",
"dentist",
"pharmasist",
"programmer",
"gamer",
"youtuber",
"writer",
"painter",
"web designer",
];
// generates a random number for userId
const generateUserID = () => Math.floor(Math.random() * 100000);
// gets random item from array
const getRandomItemFromArray = (arr) =>
arr[Math.floor(Math.random() * arr.length)];
// generates a user email
const generateUserEmail = (firstName, lastName) => {
return `${firstName.toLowerCase()}.${lastName.toLowerCase()}@mail.com`;
};
// generates random avatar for user
const generateAvatar = () => {
const name = getRandomItemFromArray(usersNames);
const url = `https://avatars.dicebear.com/api/miniavs/${name}.svg`;
return (userImg.src = url);
};
// generates a random User and displays it to the screen
generateRandomUser = () => {
const firstName = getRandomItemFromArray(usersNames);
const lastName = getRandomItemFromArray(usersLastNames);
return {
userFirstName: firstName,
userLastName: lastName,
userId: generateUserID(),
userEmail: generateUserEmail(firstName, lastName),
userOccupation: getRandomItemFromArray(usersOccupation),
userAvatar: generateAvatar(),
};
};
// display user info
const displayUser = (e) => {
e.preventDefault();
const { userFirstName, userLastName, userId, userEmail, userOccupation } =
generateRandomUser();
// new array to store values received from function to be saved to the localstorage
const user = [userFirstName, userLastName, userId, userEmail, userOccupation];
localStorage.setItem("user", user);
const fullName = `${userFirstName} ${userLastName}`;
userFullName.innerHTML = `<i>user name</i>: <strong>${fullName}</strong>`;
userProfession.innerHTML = `<i>profession</i>: <strong>${userOccupation}</strong>`;
userEmailEl.innerHTML = `<i>email</i>: <strong>${userEmail}</strong>`;
userIdEl.innerHTML = `<i>id</i>: <strong>${userId}</strong>`
};
// refreshes the screen
const refreshScreen = (e) => {
e.preventDefault();
localStorage.removeItem("user");
window.location.reload();
};
// show modal
const showModal = () => {
modalContainer.classList.add("is-visible");
};
// hide modal
const hideModal = () => {
modalContainer.classList.remove("is-visible");
};
// modal
const modalDiv = document.createElement("div");
modalDiv.classList.add("modal-content");
// modal title
const modalTitle = document.createElement("h2");
modalTitle.innerText = "User Info";
// modal content
const modalContent = document.createElement("div");
// display data in modal //must be reveised
const modalData = () => {
const { userFirstName, userLastName, userId, userEmail, userOccupation } =
generateRandomUser();
const data = `<ul>
<li>user full name: ${userFirstName} ${userLastName}<li>
<li>user id: ${userId}<li>
<li>user email: ${userEmail}<li>
<li>user profession: ${userOccupation}<li>
</ul>`;
modalContent.innerHTML = data;
};
// modal close btn
const modalCloseBtn = document.createElement("p");
modalCloseBtn.classList.add("modal-close-btn");
modalCloseBtn.innerText = "X";
modalDiv.appendChild(modalTitle);
modalDiv.appendChild(modalContent);
modalDiv.appendChild(modalCloseBtn);
modalContainer.appendChild(modalDiv);
window.addEventListener("keydown", (e) => {
if (e.key === "Escape" && modalContainer.classList.contains("is-visible")) {
hideModal();
}
});
// events
btn.addEventListener("click", displayUser);
refreshBtn.addEventListener("click", refreshScreen);
card.addEventListener("click", () => {
showModal();
modalData();
});
modalCloseBtn.addEventListener("click", hideModal);