@@ -8,6 +8,7 @@ import {GTFS_GRAPHQL_PREFIX, SECURE_API_PREFIX} from '../../common/constants'
88import { getConfigProperty } from '../../common/util/config'
99import { uploadFile } from '../../common/util/upload-file'
1010import fileDownload from '../../common/util/file-download'
11+ import { getEntityGraphQLRoot , getEntityIdField , getEntityTableString } from '../../gtfs/util'
1112import { setErrorMessage , startJobMonitor } from './status'
1213import { fetchFeedSource } from './feeds'
1314
@@ -192,10 +193,57 @@ export function receiveValidationResult (feedVersion, validationResult) {
192193const fetchingValidationErrors = createAction ( 'FETCHING_VALIDATION_ERRORS' )
193194
194195const 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
196244export 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+
234291export 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