@@ -4,26 +4,28 @@ import {async_start, slugify} from '../../utils'
44import { If } from '../shared/Tools'
55import { Grid , List } from './List'
66import ConModal from './ConModal'
7- import SelectSubjects from './SelectSubjects '
7+ import { SubjectSelect , LocationInput } from './Filters '
88
99class Contractors extends Component {
1010 constructor ( props ) {
1111 super ( props )
1212 this . state = {
13- contractors : [ ] ,
14- got_contractors : false ,
13+ contractor_response : null ,
1514 page : 1 ,
1615 more_pages : false ,
1716 subjects : [ ] ,
1817 selected_subject : null ,
1918 last_url : null ,
19+ location_str : null ,
2020 }
2121 this . update_contractors = this . update_contractors . bind ( this )
2222 this . get_contractor_details = this . get_contractor_details . bind ( this )
2323 this . set_contractor_details = this . set_contractor_details . bind ( this )
2424 this . subject_url = this . subject_url . bind ( this )
2525 this . page_url = this . page_url . bind ( this )
2626 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 )
2729 }
2830
2931 async componentDidMount ( ) {
@@ -59,36 +61,42 @@ class Contractors extends Component {
5961 this . update_contractors ( selected_subject )
6062 }
6163
62- async update_contractors ( selected_subject ) {
64+ location_change ( loc ) {
65+ this . setState ( { location_str : loc } )
66+ }
67+
68+ submit_location ( location_str ) {
69+ this . update_contractors ( this . state . selected_subject , location_str )
70+ }
71+
72+ async update_contractors ( selected_subject , location_str ) {
6373 if ( ! selected_subject ) {
6474 const m = this . props . history . location . pathname . match ( / s u b j e c t \/ ( \d + ) / )
6575 const subject_id = m ? parseInt ( m [ 1 ] , 10 ) : null
6676 if ( subject_id && this . state . subjects . length > 0 ) {
6777 selected_subject = this . state . subjects . find ( s => s . id === subject_id )
6878 }
6979 }
80+ if ( location_str === undefined ) {
81+ location_str = this . state . location_str
82+ }
7083
7184 const m = this . props . history . location . pathname . match ( / p a g e \/ ( \d + ) / )
7285 const page = m ? parseInt ( m [ 1 ] , 10 ) : 1
7386 this . setState ( { selected_subject, page} )
7487 const args = Object . assign ( { } , this . props . config . contractor_filter , {
7588 subject : selected_subject ? selected_subject . id : null ,
7689 pagination : this . props . config . pagination ,
90+ sort : this . props . config . sort_on ,
7791 page : page ,
92+ location : location_str ,
7893 } )
79- const data = await this . props . root . requests . get ( 'contractors' , args )
80- let contractors
81- if ( Array . isArray ( data ) ) {
82- contractors = data
83- } else {
84- contractors = data . results
85- }
86- this . props . config . event_callback ( 'updated_contractors' , contractors )
87- this . setState ( { contractors : [ ] } )
94+ const contractor_response = await this . props . root . requests . get ( 'contractors' , args )
95+ this . props . config . event_callback ( 'updated_contractors' , contractor_response )
96+ this . setState ( { contractor_response : { results : [ ] } } )
8897 setTimeout ( ( ) => this . setState ( {
89- contractors,
90- got_contractors : true ,
91- more_pages : contractors . length === this . props . config . pagination ,
98+ contractor_response,
99+ more_pages : contractor_response . count > contractor_response . results . length ,
92100 } ) , 0 )
93101 }
94102
@@ -109,20 +117,42 @@ class Contractors extends Component {
109117 }
110118
111119 render ( ) {
120+ let description = ''
121+ const con_count = this . state . contractor_response && this . state . contractor_response . count
122+ if ( con_count && this . state . selected_subject ) {
123+ const msg_id_suffix = con_count === 1 ? 'single' : 'plural'
124+ description = this . props . root . get_text ( 'subject_filter_summary_' + msg_id_suffix , {
125+ count : con_count ,
126+ subject : this . state . selected_subject . name ,
127+ } )
128+ }
112129 const DisplayComponent = this . props . config . mode === 'grid' ? Grid : List
113130 return (
114131 < div className = "tcs-app tcs-contractors" >
115- < If v = { this . state . got_contractors && this . props . config . subject_filter } >
116- < SelectSubjects get_text = { this . props . root . get_text }
117- contractors = { this . state . contractors }
118- subjects = { this . state . subjects }
119- selected_subject = { this . state . selected_subject }
120- subject_change = { this . subject_change } />
132+ < If v = { this . state . contractor_response } >
133+ < div className = "tcs-filters-container" >
134+ < LocationInput get_text = { this . props . root . get_text }
135+ show = { this . props . config . show_location_search }
136+ loc_raw = { this . state . location_str }
137+ loc_change = { this . location_change }
138+ submit = { this . submit_location } />
139+
140+ < SubjectSelect get_text = { this . props . root . get_text }
141+ show = { this . props . config . show_subject_filter }
142+ subjects = { this . state . subjects }
143+ selected_subject = { this . state . selected_subject }
144+ subject_change = { this . subject_change } />
145+ </ div >
146+ < div key = "summary" className = "tcs-summary" >
147+ { description }
148+ </ div >
121149 </ If >
122- < DisplayComponent contractors = { this . state . contractors } root = { this . props . root } />
123- < If v = { this . state . got_contractors && this . state . contractors . length === 0 } >
150+ < DisplayComponent
151+ contractors = { this . state . contractor_response ? this . state . contractor_response . results : [ ] }
152+ root = { this . props . root } />
153+ < If v = { this . state . contractor_response && this . state . contractor_response . count === 0 } >
124154 < div className = "tcs-no-contractors" >
125- { this . props . root . get_text ( 'no_tutors_found' ) }
155+ { this . props . root . get_text ( this . state . location_str === null ? 'no_tutors_found' : 'no_tutors_found_loc ') }
126156 </ div >
127157 </ If >
128158
@@ -145,8 +175,8 @@ class Contractors extends Component {
145175 < Route path = { this . props . root . url ( ':id(\\d+):_extra' ) } render = { props => (
146176 < ConModal id = { props . match . params . id }
147177 last_url = { this . state . last_url }
148- contractors = { this . state . contractors }
149- got_contractors = { this . state . got_contractors }
178+ contractors = { this . state . contractor_response . results }
179+ got_contractors = { ! ! this . state . contractor_response }
150180 get_contractor_details = { this . get_contractor_details }
151181 root = { this . props . root }
152182 config = { this . props . config }
0 commit comments