@@ -9,7 +9,16 @@ const RECORDS_PER_PAGE = 25
9
9
10
10
export default class GtfsPlusTable extends Component {
11
11
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
13
22
}
14
23
15
24
state = {
@@ -22,6 +31,19 @@ export default class GtfsPlusTable extends Component {
22
31
if ( this . props . table !== nextProps . table ) {
23
32
this . setState ( { currentPage : 1 } )
24
33
}
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
+ }
25
47
}
26
48
27
49
componentDidMount ( ) {
@@ -33,17 +55,18 @@ export default class GtfsPlusTable extends Component {
33
55
}
34
56
35
57
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 || [ ]
38
61
if ( this . state . visibility === 'validation' && tableValidation . length < 5000 ) {
39
- return this . props . tableData
62
+ return tableData
40
63
. filter ( record => ( tableValidation . find ( v => v . rowIndex === record . origRowIndex ) ) )
41
64
. 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 ) )
43
66
}
44
- return this . props . tableData
67
+ return tableData
45
68
. 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 ) )
47
70
}
48
71
49
72
_onChangeVisibleRows = ( evt ) => this . setState ( { visibility : evt . target . value , currentPage : 1 } )
@@ -71,9 +94,10 @@ export default class GtfsPlusTable extends Component {
71
94
_onPageRight = evt => this . setState ( { currentPage : this . state . currentPage + 1 } )
72
95
73
96
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 )
77
101
const headerStyle = {
78
102
fontWeight : 'bold' ,
79
103
fontSize : '24px' ,
@@ -103,15 +127,15 @@ export default class GtfsPlusTable extends Component {
103
127
{ ( pageCount > 1 )
104
128
? < span style = { { marginRight : '15px' } } >
105
129
< Button
106
- disabled = { this . state . currentPage <= 1 }
130
+ disabled = { currentPage <= 1 }
107
131
onClick = { this . _onPageLeft } >
108
132
< Glyphicon glyph = 'arrow-left' />
109
133
</ Button >
110
134
< span style = { { fontSize : '18px' , margin : '0px 10px' } } >
111
- Page { this . state . currentPage } of { pageCount }
135
+ Page { currentPage } of { pageCount }
112
136
</ span >
113
137
< Button
114
- disabled = { this . state . currentPage >= pageCount }
138
+ disabled = { currentPage >= pageCount }
115
139
onClick = { this . _onPageRight } >
116
140
< Glyphicon glyph = 'arrow-right' />
117
141
</ Button >
@@ -160,11 +184,11 @@ export default class GtfsPlusTable extends Component {
160
184
< tbody >
161
185
{ rowData && rowData . length > 0
162
186
? rowData . map ( ( data , rowIndex ) => {
163
- const tableRowIndex = ( ( this . state . currentPage - 1 ) * RECORDS_PER_PAGE ) + rowIndex
187
+ const tableRowIndex = ( ( currentPage - 1 ) * RECORDS_PER_PAGE ) + rowIndex
164
188
return (
165
189
< tr key = { rowIndex } >
166
190
{ table . fields . map ( field => {
167
- const validationIssue = this . props . validation
191
+ const validationIssue = validation
168
192
? this . props . validation . find ( v =>
169
193
( v . rowIndex === data . origRowIndex && v . fieldName === field . name ) )
170
194
: null
@@ -196,7 +220,7 @@ export default class GtfsPlusTable extends Component {
196
220
< OptionButton
197
221
bsStyle = 'danger'
198
222
bsSize = 'small'
199
- value = { rowIndex }
223
+ value = { tableRowIndex }
200
224
className = 'pull-right'
201
225
onClick = { this . _onClickDeleteRow } >
202
226
< Glyphicon glyph = 'remove' />
@@ -210,7 +234,7 @@ export default class GtfsPlusTable extends Component {
210
234
</ tbody >
211
235
</ Table >
212
236
213
- { ! rowData || rowData . length === 0
237
+ { ( ! tableData || tableData . length === 0 )
214
238
? < Row > < Col xs = { 12 } >
215
239
< i > No entries exist for this table.</ i >
216
240
</ Col > </ Row >
0 commit comments