Skip to content

Commit 70144a5

Browse files
author
Balashov Nikita
committed
add users from server
1 parent b836a3c commit 70144a5

File tree

7 files changed

+639
-617
lines changed

7 files changed

+639
-617
lines changed

phone-book/build/bundle.js

Lines changed: 38 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phone-book/src/add-user/add-user.js

Whitespace-only changes.

phone-book/src/components/users.js

Lines changed: 4 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,6 @@
1-
const users = [
2-
{
3-
firstName: 'Ivan',
4-
lastName: 'Ivanov',
5-
6-
},
7-
{
8-
firstName: 'German',
9-
lastName: 'Smith',
10-
11-
},
12-
{
13-
firstName: 'Kvas',
14-
lastName: 'Petrov',
15-
16-
},
17-
{
18-
firstName: 'Kvas',
19-
lastName: 'Taras',
20-
21-
},
22-
{
23-
firstName: 'Kavo',
24-
lastName: 'Ivanov',
25-
26-
},
27-
{
28-
firstName: 'Lada',
29-
lastName: 'Sedan',
30-
31-
},
32-
{
33-
firstName: 'Lada',
34-
lastName: 'Priora',
35-
36-
},
37-
{
38-
firstName: 'Orange',
39-
lastName: 'Juice',
40-
41-
},
42-
{
43-
firstName: 'Arbuz',
44-
lastName: 'Leto',
45-
46-
},
47-
{
48-
firstName: 'Dunya',
49-
lastName: 'Fall',
50-
51-
},
52-
{
53-
firstName: 'Fellow',
54-
lastName: 'Ship',
55-
56-
}
57-
];
1+
import {Url} from '../url/url';
2+
3+
const serverSide = new Url();
4+
const users = JSON.parse(serverSide.obtainUsers());
585

596
export {users};

phone-book/src/contact/contact.js

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import {users} from '../components/users';
2+
3+
/* ================== CONTACT START================== */
4+
5+
class ContactPage {
6+
constructor(store) {
7+
this.setStateContact = () => {
8+
const {setState} = store;
9+
const initializeState = {
10+
stateName: 'CONTACT',
11+
activePage: this.render(),
12+
};
13+
setState(initializeState);
14+
}
15+
16+
}
17+
18+
render() {
19+
const contactTempalte = /*html*/`
20+
<div id="contact-wraper">
21+
22+
<header class="container">
23+
<form class="form-inline search-form row">
24+
<div class="form-group">
25+
<label class="sr-only" for="search">Search</label>
26+
<input type="text" class="form-control" id= "search" placeholder="Search">
27+
</div>
28+
</form>
29+
</header>
30+
31+
<main class="container contact-list">
32+
<table class="table table-hover table-striped">
33+
34+
<thead>
35+
<tr id="wraper-for-th">
36+
<th class="for-sort">Name</th>
37+
<th class="for-sort">Last Name</th>
38+
<th class="for-sort">Email</th>
39+
</tr>
40+
</thead>
41+
42+
<tbody id="list-of-contacts">
43+
${this.contactListComponent(users)}
44+
</tbody>
45+
46+
</table>
47+
</main>
48+
</div>
49+
`;
50+
51+
return contactTempalte;
52+
}
53+
54+
contactListComponent(userList) {
55+
return userList.reduce((listStructure, user) => {
56+
const splitedFullName = user.fullName.split(' ');
57+
const userFirstName = splitedFullName[0];
58+
const userLastName = splitedFullName[1];
59+
const userEmail = user.email;
60+
61+
const userComponent = /*html*/`
62+
<tr class="user">
63+
<td> ${userFirstName} </td>
64+
<td> ${userLastName} </td>
65+
<td> ${userEmail} </td>
66+
</tr>
67+
`;
68+
69+
listStructure += userComponent;
70+
return listStructure;
71+
}, ``)
72+
}
73+
74+
applyListenerForContactPage() {
75+
const wraperForTh = document.getElementById('wraper-for-th');
76+
wraperForTh.addEventListener('click', (e) => {
77+
const TH_ELEM_CONTAINS = e.target.textContent.trim();
78+
const PREDICT_TEXT_CONTENT = {
79+
firstName: 'Name',
80+
lastName: 'Last Name',
81+
email: 'Email'
82+
};
83+
84+
const ListOfContacts = document.getElementById('list-of-contacts');
85+
86+
if(TH_ELEM_CONTAINS === PREDICT_TEXT_CONTENT.firstName) {
87+
const sortedListByFirsName = this.sortUsersByValue('firstName', users);
88+
ListOfContacts.innerHTML = this.contactListComponent(sortedListByFirsName);
89+
return;
90+
}
91+
92+
if(TH_ELEM_CONTAINS === PREDICT_TEXT_CONTENT.lastName) {
93+
const sortedListByLastName = this.sortUsersByValue('lastName', users);
94+
ListOfContacts.innerHTML = this.contactListComponent(sortedListByLastName);
95+
return;
96+
}
97+
98+
if(TH_ELEM_CONTAINS === PREDICT_TEXT_CONTENT.email) {
99+
const sortedListByEmail = this.sortUsersByValue('email', users);
100+
ListOfContacts.innerHTML = this.contactListComponent(sortedListByEmail);
101+
return;
102+
}
103+
})
104+
105+
/* SORT USERS BY INPUTED LETTERS OF NAME */
106+
107+
108+
}
109+
110+
sortUsersByValue(key, users) {
111+
const sortFunction = function(value, nextValue) {
112+
if(value[key] > nextValue[key]) return 1;
113+
if(value[key] < nextValue[key]) return -1;
114+
}
115+
116+
return [...users].sort(sortFunction);
117+
}
118+
}
119+
120+
/* ================== CONTACT END================== */
121+
122+
export {ContactPage};

0 commit comments

Comments
 (0)