11import  React ,  {  Component  }  from  'react' 
2- import  { Route }  from  'react-router-dom' 
2+ import  { Link ,   Route }  from  'react-router-dom' 
33import  { async_start ,  slugify }  from  '../../utils' 
4- import  Grid  from  './Grid' 
4+ import  { If }  from  '../shared/Tools' 
5+ import  { Grid ,  List }  from  './List' 
56import  ConModal  from  './ConModal' 
7+ import  SelectSubjects  from  './SelectSubjects' 
68
79class  Contractors  extends  Component  { 
810  constructor  ( props )  { 
911    super ( props ) 
1012    this . state  =  { 
1113      contractors : [ ] , 
1214      got_contractors : false , 
15+       page : 1 , 
16+       more_pages : false , 
1317      subjects : [ ] , 
1418      selected_subject : null , 
1519    } 
1620    this . update_contractors  =  this . update_contractors . bind ( this ) 
1721    this . get_contractor_details  =  this . get_contractor_details . bind ( this ) 
1822    this . set_contractor_details  =  this . set_contractor_details . bind ( this ) 
23+     this . subject_url  =  this . subject_url . bind ( this ) 
24+     this . page_url  =  this . page_url . bind ( this ) 
1925    this . subject_change  =  this . subject_change . bind ( this ) 
2026  } 
2127
@@ -29,12 +35,24 @@ class Contractors extends Component {
2935    await  this . update_contractors ( ) 
3036  } 
3137
32-   subject_change  ( selected_subject )  { 
38+   subject_url  ( selected_subject )  { 
3339    if  ( selected_subject )  { 
34-       this . props . history . push ( this . props . root . url ( `subject/${ selected_subject . id } ${ slugify ( selected_subject . name ) }  ) ) 
40+       return   this . props . root . url ( `subject/${ selected_subject . id } ${ slugify ( selected_subject . name ) }  ) 
3541    }  else  { 
36-       this . props . history . push ( this . props . root . url ( '' ) ) 
42+       return   this . props . root . url ( '' ) 
3743    } 
44+   } 
45+ 
46+   page_url  ( new_page )  { 
47+     let  url  =  this . subject_url ( this . state . selected_subject ) 
48+     if  ( new_page  >  1 )  { 
49+       url  +=  `${ url . substr ( - 1 )  ===  '/'  ? ''  : '/' } ${ new_page }  
50+     } 
51+     return  url 
52+   } 
53+ 
54+   subject_change  ( selected_subject )  { 
55+     this . props . history . push ( this . subject_url ( selected_subject ) ) 
3856    this . update_contractors ( selected_subject ) 
3957  } 
4058
@@ -47,15 +65,22 @@ class Contractors extends Component {
4765      } 
4866    } 
4967
50-     this . setState ( { selected_subject} ) 
51-     const  sub_id  =  selected_subject  &&  selected_subject . id 
52-     const  args  =  Object . assign ( { } ,  this . props . config . contractor_filter ,  { subject : sub_id  ||  null } ) 
68+     const  m  =  this . props . history . location . pathname . match ( / p a g e \/ ( \d + ) / ) 
69+     const  page  =  m  ? parseInt ( m [ 1 ] ,  10 )  : 1 
70+     this . setState ( { selected_subject,  page} ) 
71+     const  args  =  Object . assign ( { } ,  this . props . config . contractor_filter ,  { 
72+       subject : selected_subject  ? selected_subject . id  : null , 
73+       pagination : this . props . config . pagination , 
74+       page : page , 
75+     } ) 
5376    const  contractors  =  await  this . props . root . requests . get ( 'contractors' ,  args ) 
5477    this . props . config . event_callback ( 'updated_contractors' ,  contractors ) 
55-     this . setState ( { 
78+     this . setState ( { contractors : [ ] } ) 
79+     setTimeout ( ( )  =>  this . setState ( { 
5680      contractors, 
57-       got_contractors : true 
58-     } ) 
81+       got_contractors : true , 
82+       more_pages : contractors . length  ===  this . props . config . pagination , 
83+     } ) ,  0 ) 
5984  } 
6085
6186  get_contractor_details  ( con )  { 
@@ -75,16 +100,39 @@ class Contractors extends Component {
75100  } 
76101
77102  render  ( )  { 
103+     const  DisplayComponent  =  this . props . config . mode  ===  'grid'  ? Grid  : List 
78104    return  ( 
79-       < div  className = "tcs-app" > 
80-         < Grid  config = { this . props . config } 
81-               history = { this . props . history } 
82-               contractors = { this . state . contractors } 
83-               subjects = { this . state . subjects } 
84-               selected_subject = { this . state . selected_subject } 
85-               subject_change = { this . subject_change } 
86-               root = { this . props . root } /> 
105+       < div  className = "tcs-app tcs-contractors" > 
106+         < If  v = { this . state . got_contractors  &&  this . props . config . subject_filter } > 
107+           < SelectSubjects  get_text = { this . props . root . get_text } 
108+                           contractors = { this . state . contractors } 
109+                           subjects = { this . state . subjects } 
110+                           selected_subject = { this . state . selected_subject } 
111+                           subject_change = { this . subject_change } /> 
112+         </ If > 
113+         < DisplayComponent  contractors = { this . state . contractors }  root = { this . props . root } /> 
114+         < If  v = { this . state . got_contractors  &&  this . state . contractors . length  ===  0 } > 
115+           < div  className = "tcs-no-contractors" > 
116+             { this . props . root . get_text ( 'no_tutors_found' ) } 
117+           </ div > 
118+         </ If > 
87119
120+         < If  v = { this . state . page  >  1  ||  this . state . more_pages } > 
121+           < div  className = "tcs-pagination" > 
122+             < Link 
123+               to = { this . page_url ( this . state . page  -  1 ) } 
124+               onClick = { ( )  =>  setTimeout ( ( )  =>  this . update_contractors ( ) ,  0 ) } 
125+               className = { 'tcs-previous'  +  ( this . state . page  >  1  ? ''  : ' tcs-disable' ) } > 
126+               ‹‹ { this . props . root . get_text ( 'previous' ) } 
127+             </ Link > 
128+             < Link 
129+               to = { this . page_url ( this . state . page  +  1 ) } 
130+               onClick = { ( )  =>  setTimeout ( ( )  =>  this . update_contractors ( ) ,  0 ) } 
131+               className = { 'tcs-next'  +  ( this . state . more_pages  ? ''  : ' tcs-disable' ) } > 
132+               { this . props . root . get_text ( 'next' ) }  ››
133+             </ Link > 
134+           </ div > 
135+         </ If > 
88136        < Route  path = { this . props . root . url ( ':id(\\d+):_extra' ) }  render = { props  =>  ( 
89137          < ConModal  id = { props . match . params . id } 
90138                    contractors = { this . state . contractors } 
0 commit comments