-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
108 lines (82 loc) · 3.39 KB
/
app.js
File metadata and controls
108 lines (82 loc) · 3.39 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
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
'use strict';
const userList = document.getElementById("users");
let counter = 0;
const getProfile = () => {
fetch('https://randomuser.me/api/')
.then(response => response.json())
.then(data => {
// Title
let title = data.results["0"].name.title;
title = title.charAt(0).toUpperCase() + title.slice(1)
// First Name
let firstName = data.results["0"].name.first;
firstName = firstName.charAt(0).toUpperCase() + firstName.slice(1)
// Last Name
let lastName = data.results["0"].name.last;
lastName = lastName.charAt(0).toUpperCase() + lastName.slice(1)
// Profile image
let image = data.results["0"].picture.large;
// Address
let street = data.results["0"].location.street;
let city = data.results["0"].location.city;
let state = data.results["0"].location.state;
let zip = data.results["0"].location.postcode;
let address = street + ", " + city + ", " + state + ", " + zip;
// DOB
let dob = data.results["0"].dob.date;
dob = dob.slice(0, 10);
// Age
let age = data.results["0"].dob.age;
// Email
let email = data.results["0"].email;
// Generate Unique ID
let id = counter;
createUser(title, firstName, lastName, image, email, address, dob, age, id);
counter++;
})
.catch(error => console.log("oops, looks like we got an error: ", error))
.finally(() => console.log("finally, This function always runs..."))
}
const createUser = (title, firstName, lastName, image, email, address, dob, age, id) => {
const profileBox = document.createElement("div");
const nameLabel = document.createElement("div");
const userImage = document.createElement("img");
const moreBtn = document.createElement("button");
const container = document.createElement("div");
const fullName = document.createElement("div");
const cAddress = document.createElement("div");
const birth = document.createElement("div");
const personAge = document.createElement("div");
const eAddress = document.createElement("div");
const index = id;
userList.setAttribute("style", "display:inline-block; margin:3%;")
nameLabel.innerText = `${title}, ${lastName}`;
userImage.setAttribute("src", image);
moreBtn.innerText = " Full Information";
moreBtn.setAttribute("onClick", `displayMore(${index})`);
container.setAttribute("class", "more-info");
container.setAttribute("style", "display:none;");
fullName.innerText = `${"Full Name: " + firstName + ", " + lastName}`;
cAddress.innerText = `${"Full Address: " + address}`;
birth.innerText = `${"DOB: " + dob}`;
personAge.innerText = `${"Age: " + age}`;
eAddress.innerText = `${"Email Address: " + email}`;
profileBox.appendChild(nameLabel);
profileBox.appendChild(userImage);
profileBox.appendChild(moreBtn);
moreBtn.appendChild(container)
container.appendChild(fullName);
container.appendChild(cAddress);
container.appendChild(birth);
container.appendChild(personAge);
container.appendChild(eAddress);
userList.appendChild(profileBox);
}
//Hellper method to toggle each `Full Information` button
function displayMore(id) {
if (document.getElementsByClassName("more-info")[id].style.display === "none") {
document.getElementsByClassName("more-info")[id].style.display = "inline-block";
} else {
document.getElementsByClassName("more-info")[id].style.display = "none";
}
}