Skip to content

Commit f4790b6

Browse files
committed
fix(gtfsplus): fix multiple page rendering/pageCount update and use versionId for GTFS entities
Update pageCount and currentPage state variables correctly when props (specifically tableData) are updated. Also, use the feedVersionId to fetch GTFS entities rather than the MTC feed source ID (which refs latest published version) because there may be a mismatch between entity IDs (or entity IDs may be missing) fixes #34
1 parent fc26e9d commit f4790b6

File tree

4 files changed

+64
-26
lines changed

4 files changed

+64
-26
lines changed

lib/gtfsplus/actions/gtfsplus.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { secureFetch } from '../../common/actions'
55
import { getConfigProperty } from '../../common/util/config'
66
// import {stopsAndRoutes, compose} from '../../gtfs/util/graphql'
77
import { fetchFeedVersions } from '../../manager/actions/versions'
8-
import { getFeedId } from '../../common/util/modules'
98

109
// EDIT ACTIVE GTFS+ ACTIONS
1110

@@ -180,7 +179,7 @@ export function receiveGtfsEntities (gtfsEntities) {
180179
}
181180
}
182181

183-
export function loadGtfsEntities (tableId, rows, feedSource) {
182+
export function loadGtfsEntities (tableId, rows, feedSource, feedVersionId) {
184183
return function (dispatch, getState) {
185184
// lookup table for mapping tableId:fieldName keys to inputType values
186185
const typeLookup = {}
@@ -221,23 +220,32 @@ export function loadGtfsEntities (tableId, rows, feedSource) {
221220
// return dispatch(receivedStopsAndRoutes(results))
222221
// })
223222

223+
// FIXME: major issue here with feedId vs. feedVersionId.
224+
// Basically, feedId will only reference the published feed version.
225+
// Here in the GTFS+ editor, we always want routes/stops from the
226+
// feedVersionId because there may be new routes/stops here that don't exist
227+
// in the published version.
224228
if (routesToLoad.length === 0 && stopsToLoad.length === 0) return
225-
const feedId = getFeedId(feedSource)
229+
const feedId = feedVersionId.replace('.zip', '')
230+
console.log(`fetching routes ${routesToLoad.join(',')}`)
231+
console.log(`fetching stops ${stopsToLoad.join(',')}`)
226232
var loadRoutes = Promise.all(routesToLoad.map(routeId => {
227233
const url = `/api/manager/routes/${routeId}?feed=${feedId}`
228234
return fetch(url)
229235
.then((response) => response.json())
236+
.catch(err => console.log(err))
230237
}))
231238

232239
var loadStops = Promise.all(stopsToLoad.map(stopId => {
233240
const url = `/api/manager/stops/${stopId}?feed=${feedId}`
234241
return fetch(url)
235242
.then((response) => response.json())
243+
.catch(err => console.log(err))
236244
}))
237245

238246
Promise.all([loadRoutes, loadStops]).then(results => {
239-
const loadedRoutes = results[0]
240-
const loadedStops = results[1]
247+
const loadedRoutes = results[0].filter(res => res) // filter out undefined
248+
const loadedStops = results[1].filter(res => res) // filter out undefined
241249
dispatch(receiveGtfsEntities(loadedRoutes.concat(loadedStops)))
242250
})
243251
}

lib/gtfsplus/components/GtfsPlusEditor.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ export default class GtfsPlusEditor extends Component {
1212
static propTypes = {
1313
deleteRowClicked: PropTypes.func,
1414
feedSource: PropTypes.object,
15+
feedVersionId: PropTypes.string,
1516
fieldEdited: PropTypes.func,
1617
gtfsEntityLookup: PropTypes.object,
1718
gtfsEntitySelected: PropTypes.func,
19+
loadGtfsEntities: PropTypes.func,
1820
newRowClicked: PropTypes.func,
19-
newRowsDisplayed: PropTypes.func,
2021
onComponentMount: PropTypes.func,
2122
project: PropTypes.object,
2223
tableData: PropTypes.object,
@@ -64,7 +65,10 @@ export default class GtfsPlusEditor extends Component {
6465

6566
_gtfsEntitySelected = (type, entity) => this.props.gtfsEntitySelected(type, entity)
6667

67-
_newRowsDisplayed = (rows) => this.props.newRowsDisplayed(this.state.activeTableId, rows, this.props.feedSource)
68+
_newRowsDisplayed = (rows) => {
69+
const {feedSource, feedVersionId, loadGtfsEntities} = this.props
70+
loadGtfsEntities(this.state.activeTableId, rows, feedSource, feedVersionId)
71+
}
6872

6973
_selectTable = (activeTableId) => this.setState({activeTableId})
7074

lib/gtfsplus/components/GtfsPlusTable.js

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,16 @@ const RECORDS_PER_PAGE = 25
99

1010
export 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>

lib/gtfsplus/containers/ActiveGtfsPlusEditor.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414

1515
const mapStateToProps = (state, ownProps) => {
1616
const feedSourceId = ownProps.routeParams.feedSourceId
17+
const feedVersionId = ownProps.routeParams.feedVersionId
1718
const user = state.user
1819
// find the containing project
1920
const project = state.projects.all
@@ -33,6 +34,7 @@ const mapStateToProps = (state, ownProps) => {
3334
gtfsEntityLookup: state.gtfsplus.gtfsEntityLookup,
3435
validation: state.gtfsplus.validation,
3536
feedSource,
37+
feedVersionId,
3638
project,
3739
user
3840
}
@@ -54,8 +56,8 @@ const mapDispatchToProps = (dispatch, ownProps) => {
5456
dispatch(uploadGtfsPlusFeed(feedVersionId, file))
5557
.then(() => dispatch(downloadGtfsPlusFeed(feedVersionId)))
5658
},
57-
newRowsDisplayed: (tableId, rows, feedSource) => {
58-
if (feedSource) dispatch(loadGtfsEntities(tableId, rows, feedSource))
59+
loadGtfsEntities: (tableId, rows, feedSource, feedVersionId) => {
60+
if (feedSource) dispatch(loadGtfsEntities(tableId, rows, feedSource, feedVersionId))
5961
},
6062
gtfsEntitySelected: (type, entity) => dispatch(receiveGtfsEntities([entity]))
6163
}

0 commit comments

Comments
 (0)