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