-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom-user.js
84 lines (81 loc) · 3.5 KB
/
random-user.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
const loadPhotos = () => {
fetch('https://randomuser.me/api/')
.then(res => res.json())
.then(data => displayPhotos(data.results))
.catch(e => console.log(e));
}
loadPhotos();
const displayPhotos = photos => {
console.log(photos.slice(0, 10));
const mainContainer = document.querySelector('.custom-card');
const photoContainer = document.createElement('div');
const detailContainer = document.createElement('div');
mainContainer.classList.add('d-flex', 'flex-column', 'align-items-center', 'mx-auto');
photoContainer.classList.add('photoContainer', 'my-4');
photos.slice(0, 5000).forEach(photo => {
mainContainer.textContent = '';
var string_date = photo.dob.date;
var date = new Date(string_date);
photoContainer.innerHTML = `<img src="${photo.picture.large}">`;
detailContainer.innerHTML =
`
<div class="text-center">
<button onclick="loadModal('${photo.id.name}')" class="btn my-2">More Info</button>
</div>
<ul class="nav nav-tabs d-flex justify-content-center" id="myTab" role="tablist">
<li class="nav-item" role="presentation">
<button class="nav-link active text-white" id="home-tab" data-bs-toggle="tab" data-bs-target="#home" type="button" role="tab" aria-controls="home" aria-selected="true">Name</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link text-white" id="profile-tab" data-bs-toggle="tab" data-bs-target="#profile" type="button" role="tab" aria-controls="profile" aria-selected="false">Country</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link text-white" id="contact-tab" data-bs-toggle="tab" data-bs-target="#contact" type="button" role="tab" aria-controls="contact" aria-selected="false">DOB</button>
</li>
</ul>
<div class="tab-content p-2 text-center" id="myTabContent">
<div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">${photo.name.title} ${photo.name.first} ${photo.name.last}</div>
<div class="tab-pane fade" id="profile" role="tabpanel" aria-labelledby="profile-tab">${photo.location.country}</div>
<div class="tab-pane fade" id="contact" role="tabpanel" aria-labelledby="contact-tab">${date}</div>
</div>
`
mainContainer.appendChild(photoContainer);
mainContainer.appendChild(detailContainer);
})
}
const loadModal = (id) => {
console.log(id);
const modalContainer = document.querySelector('#modal');
modalContainer.textContent = '';
const infoContainer = document.createElement('div');
if (id == "") {
infoContainer.innerHTML =
`
<div id="custom-modal">
<h5>Modal title</h5>
<div class="text-center">
<button onclick="closeModal('none')" class="btn">Close</button>
</div>
<p class="text-center text-white">No Id Exists!</p>
</div>
`
}
else {
infoContainer.innerHTML =
`
<div id="custom-modal">
<h5>Modal title</h5>
<div class="text-center">
<button onclick="closeModal('none')" class="btn">Close</button>
</div>
<p class="text-center text-white">ID: ${id}</p>
</div>
`
}
modalContainer.appendChild(infoContainer);
closeModal('block');
}
function closeModal(displayStyle) {
const hidden = document.getElementById('custom-modal');
hidden.style.display = displayStyle;
}