forked from eliseoeric/is-5600-lab-02
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
146 lines (131 loc) · 5.27 KB
/
app.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
document.addEventListener('DOMContentLoaded', () => {
// note add this inside of our DOMContentLoaded event handler
const stocksData = JSON.parse(stockContent);
const userData = JSON.parse(userContent);
generateUserList(userData, stocksData);
// Register the event listener on the delete button
const deleteButton = document.querySelector('#btnDelete');
const saveButton = document.querySelector('#btnSave');
deleteButton.addEventListener('click', (event) => {
// we don't want the form to submit (since we will lose form state)
event.preventDefault();
// find the index of the user in the data array
const userId = document.querySelector('#userID').value;
const userIndex = userData.findIndex(user => user.id == userId);
// remove the user from the array
userData.splice(userIndex, 1);
// render the user list
generateUserList(userData, stocksData);
});
saveButton.addEventListener('click', (event) => {
// we don't want the form to submit (since we will lose form state)
event.preventDefault();
// find the user object in our data
const id = document.querySelector('#userID').value;
for (let i=0; i<userData.length; i++) {
// found relevant user, so update object at this index and redisplay
if (userData[i].id == id) {
userData[i].user.firstname = document.querySelector('#firstname').value;
userData[i].user.lastname = document.querySelector('#lastname').value;
userData[i].user.address = document.querySelector('#address').value;
userData[i].user.city = document.querySelector('#city').value;
userData[i].user.email = document.querySelector('#email').value;
generateUserList(userData, stocksData);
}
}
});
});
/**
* Loops through the users and renders a ul with li elements for each user
* @param {*} users
*/
function generateUserList(users, stocks) {
// get the list element and for each user create a list item and append it to the list
const userList = document.querySelector('.user-list');
userList.innerHTML = '';
users.map(({user, id}) => {
const listItem = document.createElement('li');
listItem.innerText = user.lastname + ', ' + user.firstname;
listItem.setAttribute('id', id);
userList.appendChild(listItem);
});
userList.addEventListener('click', (event) => handleUserListClick(event, users, stocks));
}
/**
* Handles the click event on the user list
* @param {*} event
*/
function handleUserListClick(event, users, stocks) {
console.log("CLicked");
// get the user id from the list item
const userId = event.target.id;
// find the user in the userData array
// we use a "truthy" comparison here because the user id is a number and the event target id is a string
const user = users.find(user => user.id == userId);
// populate the form with the user's data
populateForm(user);
renderPortfolio(user, stocks);
}
/**
* Populates the form with the user's data
* @param {*} user
*/
function populateForm(data) {
// use destructuring to get the user and id from the data object
const { user, id } = data;
document.querySelector('#userID').value = id;
document.querySelector('#firstname').value = user.firstname;
document.querySelector('#lastname').value = user.lastname;
document.querySelector('#address').value = user.address;
document.querySelector('#city').value = user.city;
document.querySelector('#email').value = user.email;
}
/**
* Renders the portfolio items for the user
* @param {*} user
*/
function renderPortfolio(user, stocks) {
// get the user's stock data
const { portfolio } = user;
// get the portfolio list element
const portfolioDetails = document.querySelector('.portfolio-list');
// clear the list from previous render
portfolioDetails.innerHTML = '';
// map over portfolio items and render them
portfolio.map(({ symbol, owned }) => {
// create a list item and append it to the list
const symbolEl = document.createElement('p');
const sharesEl = document.createElement('p');
const actionEl = document.createElement('button');
symbolEl.innerText = symbol;
sharesEl.innerText = owned;
actionEl.innerText = 'View';
actionEl.setAttribute('id', symbol);
portfolioDetails.appendChild(symbolEl);
portfolioDetails.appendChild(sharesEl);
portfolioDetails.appendChild(actionEl);
});
portfolioDetails.addEventListener('click', (event) => {
// let's make sure we only handle clicks on the buttons
if (event.target.tagName === 'BUTTON') {
viewStock(event.target.id, stocks);
}
});
}
/**
* Renders the stock information for the symbol
* @param {*} symbol
*/
function viewStock(symbol, stocks) {
// begin by hiding the stock area until a stock is viewed
const stockArea = document.querySelector('.stock-form');
if (stockArea) {
// find the stock object for this symbol
const stock = stocks.find( function (s) { return s.symbol == symbol;});
document.querySelector('#stockName').textContent = stock.name;
document.querySelector('#stockSector').textContent = stock.sector;
document.querySelector('#stockIndustry').textContent = stock.subIndustry;
document.querySelector('#stockAddress').textContent = stock.address;
document.querySelector('#logo').src = `logos/${symbol}.svg`;
}
}