Skip to content

Commit b4163e2

Browse files
author
Landon Reed
authored
Merge pull request #55 from catalogueglobal/dev
Version release
2 parents 948ba43 + 067f619 commit b4163e2

File tree

7 files changed

+375
-127
lines changed

7 files changed

+375
-127
lines changed

lib/gtfs/reducers/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ import feed from './feed'
55
import patterns from './patterns'
66
import routes from './routes'
77
import stops from './stops'
8+
import validation from './validation'
89

910
export default combineReducers({
1011
filter,
1112
feed,
1213
patterns,
1314
routes,
14-
stops
15+
stops,
16+
validation
1517
})

lib/gtfs/reducers/validation.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import update from 'react-addons-update'
2+
3+
import {getEntityGraphQLRoot, getEntityIdField} from '../../gtfs/util'
4+
5+
const defaultState = {
6+
fetchStatus: {
7+
fetched: false,
8+
fetching: false,
9+
error: false
10+
},
11+
data: []
12+
}
13+
14+
export default function reducer (state = defaultState, action) {
15+
switch (action.type) {
16+
case 'SET_ACTIVE_FEEDVERSION':
17+
return defaultState
18+
case 'FETCH_GRAPHQL_ROUTES':
19+
return {
20+
fetchStatus: {
21+
fetched: false,
22+
fetching: true,
23+
error: false
24+
},
25+
data: []
26+
}
27+
case 'FETCH_GRAPHQL_ROUTES_REJECTED':
28+
return update(state, {
29+
fetchStatus: {
30+
$set: {
31+
fetched: false,
32+
fetching: false,
33+
error: true
34+
}
35+
}
36+
})
37+
case 'RECEIVE_GTFS_ENTITIES':
38+
const type = action.payload.type
39+
const entities = action.payload.data.feed[getEntityGraphQLRoot(type)]
40+
entities.forEach(entity => {
41+
const id = entity[getEntityIdField(type)]
42+
entity._id = `${type}:${id}`
43+
})
44+
return update(state, {
45+
data: {$push: entities}
46+
})
47+
default:
48+
return state
49+
}
50+
}

lib/gtfs/util/index.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// @flow
2+
3+
export function getEntityIdField (type: string): string {
4+
if (!type) return ''
5+
switch (type.toLowerCase()) {
6+
case 'stop':
7+
return 'stop_id'
8+
case 'route':
9+
return 'route_id'
10+
case 'trip':
11+
return 'trip_id'
12+
case 'stoptime':
13+
return 'trip_id'
14+
case 'service':
15+
return 'service_id'
16+
case 'pattern':
17+
return 'pattern_id'
18+
default:
19+
return ''
20+
}
21+
}
22+
23+
export function getEntityGraphQLRoot (type: string): string {
24+
if (!type) return ''
25+
switch (type.toLowerCase()) {
26+
case 'stop':
27+
return 'stops'
28+
case 'route':
29+
return 'routes'
30+
case 'trip':
31+
return 'trips'
32+
case 'stoptime':
33+
return 'trips'
34+
case 'service':
35+
return 'services'
36+
case 'pattern':
37+
return 'patterns'
38+
default:
39+
return ''
40+
}
41+
}
42+
43+
export function getEntityTableString (type: string): string {
44+
if (!type) return ''
45+
switch (type.toLowerCase()) {
46+
case 'stop':
47+
return 'stop'
48+
case 'route':
49+
return 'route'
50+
case 'trip':
51+
return 'trip'
52+
case 'stoptime':
53+
return 'stop_time'
54+
case 'service':
55+
return 'services'
56+
case 'pattern':
57+
return 'patterns'
58+
default:
59+
return ''
60+
}
61+
}

lib/manager/actions/versions.js

Lines changed: 66 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {GTFS_GRAPHQL_PREFIX, SECURE_API_PREFIX} from '../../common/constants'
88
import {getConfigProperty} from '../../common/util/config'
99
import {uploadFile} from '../../common/util/upload-file'
1010
import fileDownload from '../../common/util/file-download'
11+
import {getEntityGraphQLRoot, getEntityIdField, getEntityTableString} from '../../gtfs/util'
1112
import {setErrorMessage, startJobMonitor} from './status'
1213
import {fetchFeedSource} from './feeds'
1314

@@ -192,10 +193,57 @@ export function receiveValidationResult (feedVersion, validationResult) {
192193
const fetchingValidationErrors = createAction('FETCHING_VALIDATION_ERRORS')
193194

194195
const receiveValidationErrors = createAction('RECEIVE_VALIDATION_ERRORS')
196+
const fetchingGTFSEntities = createAction('FETCHING_GTFS_ENTITIES')
197+
const receiveGTFSEntities = createAction('RECEIVE_GTFS_ENTITIES')
198+
199+
export function fetchGTFSEntities ({feedVersion, id, type}) {
200+
return function (dispatch, getState) {
201+
const {namespace} = feedVersion
202+
const entityIdField = getEntityIdField(type)
203+
const entityTableString = getEntityTableString(type)
204+
const graphQLRoot = getEntityGraphQLRoot(type)
205+
const gtfsSpec = getConfigProperty('modules.editor.spec')
206+
const table = gtfsSpec.find(table => table.id === entityTableString)
207+
let fields = ''
208+
if (table) {
209+
fields = table.fields
210+
.filter(field => field.required && !field.datatools)
211+
.map(field => field.name)
212+
.join('\n')
213+
// stop_times are a special case because they must be requested as a
214+
// nested list underneath a trip
215+
if (type === 'StopTime') {
216+
fields = `
217+
trip_id
218+
stop_times {
219+
${fields}
220+
}
221+
`
222+
}
223+
}
224+
const query = `
225+
query entityQuery($namespace: String, $${entityIdField}: [String]) {
226+
feed(namespace: $namespace) {
227+
feed_id
228+
feed_version
229+
filename
230+
${graphQLRoot} (${entityIdField}: $${entityIdField}) {
231+
${fields}
232+
}
233+
}
234+
}
235+
`
236+
dispatch(fetchingGTFSEntities({feedVersion, id, type}))
237+
return fetchGraphQL({query, variables: {namespace, [entityIdField]: id}})
238+
.then(response => response.json())
239+
.then(data => dispatch(receiveGTFSEntities({feedVersion, id, type, data})))
240+
.catch(err => console.log(err))
241+
}
242+
}
195243

196244
export function fetchValidationErrors ({feedVersion, errorType, limit, offset}) {
197245
return function (dispatch, getState) {
198-
dispatch(fetchingValidationErrors)
246+
dispatch(fetchingValidationErrors({feedVersion, errorType, limit, offset}))
199247
// FIXME: why does namespace need to appear twice?
200248
const query = `
201249
query errorsQuery($namespace: String, $errorType: [String], $limit: Int, $offset: Int) {
@@ -209,17 +257,13 @@ export function fetchValidationErrors ({feedVersion, errorType, limit, offset})
209257
entity_id
210258
line_number
211259
bad_value
260+
entity_sequence
212261
}
213262
}
214263
}
215264
`
216265
const {namespace} = feedVersion
217-
const method = 'post'
218-
const body = JSON.stringify({
219-
query,
220-
variables: JSON.stringify({namespace, errorType: [errorType], limit, offset})
221-
})
222-
return fetch(GTFS_GRAPHQL_PREFIX, {method, body})
266+
return fetchGraphQL({query, variables: {namespace, errorType: [errorType], limit, offset}})
223267
.then(response => response.json())
224268
.then(result => {
225269
if (result.feed) {
@@ -231,6 +275,19 @@ export function fetchValidationErrors ({feedVersion, errorType, limit, offset})
231275
}
232276
}
233277

278+
function fetchGraphQL ({query, variables, ...requestParams}) {
279+
const body = JSON.stringify({
280+
query,
281+
variables: JSON.stringify(variables)
282+
})
283+
return fetch(GTFS_GRAPHQL_PREFIX, {
284+
method: 'post',
285+
body,
286+
...requestParams,
287+
headers: {'Content-Type': 'application/json'}
288+
})
289+
}
290+
234291
export function fetchValidationResult (feedVersion, isPublic) {
235292
return function (dispatch, getState) {
236293
dispatch(requestingValidationResult(feedVersion))
@@ -248,17 +305,12 @@ export function fetchValidationResult (feedVersion, isPublic) {
248305
errors
249306
}
250307
error_counts {
251-
type, count
308+
type count message
252309
}
253310
}
254311
}
255312
`
256-
const method = 'post'
257-
const body = JSON.stringify({
258-
query,
259-
variables: JSON.stringify({namespace})
260-
})
261-
return fetch(GTFS_GRAPHQL_PREFIX, {method, body})
313+
return fetchGraphQL({query, variables: {namespace}})
262314
.then(response => response.json())
263315
.then(result => {
264316
if (result.feed) {

0 commit comments

Comments
 (0)