Skip to content

Commit ae3b5d0

Browse files
author
Balashov Nikita
committed
contact-page
1 parent f1747a9 commit ae3b5d0

File tree

1 file changed

+44
-9
lines changed

1 file changed

+44
-9
lines changed

phone-book/src/contact/contact.js

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import {users} from '../components/users';
1+
import {Url} from '../url/url';
2+
import {UserPage} from '../user-page/user-page';
23

34
/* ================== CONTACT START================== */
45

56
class ContactPage {
6-
constructor(store) {
7+
constructor(store, accountName) {
78
this.setStateContact = () => {
89
const {setState} = store;
910
const initializeState = {
@@ -13,6 +14,8 @@ class ContactPage {
1314
setState(initializeState);
1415
}
1516

17+
this.url = new Url(accountName);
18+
this.userPage = new UserPage(store, accountName);
1619
}
1720

1821
render() {
@@ -40,7 +43,7 @@ class ContactPage {
4043
</thead>
4144
4245
<tbody id="list-of-contacts">
43-
${this.contactListComponent(users)}
46+
4447
</tbody>
4548
4649
</table>
@@ -57,9 +60,10 @@ class ContactPage {
5760
const userFirstName = splitedFullName[0];
5861
const userLastName = splitedFullName[1];
5962
const userEmail = user.email;
63+
const id = user._id;
6064

6165
const userComponent = /*html*/`
62-
<tr class="user">
66+
<tr id="${id}" class="user">
6367
<td> ${userFirstName} </td>
6468
<td> ${userLastName} </td>
6569
<td> ${userEmail} </td>
@@ -71,6 +75,19 @@ class ContactPage {
7175
}, ``)
7276
}
7377

78+
renderUsers() {
79+
this.url.getUsersFromServer()
80+
.then(data => {
81+
return data.json()
82+
})
83+
.then(users => {
84+
this.users = users;
85+
const listStructure = this.contactListComponent(users);
86+
const listOfContacts = document.getElementById('list-of-contacts');
87+
listOfContacts.innerHTML = listStructure;
88+
})
89+
}
90+
7491
applyListenerForContactPage() {
7592
const wraperForTh = document.getElementById('wraper-for-th');
7693
wraperForTh.addEventListener('click', (e) => {
@@ -85,20 +102,20 @@ class ContactPage {
85102

86103
if(TH_ELEM_CONTAINS === PREDICT_TEXT_CONTENT.firstName) {
87104
const firstName = 0;
88-
const sortedListByFirsName = this.mergeSort(users, firstName);
105+
const sortedListByFirsName = this.mergeSort(this.users, firstName);
89106
listOfContacts.innerHTML = this.contactListComponent(sortedListByFirsName);
90107
return;
91108
}
92109

93110
if(TH_ELEM_CONTAINS === PREDICT_TEXT_CONTENT.lastName) {
94111
const lastName = 1;
95-
const sortedListByLastName = this.mergeSort(users, lastName);
112+
const sortedListByLastName = this.mergeSort(this.users, lastName);
96113
listOfContacts.innerHTML = this.contactListComponent(sortedListByLastName);
97114
return;
98115
}
99116

100117
if(TH_ELEM_CONTAINS === PREDICT_TEXT_CONTENT.email) {
101-
const sortedListByEmail = this.sortUsersByValue('email', users);
118+
const sortedListByEmail = this.sortUsersByValue('email', this.users);
102119
listOfContacts.innerHTML = this.contactListComponent(sortedListByEmail);
103120
return;
104121
}
@@ -117,7 +134,25 @@ class ContactPage {
117134
: listOfContacts.innerHTML = this.contactListComponent(filteredUsers);
118135

119136

120-
})
137+
});
138+
139+
/* DEFINE USER */
140+
141+
const listOfContacts = document.getElementById('list-of-contacts');
142+
const handlerForListOfContacts = (e) => {
143+
if(e.target.parentElement.tagName === "TR") {
144+
const id = e.target.parentElement.id;
145+
const user = this.users.filter(user => user._id === id)[0];
146+
this.userPage.setStateUserPage();
147+
const userPage = this.userPage.render(user);
148+
149+
const MAIN_WRAPER = document.getElementById('main-wraper');
150+
MAIN_WRAPER.firstElementChild.outerHTML = userPage;
151+
this.userPage.applyListenersForUserPage();
152+
}
153+
}
154+
155+
listOfContacts.addEventListener('click', handlerForListOfContacts);
121156
}
122157

123158
sortUsersByValue(key, users) {
@@ -161,7 +196,7 @@ class ContactPage {
161196
}
162197

163198
filterUsersByInputValueByName(inputValue) {
164-
return users.reduce((newUsers, user) => {
199+
return this.users.reduce((newUsers, user) => {
165200
const firstName = user.fullName.split(' ')[0].toLowerCase();
166201

167202
const comparedPartOfName = firstName.slice(0, inputValue.length);

0 commit comments

Comments
 (0)