Skip to content

Commit d814cd8

Browse files
committed
refactor(gtfsplus): move component state into store, use selectors for filtering rows
1 parent e04c0b6 commit d814cd8

File tree

8 files changed

+292
-211
lines changed

8 files changed

+292
-211
lines changed

lib/gtfsplus/actions/gtfsplus.js

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import JSZip from 'jszip'
22
import fetch from 'isomorphic-fetch'
3+
import {createAction} from 'redux-actions'
34

45
import { secureFetch } from '../../common/actions'
56
import { getConfigProperty } from '../../common/util/config'
@@ -8,30 +9,15 @@ import { fetchFeedVersions } from '../../manager/actions/versions'
89

910
// EDIT ACTIVE GTFS+ ACTIONS
1011

11-
export function addGtfsPlusRow (tableId) {
12-
const table = getConfigProperty('modules.gtfsplus.spec').find(t => t.id === tableId)
12+
export const addGtfsPlusRow = createAction('ADD_GTFSPLUS_ROW')
1313

14-
const rowData = {}
15-
for (const field of table.fields) {
16-
rowData[field.name] = null
17-
}
14+
export const setActiveTable = createAction('SET_ACTIVE_GTFSPLUS_TABLE')
1815

19-
return {
20-
type: 'ADD_GTFSPLUS_ROW',
21-
tableId,
22-
rowData
23-
}
24-
}
16+
export const setCurrentPage = createAction('SET_CURRENT_GTFSPLUS_PAGE')
2517

26-
export function updateGtfsPlusField (tableId, rowIndex, fieldName, newValue) {
27-
return {
28-
type: 'UPDATE_GTFSPLUS_FIELD',
29-
tableId,
30-
rowIndex,
31-
fieldName,
32-
newValue
33-
}
34-
}
18+
export const setVisibilityFilter = createAction('SET_GTFSPLUS_VISIBILITY')
19+
20+
export const updateGtfsPlusField = createAction('UPDATE_GTFSPLUS_FIELD')
3521

3622
export function deleteGtfsPlusRow (tableId, rowIndex) {
3723
return {
@@ -227,8 +213,6 @@ export function loadGtfsEntities (tableId, rows, feedSource, feedVersionId) {
227213
// in the published version.
228214
if (routesToLoad.length === 0 && stopsToLoad.length === 0) return
229215
const feedId = feedVersionId.replace('.zip', '')
230-
console.log(`fetching routes ${routesToLoad.join(',')}`)
231-
console.log(`fetching stops ${stopsToLoad.join(',')}`)
232216
var loadRoutes = Promise.all(routesToLoad.map(routeId => {
233217
const url = `/api/manager/routes/${routeId}?feed=${feedId}`
234218
return fetch(url)

lib/gtfsplus/components/GtfsPlusEditor.js

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, {Component, PropTypes} from 'react'
2-
import { Grid, Row, Col, Button, Glyphicon, PageHeader } from 'react-bootstrap'
2+
import {Grid, Row, Col, Button, Glyphicon, PageHeader} from 'react-bootstrap'
33
import JSZip from 'jszip'
44
import { Link } from 'react-router'
55

@@ -20,15 +20,14 @@ export default class GtfsPlusEditor extends Component {
2020
newRowClicked: PropTypes.func,
2121
onComponentMount: PropTypes.func,
2222
project: PropTypes.object,
23+
setActiveTable: PropTypes.func,
24+
setCurrentPage: PropTypes.func,
25+
setVisibilityFilter: PropTypes.func,
2326
tableData: PropTypes.object,
2427
user: PropTypes.object,
2528
validation: PropTypes.object
2629
}
2730

28-
state = {
29-
activeTableId: 'realtime_routes'
30-
}
31-
3231
componentWillMount () {
3332
this.props.onComponentMount(this.props)
3433
}
@@ -66,11 +65,11 @@ export default class GtfsPlusEditor extends Component {
6665
_gtfsEntitySelected = (type, entity) => this.props.gtfsEntitySelected(type, entity)
6766

6867
_newRowsDisplayed = (rows) => {
69-
const {feedSource, feedVersionId, loadGtfsEntities} = this.props
70-
loadGtfsEntities(this.state.activeTableId, rows, feedSource, feedVersionId)
68+
const {activeTableId, feedSource, feedVersionId, loadGtfsEntities} = this.props
69+
loadGtfsEntities(activeTableId, rows, feedSource, feedVersionId)
7170
}
7271

73-
_selectTable = (activeTableId) => this.setState({activeTableId})
72+
_selectTable = (activeTableId) => this.props.setActiveTable({activeTableId})
7473

7574
_showHelpClicked = (tableId, fieldName) => {
7675
const helpContent = fieldName
@@ -87,23 +86,25 @@ export default class GtfsPlusEditor extends Component {
8786

8887
render () {
8988
const {
89+
activeTableId,
9090
feedSource,
91-
deleteRowClicked,
92-
fieldEdited,
93-
newRowClicked,
9491
project,
9592
tableData,
9693
user,
97-
validation
94+
validation,
95+
visibleRows
9896
} = this.props
9997
if (!feedSource) return null
10098
const editingIsDisabled = !user.permissions.hasFeedPermission(feedSource.organizationId, feedSource.projectId, feedSource.id, 'edit-gtfs')
10199
const buttonStyle = {
102100
display: 'block',
103-
width: '100%'
101+
width: '100%',
102+
overflow: 'hidden',
103+
textOverflow: 'ellipsis',
104+
whiteSpace: 'nowrap'
104105
}
105106

106-
const activeTable = getConfigProperty('modules.gtfsplus.spec').find(t => t.id === this.state.activeTableId)
107+
const activeTable = getConfigProperty('modules.gtfsplus.spec').find(t => t.id === activeTableId)
107108

108109
return (
109110
<ManagerPage ref='page'>
@@ -137,11 +138,16 @@ export default class GtfsPlusEditor extends Component {
137138
<Row>
138139
<Col xs={2}>
139140
{getConfigProperty('modules.gtfsplus.spec').map((table, index) => {
141+
const tableLength = tableData[table.id] && tableData[table.id].length
140142
return (<p key={index}>
141143
<OptionButton
142-
bsStyle={table.id === this.state.activeTableId ? 'info' : 'default'}
144+
active={table.id === activeTableId}
143145
key={table.id}
144-
style={buttonStyle}
146+
style={{
147+
color: tableLength ? 'black' : 'grey',
148+
...buttonStyle
149+
}}
150+
title={table.id}
145151
value={table.id}
146152
onClick={this._selectTable}>
147153
{validation && (table.id in validation)
@@ -155,14 +161,11 @@ export default class GtfsPlusEditor extends Component {
155161
</Col>
156162
<Col xs={10}>
157163
<GtfsPlusTable
164+
{...this.props}
158165
ref='activeTable'
159-
feedSource={feedSource}
160166
table={activeTable}
161-
tableData={tableData[activeTable.id]}
162-
validation={validation[activeTable.id]}
163-
newRowClicked={newRowClicked}
164-
deleteRowClicked={deleteRowClicked}
165-
fieldEdited={fieldEdited}
167+
rows={visibleRows}
168+
validation={validation[activeTable.id] || []}
166169
gtfsEntitySelected={this._gtfsEntitySelected}
167170
getGtfsEntity={this._getGtfsEntity}
168171
showHelpClicked={this._showHelpClicked}

lib/gtfsplus/components/GtfsPlusField.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,26 @@ export default class GtfsPlusField extends Component {
1717
}
1818

1919
_onChangeRoute = ({route}) => {
20-
const {field, fieldEdited, gtfsEntitySelected, row, table} = this.props
21-
fieldEdited(table.id, row, field.name, route.route_id)
20+
const {field, fieldEdited, gtfsEntitySelected, row: rowIndex, table} = this.props
21+
// tableId, rowIndex, fieldName, newValue
22+
fieldEdited({tableId: table.id, rowIndex, fieldName: field.name, newValue: route.route_id})
2223
gtfsEntitySelected('route', route)
2324
}
2425

2526
_onChangeStop = ({stop}) => {
26-
const {field, fieldEdited, gtfsEntitySelected, row, table} = this.props
27-
fieldEdited(table.id, row, field.name, stop.stop_id)
27+
const {field, fieldEdited, gtfsEntitySelected, row: rowIndex, table} = this.props
28+
fieldEdited({tableId: table.id, rowIndex, fieldName: field.name, newValue: stop.stop_id})
2829
gtfsEntitySelected('stop', stop)
2930
}
3031

31-
_onChangeValue = (value) => {
32-
const {field, fieldEdited, row, table} = this.props
33-
fieldEdited(table.id, row, field.name, value)
32+
_onChangeValue = (newValue) => {
33+
const {field, fieldEdited, row: rowIndex, table} = this.props
34+
fieldEdited({tableId: table.id, rowIndex, fieldName: field.name, newValue})
3435
}
3536

3637
_onChange = ({target}) => {
37-
const {field, fieldEdited, row, table} = this.props
38-
fieldEdited(table.id, row, field.name, target.value)
38+
const {field, fieldEdited, row: rowIndex, table} = this.props
39+
fieldEdited({tableId: table.id, rowIndex, fieldName: field.name, newValue: target.value})
3940
}
4041

4142
render () {
@@ -56,7 +57,7 @@ export default class GtfsPlusField extends Component {
5657
const option = currentValue !== null && field.options.find(o => o.value.toUpperCase() === currentValue.toUpperCase())
5758
return (
5859
<FormControl componentClass='select'
59-
value={option ? option.value : undefined}
60+
value={option ? option.value : ''}
6061
onChange={this._onChange}>
6162
{/* Add field for empty string value if that is not an allowable option so that user selection triggers onChange */}
6263
{field.options.findIndex(option => option.value === '') === -1

0 commit comments

Comments
 (0)