generated from bloominstituteoftechnology/W_S5_Challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
137 lines (117 loc) · 5.16 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
//const { default: axios } = require("axios")
async function sprintChallenge5() { // Note the async keyword so you can use `await` inside sprintChallenge5
// 👇 WORK ONLY BELOW THIS LINE 👇
// 👇 WORK ONLY BELOW THIS LINE 👇
// 👇 WORK ONLY BELOW THIS LINE 👇
// 👇 ==================== TASK 1 START ==================== 👇
// 🧠 Use Axios to GET learners and mentors.
// ❗ Use the variables `mentors` and `learners` to store the data.
// ❗ Use the await keyword when using axios.
let mentors = [] // fix this
await axios.get('http://localhost:3003/api/mentors').then(res=>{
mentors.push(...res.data)
})
let learners = [] // fix this
await axios.get('http://localhost:3003/api/learners').then(res=>{
learners.push(...res.data)
})
//console.log(learners)
//console.log(mentors)
// 👆 ==================== TASK 1 END ====================== 👆
// 👇 ==================== TASK 2 START ==================== 👇
// 🧠 Combine learners and mentors.
// ❗ At this point the learner objects only have the mentors' IDs.
// ❗ Fix the `learners` array so that each learner ends up with this exact structure:
// {
// id: 6,
// fullName: "Bob Johnson",
// email: "[email protected]",
// mentors: [
// "Bill Gates",
// "Grace Hopper"
// ]`
// }
learners.forEach(person=>{
person.mentors=person.mentors.map(mentorname=>mentors.find(mentor=>mentor.id===mentorname))
})
// 👆 ==================== TASK 2 END ====================== 👆
const cardsContainer = document.querySelector('.cards')
const info = document.querySelector('.info')
info.textContent = 'No learner is selected'
// 👇 ==================== TASK 3 START ==================== 👇
for (let learner of learners) { // looping over each learner object
// 🧠 Flesh out the elements that describe each learner
// ❗ Give the elements below their (initial) classes, textContent and proper nesting.
// ❗ Do not change the variable names, as the code that follows depends on those names.
// ❗ Also, loop over the mentors inside the learner object, creating an <li> element for each mentor.
// ❗ Fill each <li> with a mentor name, and append it to the <ul> mentorList.
// ❗ Inspect the mock site closely to understand what the initial texts and classes look like!
const card = document.createElement('div')
card.className='card'
const heading = document.createElement('h3')
heading.textContent=learner.fullName
card.appendChild(heading)
const email = document.createElement('div')
email.className='email'
card.appendChild(email)
email.textContent=learner.email
const mentorsHeading = document.createElement('h4')
mentorsHeading.classList.add('closed')
card.appendChild(mentorsHeading)
mentorsHeading.textContent='Mentors'
const mentorsList = document.createElement('ul')
learner.mentors.forEach(men=>{
let mentorre=document.createElement('li')
mentorre.textContent=`${men.firstName} ${men.lastName}`
mentorsList.appendChild(mentorre)
})
// 👆 ==================== TASK 3 END ====================== 👆
// 👆 WORK ONLY ABOVE THIS LINE 👆
// 👆 WORK ONLY ABOVE THIS LINE 👆
// 👆 WORK ONLY ABOVE THIS LINE 👆
card.appendChild(mentorsList)
card.dataset.fullName = learner.fullName
cardsContainer.appendChild(card)
card.addEventListener('click', evt => {
const mentorsHeading = card.querySelector('h4')
// critical booleans
const didClickTheMentors = evt.target === mentorsHeading
const isCardSelected = card.classList.contains('selected')
// do a reset of all learner names, selected statuses, info message
document.querySelectorAll('.card').forEach(crd => {
crd.classList.remove('selected')
crd.querySelector('h3').textContent = crd.dataset.fullName
})
info.textContent = 'No learner is selected'
// conditional logic
if (!didClickTheMentors) {
// easy case, no mentor involvement
if (!isCardSelected) {
// selecting the card:
card.classList.add('selected')
heading.textContent += `, ID ${learner.id}`
info.textContent = `The selected learner is ${learner.fullName}`
}
} else {
// clicked on mentors, we toggle and select no matter what
card.classList.add('selected')
if (mentorsHeading.classList.contains('open')) {
mentorsHeading.classList.replace('open', 'closed')
} else {
mentorsHeading.classList.replace('closed', 'open')
}
if (!isCardSelected) {
// if card was not selected adjust texts
heading.textContent += `, ID ${learner.id}`
info.textContent = `The selected learner is ${learner.fullName}`
}
}
})
}
const footer = document.querySelector('footer')
const currentYear = new Date().getFullYear()
footer.textContent = `© BLOOM INSTITUTE OF TECHNOLOGY 2023`
}
// ❗ DO NOT CHANGE THIS CODE. WORK ONLY INSIDE TASKS 1, 2, 3
if (typeof module !== 'undefined' && module.exports) module.exports = { sprintChallenge5 }
else sprintChallenge5()