Skip to content

Commit

Permalink
fix(graphql): fix graphql reducers, more createAction updates
Browse files Browse the repository at this point in the history
  • Loading branch information
landonreed committed Jan 16, 2018
1 parent c5b17ab commit 7054dd7
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 33 deletions.
14 changes: 11 additions & 3 deletions lib/gtfs/reducers/stops.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,17 @@ export default function reducer (state = defaultState, action) {
data: {$set: action.stops}
})
case 'FETCH_GRAPHQL_STOPS_FULFILLED':
const allRoutes = action.data.routes || []
let {feed} = action.payload.data
// FIXME: when stops fetched for pattern, an error is not caught
let hasError = false
// If no feed is returned, there was an uncaught error during fetch.
if (!feed) {
feed = {routes: [], stops: []}
hasError = true
}
const allRoutes = feed.routes || []
const allPatterns = []
const allStops = action.data.stops || []
const allStops = feed.stops || []

for (let i = 0; i < allRoutes.length; i++) {
const route = allRoutes[i]
Expand All @@ -80,7 +88,7 @@ export default function reducer (state = defaultState, action) {
$set: {
fetched: true,
fetching: false,
error: false
error: hasError
}
},
data: {$set: allStops}
Expand Down
12 changes: 8 additions & 4 deletions lib/gtfs/reducers/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,15 @@ export default function reducer (state = defaultState, action) {
}
})
case 'RECEIVE_GTFS_ENTITIES':
const type = action.payload.type
const entities = action.payload.data.feed[getEntityGraphQLRoot(type)]
const {data, editor, component} = action.payload
if (editor) {
// Ignore entity fetches for the editor
return state
}
const entities = data.feed[getEntityGraphQLRoot(component)]
entities.forEach(entity => {
const id = entity[getEntityIdField(type)]
entity._id = `${type}:${id}`
const id = entity[getEntityIdField(component)]
entity._id = `${component}:${id}`
})
return update(state, {
data: {$push: entities}
Expand Down
32 changes: 18 additions & 14 deletions lib/gtfs/util/graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,24 @@ query routeQuery($namespace: String) {

// FIXME: Pattern stats are not supported yet in gtfs-api, so this is broken.
export const patterns = `
query patternsQuery($namespace: String, $routeId: [String], $date: String, $from: Long, $to: Long) {
feed (namespace: $namespace, route_id: $routeId) {
routes {
query patternsQuery($namespace: String, $routeId: [String]
#, $date: String, $from: Long, $to: Long
) {
feed (namespace: $namespace) {
routes (route_id: $routeId) {
route_id,
route_short_name,
route_long_name,
patterns {
pattern_id,
name,
geometry,
stop_count,
trip_count,
stats(date: $date, from: $from, to: $to){
headway,
avgSpeed
},
# stop_count,
# trip_count,
# stats(date: $date, from: $from, to: $to){
# headway,
# avgSpeed
# },
}
}
}
Expand Down Expand Up @@ -186,12 +188,14 @@ export function stopsFiltered (
${namespace ? '$namespace: String,' : ''}
${routeId ? '$routeId: [String],' : ''}
${patternId ? '$patternId: [String],' : ''}
${date ? '$date: String,' : ''}
${hasFrom ? '$from: Long,' : ''}
${hasTo ? '$to: Long' : ''}
# ${date ? '$date: String,' : ''}
# ${hasFrom ? '$from: Long,' : ''}
# ${hasTo ? '$to: Long' : ''}
) {
feed(${namespace ? 'namespace: $namespace,' : ''} ${routeId ? 'route_id: $routeId,' : ''} ${patternId ? 'pattern_id: $patternId,' : ''}) {
stops {
feed(${namespace ? 'namespace: $namespace' : ''}) {
stops
# (${routeId ? 'route_id: $routeId,' : ''} ${patternId ? 'pattern_id: $patternId,' : ''})
{
stop_id,
stop_name,
stop_name,
Expand Down
95 changes: 95 additions & 0 deletions lib/gtfs/util/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
// @flow

import {getConfigProperty} from '../../common/util/config'

export function getEntityIdField (type: string): string {
if (!type) return ''
switch (type.toLowerCase()) {
case 'agency':
return 'agency_id'
case 'fare':
return 'fare_id'
case 'calendar':
return 'service_id'
case 'stop':
return 'stop_id'
case 'route':
Expand All @@ -20,9 +28,87 @@ export function getEntityIdField (type: string): string {
}
}

export function getGraphQLFieldsForEntity (type, editor = false) {
const gtfsSpec = getConfigProperty('modules.editor.spec')
const entityTableString = getEntityTableString(type)
const table = gtfsSpec.find(table => table.id === entityTableString)
let fields = ''
if (table) {
fields = table.fields
// Only filter required fields if not fetching for editor
// FIXME: add missing datatools fields to graphql
.filter(field => editor ? !field.datatools : field.required && !field.datatools)
.map(field => field.name)
.join('\n')
// stop_times are a special case because they must be requested as a
// nested list underneath a trip
console.log(type)
switch (type.toLowerCase()) {
case 'stoptime':
return `
trip_id
stop_times {
${fields}
}
`
case 'fare':
console.log('getting fare fields')
return `
${fields}
fare_rules {
id
fare_id
route_id
origin_id
destination_id
contains_id
}
`
case 'route':
return `
${fields}
tripPatterns: patterns {
id
shape_id
patternId: pattern_id
tripCount: trip_count
routeId: route_id
name
patternStops: stops {
id
stop_id
default_travel_time
default_dwell_time
stop_sequence
shape_dist_traveled
}
shapePoints: shape (limit: -1) {
shape_pt_lon
shape_pt_lat
shape_pt_sequence
point_type
shape_dist_traveled
}
}
`
default:
return fields
}
} else {
console.error(`Could not find table fare entity ${type}`)
return ''
}
}

export function getEntityGraphQLRoot (type: string): string {
if (!type) return ''
switch (type.toLowerCase()) {
case 'agency':
return 'agency'
case 'calendar':
return 'calendar'
case 'fare':
return 'fares'
case 'stop':
return 'stops'
case 'route':
Expand All @@ -40,9 +126,18 @@ export function getEntityGraphQLRoot (type: string): string {
}
}

/**
* Takes a entity component type and returns the gtfs.yml table id
*/
export function getEntityTableString (type: string): string {
if (!type) return ''
switch (type.toLowerCase()) {
case 'agency':
return 'agency'
case 'calendar':
return 'calendar'
case 'fare':
return 'fare_attributes'
case 'stop':
return 'stop'
case 'route':
Expand Down
10 changes: 5 additions & 5 deletions lib/manager/components/reporter/containers/Stops.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ const mapStateToProps = (state, ownProps) => {
}

const mapDispatchToProps = (dispatch, ownProps) => {
const feedId = ownProps.version.id.replace('.zip', '')
const {namespace} = ownProps.version
return {
onComponentMount: (initialProps) => {
if (!initialProps.routes.fetchStatus.fetched) {
dispatch(fetchRoutes(feedId))
dispatch(fetchRoutes(namespace))
}
},
stopRouteFilterChange: (newValue) => {
dispatch(stopRouteFilterChange(feedId, newValue))
dispatch(stopRouteFilterChange(namespace, newValue))
},
stopPatternFilterChange: (newValue) => {
dispatch(stopPatternFilterChange(feedId, newValue))
dispatch(stopPatternFilterChange(namespace, newValue))
},
stopDateTimeFilterChange: (props) => {
dispatch(stopDateTimeFilterChange(feedId, props))
dispatch(stopDateTimeFilterChange(namespace, props))
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions lib/manager/reducers/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,9 @@ const projects = (state = {
}
})
case 'RECEIVE_VALIDATION_RESULT':
projectIndex = state.all.findIndex(p => p.id === action.feedVersion.feedSource.projectId)
sourceIndex = state.all[projectIndex].feedSources.findIndex(s => s.id === action.feedVersion.feedSource.id)
versionIndex = state.all[projectIndex].feedSources[sourceIndex].feedVersions.findIndex(v => v.id === action.feedVersion.id)
projectIndex = state.all.findIndex(p => p.id === action.payload.feedVersion.feedSource.projectId)
sourceIndex = state.all[projectIndex].feedSources.findIndex(s => s.id === action.payload.feedVersion.feedSource.id)
versionIndex = state.all[projectIndex].feedSources[sourceIndex].feedVersions.findIndex(v => v.id === action.payload.feedVersion.id)
// let result = {}
// action.validationResult.map(error => {
// if (!result[error.file]) {
Expand All @@ -243,7 +243,7 @@ const projects = (state = {
feedVersions: {
[versionIndex]: {
$merge: {
validationResult: action.validationResult
validationResult: action.payload.validationResult
}
}
}
Expand Down Expand Up @@ -401,15 +401,15 @@ const projects = (state = {
})

case 'RECEIVE_GTFSEDITOR_SNAPSHOTS':
projectIndex = state.all.findIndex(p => p.id === action.feedSource.projectId)
sourceIndex = state.all[projectIndex].feedSources.findIndex(s => s.id === action.feedSource.id)
projectIndex = state.all.findIndex(p => p.id === action.payload.feedSource.projectId)
sourceIndex = state.all[projectIndex].feedSources.findIndex(s => s.id === action.payload.feedSource.id)
return update(state, {
all: {
[projectIndex]: {
feedSources: {
[sourceIndex]: {
$merge: {
editorSnapshots: action.snapshots
editorSnapshots: action.payload.snapshots
}
}
}
Expand Down

0 comments on commit 7054dd7

Please sign in to comment.