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'
44import { If } from '../shared/Tools'
55import { Grid , List } from './List'
@@ -12,12 +12,16 @@ class Contractors extends Component {
1212 this . state = {
1313 contractors : [ ] ,
1414 got_contractors : false ,
15+ page : 1 ,
16+ more_pages : false ,
1517 subjects : [ ] ,
1618 selected_subject : null ,
1719 }
1820 this . update_contractors = this . update_contractors . bind ( this )
1921 this . get_contractor_details = this . get_contractor_details . bind ( this )
2022 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 )
2125 this . subject_change = this . subject_change . bind ( this )
2226 }
2327
@@ -31,12 +35,24 @@ class Contractors extends Component {
3135 await this . update_contractors ( )
3236 }
3337
34- subject_change ( selected_subject ) {
38+ subject_url ( selected_subject ) {
3539 if ( selected_subject ) {
36- 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 ) } ` )
3741 } else {
38- this . props . history . push ( this . props . root . url ( '' ) )
42+ return this . props . root . url ( '' )
3943 }
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 ) === '/' ? '' : '/' } page/${ new_page } `
50+ }
51+ return url
52+ }
53+
54+ subject_change ( selected_subject ) {
55+ this . props . history . push ( this . subject_url ( selected_subject ) )
4056 this . update_contractors ( selected_subject )
4157 }
4258
@@ -49,14 +65,19 @@ class Contractors extends Component {
4965 }
5066 }
5167
52- this . setState ( { selected_subject} )
53- const sub_id = selected_subject && selected_subject . id
54- 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+ } )
5576 const contractors = await this . props . root . requests . get ( 'contractors' , args )
56- contractors . reverse ( )
5777 this . props . config . event_callback ( 'updated_contractors' , contractors )
5878 this . setState ( {
5979 contractors,
80+ more_pages : contractors . length === this . props . config . pagination ,
6081 got_contractors : true
6182 } )
6283 }
@@ -90,6 +111,22 @@ class Contractors extends Component {
90111 </ If >
91112 < DisplayComponent contractors = { this . state . contractors } root = { this . props . root } />
92113
114+ < If v = { this . state . page > 1 || this . state . more_pages } >
115+ < div className = "tcs-pagination" >
116+ < Link
117+ to = { this . page_url ( this . state . page - 1 ) }
118+ onClick = { ( ) => setTimeout ( ( ) => this . update_contractors ( ) , 0 ) }
119+ className = { 'tcs-previous' + ( this . state . page > 1 ? '' : ' tcs-disable' ) } >
120+ ‹‹ { this . props . root . get_text ( 'previous' ) }
121+ </ Link >
122+ < Link
123+ to = { this . page_url ( this . state . page + 1 ) }
124+ onClick = { ( ) => setTimeout ( ( ) => this . update_contractors ( ) , 0 ) }
125+ className = { 'tcs-next' + ( this . state . more_pages ? '' : ' tcs-disable' ) } >
126+ { this . props . root . get_text ( 'next' ) } ››
127+ </ Link >
128+ </ div >
129+ </ If >
93130 < Route path = { this . props . root . url ( ':id(\\d+):_extra' ) } render = { props => (
94131 < ConModal id = { props . match . params . id }
95132 contractors = { this . state . contractors }
0 commit comments