Skip to content

Commit

Permalink
Merge pull request #55 from catalogueglobal/dev
Browse files Browse the repository at this point in the history
Version release
  • Loading branch information
Landon Reed authored Nov 21, 2017
2 parents 948ba43 + 067f619 commit b4163e2
Show file tree
Hide file tree
Showing 7 changed files with 375 additions and 127 deletions.
4 changes: 3 additions & 1 deletion lib/gtfs/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import feed from './feed'
import patterns from './patterns'
import routes from './routes'
import stops from './stops'
import validation from './validation'

export default combineReducers({
filter,
feed,
patterns,
routes,
stops
stops,
validation
})
50 changes: 50 additions & 0 deletions lib/gtfs/reducers/validation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import update from 'react-addons-update'

import {getEntityGraphQLRoot, getEntityIdField} from '../../gtfs/util'

const defaultState = {
fetchStatus: {
fetched: false,
fetching: false,
error: false
},
data: []
}

export default function reducer (state = defaultState, action) {
switch (action.type) {
case 'SET_ACTIVE_FEEDVERSION':
return defaultState
case 'FETCH_GRAPHQL_ROUTES':
return {
fetchStatus: {
fetched: false,
fetching: true,
error: false
},
data: []
}
case 'FETCH_GRAPHQL_ROUTES_REJECTED':
return update(state, {
fetchStatus: {
$set: {
fetched: false,
fetching: false,
error: true
}
}
})
case 'RECEIVE_GTFS_ENTITIES':
const type = action.payload.type
const entities = action.payload.data.feed[getEntityGraphQLRoot(type)]
entities.forEach(entity => {
const id = entity[getEntityIdField(type)]
entity._id = `${type}:${id}`
})
return update(state, {
data: {$push: entities}
})
default:
return state
}
}
61 changes: 61 additions & 0 deletions lib/gtfs/util/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// @flow

export function getEntityIdField (type: string): string {
if (!type) return ''
switch (type.toLowerCase()) {
case 'stop':
return 'stop_id'
case 'route':
return 'route_id'
case 'trip':
return 'trip_id'
case 'stoptime':
return 'trip_id'
case 'service':
return 'service_id'
case 'pattern':
return 'pattern_id'
default:
return ''
}
}

export function getEntityGraphQLRoot (type: string): string {
if (!type) return ''
switch (type.toLowerCase()) {
case 'stop':
return 'stops'
case 'route':
return 'routes'
case 'trip':
return 'trips'
case 'stoptime':
return 'trips'
case 'service':
return 'services'
case 'pattern':
return 'patterns'
default:
return ''
}
}

export function getEntityTableString (type: string): string {
if (!type) return ''
switch (type.toLowerCase()) {
case 'stop':
return 'stop'
case 'route':
return 'route'
case 'trip':
return 'trip'
case 'stoptime':
return 'stop_time'
case 'service':
return 'services'
case 'pattern':
return 'patterns'
default:
return ''
}
}
80 changes: 66 additions & 14 deletions lib/manager/actions/versions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {GTFS_GRAPHQL_PREFIX, SECURE_API_PREFIX} from '../../common/constants'
import {getConfigProperty} from '../../common/util/config'
import {uploadFile} from '../../common/util/upload-file'
import fileDownload from '../../common/util/file-download'
import {getEntityGraphQLRoot, getEntityIdField, getEntityTableString} from '../../gtfs/util'
import {setErrorMessage, startJobMonitor} from './status'
import {fetchFeedSource} from './feeds'

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

const receiveValidationErrors = createAction('RECEIVE_VALIDATION_ERRORS')
const fetchingGTFSEntities = createAction('FETCHING_GTFS_ENTITIES')
const receiveGTFSEntities = createAction('RECEIVE_GTFS_ENTITIES')

export function fetchGTFSEntities ({feedVersion, id, type}) {
return function (dispatch, getState) {
const {namespace} = feedVersion
const entityIdField = getEntityIdField(type)
const entityTableString = getEntityTableString(type)
const graphQLRoot = getEntityGraphQLRoot(type)
const gtfsSpec = getConfigProperty('modules.editor.spec')
const table = gtfsSpec.find(table => table.id === entityTableString)
let fields = ''
if (table) {
fields = table.fields
.filter(field => 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
if (type === 'StopTime') {
fields = `
trip_id
stop_times {
${fields}
}
`
}
}
const query = `
query entityQuery($namespace: String, $${entityIdField}: [String]) {
feed(namespace: $namespace) {
feed_id
feed_version
filename
${graphQLRoot} (${entityIdField}: $${entityIdField}) {
${fields}
}
}
}
`
dispatch(fetchingGTFSEntities({feedVersion, id, type}))
return fetchGraphQL({query, variables: {namespace, [entityIdField]: id}})
.then(response => response.json())
.then(data => dispatch(receiveGTFSEntities({feedVersion, id, type, data})))
.catch(err => console.log(err))
}
}

export function fetchValidationErrors ({feedVersion, errorType, limit, offset}) {
return function (dispatch, getState) {
dispatch(fetchingValidationErrors)
dispatch(fetchingValidationErrors({feedVersion, errorType, limit, offset}))
// FIXME: why does namespace need to appear twice?
const query = `
query errorsQuery($namespace: String, $errorType: [String], $limit: Int, $offset: Int) {
Expand All @@ -209,17 +257,13 @@ export function fetchValidationErrors ({feedVersion, errorType, limit, offset})
entity_id
line_number
bad_value
entity_sequence
}
}
}
`
const {namespace} = feedVersion
const method = 'post'
const body = JSON.stringify({
query,
variables: JSON.stringify({namespace, errorType: [errorType], limit, offset})
})
return fetch(GTFS_GRAPHQL_PREFIX, {method, body})
return fetchGraphQL({query, variables: {namespace, errorType: [errorType], limit, offset}})
.then(response => response.json())
.then(result => {
if (result.feed) {
Expand All @@ -231,6 +275,19 @@ export function fetchValidationErrors ({feedVersion, errorType, limit, offset})
}
}

function fetchGraphQL ({query, variables, ...requestParams}) {
const body = JSON.stringify({
query,
variables: JSON.stringify(variables)
})
return fetch(GTFS_GRAPHQL_PREFIX, {
method: 'post',
body,
...requestParams,
headers: {'Content-Type': 'application/json'}
})
}

export function fetchValidationResult (feedVersion, isPublic) {
return function (dispatch, getState) {
dispatch(requestingValidationResult(feedVersion))
Expand All @@ -248,17 +305,12 @@ export function fetchValidationResult (feedVersion, isPublic) {
errors
}
error_counts {
type, count
type count message
}
}
}
`
const method = 'post'
const body = JSON.stringify({
query,
variables: JSON.stringify({namespace})
})
return fetch(GTFS_GRAPHQL_PREFIX, {method, body})
return fetchGraphQL({query, variables: {namespace}})
.then(response => response.json())
.then(result => {
if (result.feed) {
Expand Down
Loading

0 comments on commit b4163e2

Please sign in to comment.