@@ -9,7 +9,16 @@ const RECORDS_PER_PAGE = 25
99
1010export default class GtfsPlusTable extends Component {
1111 static propTypes = {
12- table : PropTypes . object
12+ table : PropTypes . object ,
13+ tableData : PropTypes . array ,
14+ validation : PropTypes . object ,
15+ newRowClicked : PropTypes . func ,
16+ deleteRowClicked : PropTypes . func ,
17+ fieldEdited : PropTypes . func ,
18+ gtfsEntitySelected : PropTypes . func ,
19+ getGtfsEntity : PropTypes . func ,
20+ showHelpClicked : PropTypes . func ,
21+ newRowsDisplayed : PropTypes . func
1322 }
1423
1524 state = {
@@ -22,6 +31,19 @@ export default class GtfsPlusTable extends Component {
2231 if ( this . props . table !== nextProps . table ) {
2332 this . setState ( { currentPage : 1 } )
2433 }
34+ const nextTableLength = nextProps . tableData && nextProps . tableData . length
35+ const tableLength = this . props . tableData && this . props . tableData . length
36+ if ( tableLength !== nextTableLength && nextProps . tableData ) {
37+ const pageCount = Math . ceil ( nextTableLength / RECORDS_PER_PAGE )
38+ // If current page becomes greater than page count (e.g., after deleting rows)
39+ // or page count increases (e.g., after adding rows), set current page to
40+ // page count to avoid confusion for user (user has added a row, but they
41+ // don't see it because the current page has not updated).
42+ const currentPage = this . state . currentPage > pageCount || pageCount > this . state . pageCount
43+ ? pageCount
44+ : this . state . currentPage
45+ this . setState ( { currentPage, pageCount} )
46+ }
2547 }
2648
2749 componentDidMount ( ) {
@@ -33,17 +55,18 @@ export default class GtfsPlusTable extends Component {
3355 }
3456
3557 getActiveRowData ( currentPage ) {
36- if ( ! this . props . tableData ) return [ ]
37- const tableValidation = this . props . validation || [ ]
58+ const { tableData, validation} = this . props
59+ if ( ! tableData ) return [ ]
60+ const tableValidation = validation || [ ]
3861 if ( this . state . visibility === 'validation' && tableValidation . length < 5000 ) {
39- return this . props . tableData
62+ return tableData
4063 . filter ( record => ( tableValidation . find ( v => v . rowIndex === record . origRowIndex ) ) )
4164 . slice ( ( currentPage - 1 ) * RECORDS_PER_PAGE ,
42- Math . min ( currentPage * RECORDS_PER_PAGE , this . props . tableData . length ) )
65+ Math . min ( currentPage * RECORDS_PER_PAGE , tableData . length ) )
4366 }
44- return this . props . tableData
67+ return tableData
4568 . slice ( ( currentPage - 1 ) * RECORDS_PER_PAGE ,
46- Math . min ( currentPage * RECORDS_PER_PAGE , this . props . tableData . length ) )
69+ Math . min ( currentPage * RECORDS_PER_PAGE , tableData . length ) )
4770 }
4871
4972 _onChangeVisibleRows = ( evt ) => this . setState ( { visibility : evt . target . value , currentPage : 1 } )
@@ -71,9 +94,10 @@ export default class GtfsPlusTable extends Component {
7194 _onPageRight = evt => this . setState ( { currentPage : this . state . currentPage + 1 } )
7295
7396 render ( ) {
74- const { showHelpClicked, table} = this . props
75- const { pageCount} = this . state
76- const rowData = this . getActiveRowData ( this . state . currentPage )
97+ const { showHelpClicked, table, tableData, validation} = this . props
98+ const { currentPage, pageCount} = this . state
99+ console . log ( currentPage , pageCount , tableData && tableData . length )
100+ const rowData = this . getActiveRowData ( currentPage )
77101 const headerStyle = {
78102 fontWeight : 'bold' ,
79103 fontSize : '24px' ,
@@ -103,15 +127,15 @@ export default class GtfsPlusTable extends Component {
103127 { ( pageCount > 1 )
104128 ? < span style = { { marginRight : '15px' } } >
105129 < Button
106- disabled = { this . state . currentPage <= 1 }
130+ disabled = { currentPage <= 1 }
107131 onClick = { this . _onPageLeft } >
108132 < Glyphicon glyph = 'arrow-left' />
109133 </ Button >
110134 < span style = { { fontSize : '18px' , margin : '0px 10px' } } >
111- Page { this . state . currentPage } of { pageCount }
135+ Page { currentPage } of { pageCount }
112136 </ span >
113137 < Button
114- disabled = { this . state . currentPage >= pageCount }
138+ disabled = { currentPage >= pageCount }
115139 onClick = { this . _onPageRight } >
116140 < Glyphicon glyph = 'arrow-right' />
117141 </ Button >
@@ -160,11 +184,11 @@ export default class GtfsPlusTable extends Component {
160184 < tbody >
161185 { rowData && rowData . length > 0
162186 ? rowData . map ( ( data , rowIndex ) => {
163- const tableRowIndex = ( ( this . state . currentPage - 1 ) * RECORDS_PER_PAGE ) + rowIndex
187+ const tableRowIndex = ( ( currentPage - 1 ) * RECORDS_PER_PAGE ) + rowIndex
164188 return (
165189 < tr key = { rowIndex } >
166190 { table . fields . map ( field => {
167- const validationIssue = this . props . validation
191+ const validationIssue = validation
168192 ? this . props . validation . find ( v =>
169193 ( v . rowIndex === data . origRowIndex && v . fieldName === field . name ) )
170194 : null
@@ -196,7 +220,7 @@ export default class GtfsPlusTable extends Component {
196220 < OptionButton
197221 bsStyle = 'danger'
198222 bsSize = 'small'
199- value = { rowIndex }
223+ value = { tableRowIndex }
200224 className = 'pull-right'
201225 onClick = { this . _onClickDeleteRow } >
202226 < Glyphicon glyph = 'remove' />
@@ -210,7 +234,7 @@ export default class GtfsPlusTable extends Component {
210234 </ tbody >
211235 </ Table >
212236
213- { ! rowData || rowData . length === 0
237+ { ( ! tableData || tableData . length === 0 )
214238 ? < Row > < Col xs = { 12 } >
215239 < i > No entries exist for this table.</ i >
216240 </ Col > </ Row >
0 commit comments