11import  React ,  {  Component  }  from  'react' 
22import  { Link ,  Route }  from  'react-router-dom' 
3- import  { slugify }  from  '../../utils' 
3+ import  { slugify ,   sleep }  from  '../../utils' 
44import  { If }  from  '../shared/Tools' 
55import  { Grid ,  List }  from  './List' 
66import  ConModal  from  './ConModal' 
77import  { SubjectSelect ,  LocationInput }  from  './Filters' 
88
9- class  Contractors  extends  Component  { 
9+ export   default   class  Contractors  extends  Component  { 
1010  constructor  ( props )  { 
1111    super ( props ) 
1212    this . state  =  { 
@@ -18,14 +18,6 @@ class Contractors extends Component {
1818      last_url : null , 
1919      location_str : null , 
2020    } 
21-     this . update_contractors  =  this . update_contractors . bind ( this ) 
22-     this . get_contractor_details  =  this . get_contractor_details . bind ( this ) 
23-     this . set_contractor_details  =  this . set_contractor_details . bind ( this ) 
24-     this . subject_url  =  this . subject_url . bind ( this ) 
25-     this . page_url  =  this . page_url . bind ( this ) 
26-     this . subject_change  =  this . subject_change . bind ( this ) 
27-     this . location_change  =  this . location_change . bind ( this ) 
28-     this . submit_location  =  this . submit_location . bind ( this ) 
2921  } 
3022
3123  async  componentDidMount  ( )  { 
@@ -38,38 +30,38 @@ class Contractors extends Component {
3830    await  this . update_contractors ( ) 
3931  } 
4032
41-   subject_url  ( selected_subject )  { 
33+   subject_url  =   selected_subject   =>  { 
4234    if  ( selected_subject )  { 
4335      return  this . props . root . url ( `subject/${ selected_subject . id } ${ slugify ( selected_subject . name ) }  ) 
4436    }  else  { 
4537      return  this . props . root . url ( '' ) 
4638    } 
4739  } 
4840
49-   page_url  ( new_page )  { 
41+   page_url  =   new_page   =>  { 
5042    let  url  =  this . subject_url ( this . state . selected_subject ) 
5143    if  ( new_page  >  1 )  { 
5244      url  +=  `${ url . substr ( - 1 )  ===  '/'  ? ''  : '/' } ${ new_page }  
5345    } 
5446    return  url 
5547  } 
5648
57-   subject_change  ( selected_subject )  { 
49+   subject_change  =   selected_subject   =>  { 
5850    const  url  =  this . subject_url ( selected_subject ) 
5951    this . props . history . push ( url ) 
6052    this . setState ( { last_url : url } ) 
6153    this . update_contractors ( selected_subject ) 
6254  } 
6355
64-   location_change  ( loc )  { 
56+   location_change  =   loc   =>  { 
6557    this . setState ( { location_str : loc } ) 
6658  } 
6759
68-   submit_location  ( location_str )  { 
60+   submit_location  =   location_str   =>  { 
6961    this . update_contractors ( this . state . selected_subject ,  location_str ) 
7062  } 
7163
72-   async   update_contractors  ( selected_subject ,  location_str )  { 
64+   update_contractors  =   async   ( selected_subject ,  location_str )   =>  { 
7365    if  ( ! selected_subject )  { 
7466      const  m  =  this . props . history . location . pathname . match ( / s u b j e c t \/ ( \d + ) / ) 
7567      const  subject_id  =  m  ? parseInt ( m [ 1 ] ,  10 )  : null 
@@ -101,20 +93,39 @@ class Contractors extends Component {
10193    } ) ,  0 ) 
10294  } 
10395
104-   get_contractor_details  ( con )  { 
105-     const  state_ref  =  'con_extra_'  +  con . id 
106-     const  con_extra  =  this . state [ state_ref ] 
107-     if  ( con_extra  ===  undefined )  { 
108-       this . set_contractor_details ( con . url ,  state_ref ) 
96+   get_contractor  =  async  ( contractor_id ,  set_contractor )  =>  { 
97+ 
98+     const  state_ref  =  `con_extra_${ contractor_id }  
99+     let  contractor  =  this . state [ state_ref ] 
100+     if  ( contractor )  { 
101+       set_contractor ( contractor ) 
102+       return 
103+     } 
104+ 
105+     // make sure update_contractors has finished before getting the contractor to avoid unnecessary requests 
106+     while  ( ! this . state . contractor_response )  { 
107+       await  sleep ( 50 ) 
109108    } 
110-     return  con_extra 
111-   } 
112109
113-   async  set_contractor_details  ( url ,  state_ref )  { 
114-     this . setState ( { [ state_ref ] : null } ) 
115-     const  con_details  =  await  this . props . root . requests . get ( url ) 
116-     this . props . config . event_callback ( 'get_contractor_details' ,  con_details ) 
117-     this . setState ( { [ state_ref ] : con_details } ) 
110+     const  contractor_summary  =  this . state . contractor_response . results . find ( c  =>  c . id  ===  contractor_id ) 
111+     let  url 
112+     if  ( contractor_summary )  { 
113+       set_contractor ( contractor_summary ) 
114+       url  =  contractor_summary . url 
115+     }  else  { 
116+       url  =  `contractors/${ contractor_id }  
117+     } 
118+ 
119+     let  r  =  await  this . props . root . requests . get ( url ,  null ,  { expected_statuses : [ 200 ,  404 ] } ) 
120+     if  ( r . status  ===  404 )  { 
121+       set_contractor ( null ) 
122+     }  else  { 
123+       contractor  =  r . data 
124+       this . props . config . event_callback ( 'get_contractor_details' ,  contractor ) 
125+ 
126+       this . setState ( { [ state_ref ] : contractor } ) 
127+       set_contractor ( contractor ) 
128+     } 
118129  } 
119130
120131  render  ( )  { 
@@ -194,18 +205,16 @@ class Contractors extends Component {
194205          </ div > 
195206        </ If > 
196207        < Route  path = { this . props . root . url ( ':id(\\d+):_extra' ) }  render = { props  =>  ( 
197-           < ConModal   id = { props . match . params . id } 
198-                      last_url = { this . state . last_url } 
199-                      contractors = { this . state . contractor_response   &&   this . state . contractor_response . results } 
200-                      got_contractors = { Boolean ( this . state . contractor_response ) } 
201-                      get_contractor_details = { this . get_contractor_details } 
202-                      root = { this . props . root } 
203-                      config = { this . props . config } 
204-                      history = { props . history } /> 
208+           < ConModal 
209+               id = { props . match . params . id } 
210+               last_url = { this . state . last_url } 
211+               get_contractor = { this . get_contractor } 
212+               root = { this . props . root } 
213+               config = { this . props . config } 
214+               history = { props . history } 
215+           /> 
205216        ) } /> 
206217      </ div > 
207218    ) 
208219  } 
209220} 
210- 
211- export  default  Contractors 
0 commit comments