Skip to content

Commit

Permalink
fix(graphql): update graphql to match new gtfs-api
Browse files Browse the repository at this point in the history
  • Loading branch information
landonreed committed Nov 8, 2017
1 parent 92194c3 commit b53d7e8
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 153 deletions.
10 changes: 5 additions & 5 deletions lib/gtfs/actions/feed.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,21 @@ function receiveFeed (feedId, data) {
}
}

export function fetchFeed (feedId, date, from, to) {
export function fetchFeed (namespace, date, from, to) {
return function (dispatch, getState) {
dispatch(fetchingFeed(feedId, date, from, to))
dispatch(fetchingFeed(namespace, date, from, to))
return fetch(compose(
feed // (feedId, date, from, to)
, {feedId, date, from, to})
, {namespace, date, from, to})
)
.then((response) => {
if (response.status >= 300) {
return dispatch(errorFetchingFeed(feedId, date, from, to))
return dispatch(errorFetchingFeed(namespace, date, from, to))
}
return response.json()
})
.then(json => {
dispatch(receiveFeed(feedId, json))
dispatch(receiveFeed(namespace, json))
})
}
}
Expand Down
59 changes: 22 additions & 37 deletions lib/gtfs/actions/patterns.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,43 @@
import fetch from 'isomorphic-fetch'
import {createAction} from 'redux-actions'

import { compose, patterns } from '../../gtfs/util/graphql'

export function fetchingPatterns (feedId, routeId) {
return {
type: 'FETCH_GRAPHQL_PATTERNS',
feedId,
routeId
}
}

export function clearPatterns () {
return {
type: 'CLEAR_GRAPHQL_PATTERNS'
}
}

export function errorFetchingPatterns (feedId, data) {
return {
type: 'FETCH_GRAPHQL_PATTERNS_REJECTED',
data
}
}

export function receivePatterns (feedId, data) {
return {
type: 'FETCH_GRAPHQL_PATTERNS_FULFILLED',
data
}
}
export const fetchingPatterns = createAction('FETCH_GRAPHQL_PATTERNS')
export const clearPatterns = createAction('CLEAR_GRAPHQL_PATTERNS')
export const errorFetchingPatterns = createAction('FETCH_GRAPHQL_PATTERNS_REJECTED')
export const receivePatterns = createAction('FETCH_GRAPHQL_PATTERNS_FULFILLED')

export function patternDateTimeFilterChange (feedId, props) {
export function patternDateTimeFilterChange (namespace, props) {
return function (dispatch, getState) {
const routeId = getState().gtfs.patterns.routeFilter
const { date, from, to } = getState().gtfs.filter.dateTimeFilter
dispatch(fetchPatterns(feedId, routeId, date, from, to))
dispatch(fetchPatterns(namespace, routeId, date, from, to))
}
}

export function fetchPatterns (feedId, routeId, date, from, to) {
export function fetchPatterns (namespace, routeId, date, from, to) {
return function (dispatch, getState) {
dispatch(fetchingPatterns(feedId, routeId, date, from, to))
dispatch(fetchingPatterns({namespace, routeId, date, from, to}))
// FIXME: if routeId is null, clear current routes so we can fetch all routes
if (!routeId) {
return dispatch(receivePatterns(feedId, {routes: []}))
const routes = []
return dispatch(receivePatterns({namespace, routes}))
}
return fetch(compose(patterns, {feedId, routeId, date, from, to}))
return fetch(compose(patterns, {namespace, routeId, date, from, to}))
.then((response) => {
if (response.status >= 300) {
return dispatch(errorFetchingPatterns(feedId, routeId))
return dispatch(errorFetchingPatterns(namespace, routeId))
}
return response.json()
})
.then(json => {
dispatch(receivePatterns(feedId, json))
if (json && json.feed) {
const {routes} = json.feed
dispatch(receivePatterns({namespace, routes}))
} else {
console.log('Error fetching patterns')
}
})
}
}
Expand All @@ -64,11 +49,11 @@ export function updateRouteFilter (routeId) {
}
}

export function patternRouteFilterChange (feedId, routeData) {
export function patternRouteFilterChange (namespace, routeData) {
return function (dispatch, getState) {
const newRouteId = (routeData && routeData.route_id) ? routeData.route_id : null
const {date, from, to} = getState().gtfs.filter.dateTimeFilter
dispatch(updateRouteFilter(newRouteId))
dispatch(fetchPatterns(feedId, newRouteId, date, from, to))
dispatch(fetchPatterns(namespace, newRouteId, date, from, to))
}
}
40 changes: 10 additions & 30 deletions lib/gtfs/actions/routes.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,22 @@
import fetch from 'isomorphic-fetch'
import {createAction} from 'redux-actions'
import { compose, routes } from '../../gtfs/util/graphql'

export function fetchingRoutes (feedId) {
return {
type: 'FETCH_GRAPHQL_ROUTES',
feedId
}
}

export function clearRoutes () {
return {
type: 'CLEAR_GRAPHQL_ROUTES'
}
}

export function errorFetchingRoutes (feedId, data) {
return {
type: 'FETCH_GRAPHQL_ROUTES_REJECTED',
data
}
}

export function receiveRoutes (feedId, data) {
return {
type: 'FETCH_GRAPHQL_ROUTES_FULFILLED',
data
}
}
export const fetchingRoutes = createAction('FETCH_GRAPHQL_ROUTES')
export const clearRoutes = createAction('CLEAR_GRAPHQL_ROUTES')
export const errorFetchingRoutes = createAction('FETCH_GRAPHQL_ROUTES_REJECTED')
const receiveRoutes = createAction('FETCH_GRAPHQL_ROUTES_FULFILLED')

export function fetchRoutes (feedId) {
export function fetchRoutes (namespace) {
return function (dispatch, getState) {
dispatch(fetchingRoutes(feedId))
return fetch(compose(routes, { feedId: feedId }))
dispatch(fetchingRoutes(namespace))
return fetch(compose(routes, { namespace }))
.then((response) => {
return response.json()
})
.then(json => {
dispatch(receiveRoutes(feedId, json))
const {routes} = json.feed
dispatch(receiveRoutes({namespace, routes}))
})
}
}
15 changes: 8 additions & 7 deletions lib/gtfs/reducers/patterns.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,16 @@ export default function reducer (state = defaultState, action) {
data: {$set: action.patterns}
})
case 'FETCH_GRAPHQL_PATTERNS_FULFILLED':
const allRoutes = action.data ? action.data.routes : []
const {routes} = action.payload
const allPatterns = []

for (let i = 0; i < allRoutes.length; i++) {
const curRouteId = allRoutes[i].route_id
const curRouteName = getRouteName(allRoutes[i])

for (let j = 0; j < allRoutes[i].patterns.length; j++) {
const curPattern = allRoutes[i].patterns[j]
// iterate over routes from graphql to set route name
for (let i = 0; i < routes.length; i++) {
const curRouteId = routes[i].route_id
const curRouteName = getRouteName(routes[i])
// iterate over patterns from graphql to add route ref fields
for (let j = 0; j < routes[i].patterns.length; j++) {
const curPattern = routes[i].patterns[j]
curPattern.route_id = curRouteId
curPattern.route_name = curRouteName
allPatterns.push(curPattern)
Expand Down
4 changes: 2 additions & 2 deletions lib/gtfs/reducers/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ export default function reducer (state = defaultState, action) {
})
case 'FETCH_GRAPHQL_ROUTES_FULFILLED':
const newRoutes = []
for (let i = 0; i < action.data.routes.length; i++) {
const curRoute = action.data.routes[i]
for (let i = 0; i < action.payload.routes.length; i++) {
const curRoute = action.payload.routes[i]
curRoute.route_name = getRouteName(curRoute)
newRoutes.push(curRoute)
}
Expand Down
Loading

0 comments on commit b53d7e8

Please sign in to comment.