Skip to content

Commit bb9ac90

Browse files
committed
Read stop from fragment instead of looping through stopTimes
1 parent ed1c042 commit bb9ac90

File tree

8 files changed

+56
-42
lines changed

8 files changed

+56
-42
lines changed

src/apollo/client.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@ const authMiddleware = new ApolloLink((operation, forward) => {
2020

2121
const client = new ApolloClient({
2222
link: concat(authMiddleware, link),
23-
cache: new InMemoryCache(),
23+
cache: new InMemoryCache({
24+
typePolicies: {
25+
Stop: {
26+
keyFields: Stop => `${Stop.__typename}:${Stop.stopId}`,
27+
},
28+
},
29+
}),
2430
});
2531

2632
export default client;

src/apollo/fragments.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { gql } from '@apollo/client';
2+
3+
export const STOP_FIELDS = gql`
4+
fragment StopFields on Stop {
5+
stopId
6+
stopName
7+
geom {
8+
coordinates
9+
}
10+
}
11+
`;

src/components/Stop.tsx

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import React, { FC } from 'react';
22
import MapboxGL, { CircleLayerStyle } from '@react-native-mapbox-gl/maps';
3-
import { IStop, IStopTime, ITrip } from 'interfaces';
3+
import { IStop } from 'interfaces';
44
import * as turf from '@turf/turf';
55

66
type Props = {
7-
stopId: string;
8-
trip: ITrip;
9-
goToStop: (stop: IStop) => void;
7+
stop: IStop;
8+
color: string;
9+
aboveLayerID: string;
10+
onPress: (stop: IStop) => void;
1011
};
1112

1213
const getCircleStyles = (color?: string): CircleLayerStyle => ({
@@ -18,26 +19,20 @@ const getCircleStyles = (color?: string): CircleLayerStyle => ({
1819
circlePitchAlignment: 'map',
1920
});
2021

21-
const Stop: FC<Props> = ({ stopId, trip, goToStop }) => {
22-
const stopTime: IStopTime | any = trip.stopTimes.find(
23-
(st: IStopTime) => st.stop.stopId === stopId,
24-
);
25-
const { stop }: { stop: IStop } = stopTime || {};
26-
const { shapeId } = trip;
22+
const Stop: FC<Props> = ({ stop, color, aboveLayerID, onPress }) => {
2723
const point = turf.point(stop.geom.coordinates);
28-
const color = trip.route.routeColor;
2924

3025
return (
3126
<MapboxGL.ShapeSource
3227
id={`shape-source-${stop.stopId}`}
3328
key={`${stop.stopId}`}
3429
shape={point}
35-
onPress={() => goToStop(stop)}>
30+
onPress={() => onPress(stop)}>
3631
<MapboxGL.CircleLayer
3732
id={`circle-layer-${stop.stopId}`}
3833
style={getCircleStyles(color)}
3934
minZoomLevel={12}
40-
aboveLayerID={`line-layer-${shapeId}`}
35+
aboveLayerID={aboveLayerID}
4136
/>
4237
</MapboxGL.ShapeSource>
4338
);

src/components/TripShape.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@ const getLineStyles = (trip: ITrip | null) => ({
1414
lineOpacity: 0.75,
1515
});
1616

17-
const TripShape: FC<Props> = ({ shape, trip }) => {
18-
const { shapeId, geom } = shape;
19-
const lineString = turf.lineString(geom.coordinates as any);
17+
const TripShape: FC<Props> = ({ shape = {}, trip }) => {
18+
let { shapeId, geom } = shape;
19+
20+
const lineString = turf.lineString(geom?.coordinates as any);
2021

2122
return (
22-
<MapboxGL.ShapeSource
23-
id={`shape-source-${shape.shapeId}`}
24-
shape={lineString}>
23+
<MapboxGL.ShapeSource id={`shape-source-${shapeId}`} shape={lineString}>
2524
<MapboxGL.LineLayer
2625
id={`line-layer-${shapeId}`}
2726
style={getLineStyles(trip)}

src/interfaces/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
type Coordinate = [number, number];
2+
13
export interface IRoute {
24
feedIndex: number;
35
routeId: string;
@@ -11,7 +13,7 @@ export interface IStop {
1113
stopId: string;
1214
stopName: string;
1315
geom: {
14-
coordinates: [number, number];
16+
coordinates: Coordinate;
1517
};
1618
}
1719

@@ -41,6 +43,6 @@ export interface IShape {
4143
length: number;
4244
geom: {
4345
type: string;
44-
coordinates: [number, number];
46+
coordinates: Coordinate[];
4547
};
4648
}

src/screens/dashboard/DashboardScreen.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
} from 'react-native-navigation-hooks';
1313
import { gql, useQuery } from '@apollo/client';
1414
import { Screens } from 'navigation/screens';
15-
import { Route } from 'interfaces';
15+
import { IRoute } from 'interfaces';
1616
import styles from './styles';
1717

1818
const DashboardScreen: FC = () => {
@@ -37,7 +37,7 @@ const DashboardScreen: FC = () => {
3737
feedIndex: number;
3838
}
3939

40-
const { loading, error, data } = useQuery<{ routes: Route[] }, RouteVars>(
40+
const { loading, error, data } = useQuery<{ routes: IRoute[] }, RouteVars>(
4141
GET_ROUTES,
4242
{
4343
variables: {
@@ -46,7 +46,7 @@ const DashboardScreen: FC = () => {
4646
},
4747
);
4848

49-
const renderItem = ({ item }: ListRenderItemInfo<Route>) => (
49+
const renderItem = ({ item }: ListRenderItemInfo<IRoute>) => (
5050
<TouchableOpacity
5151
style={{
5252
...styles.button,
@@ -70,7 +70,7 @@ const DashboardScreen: FC = () => {
7070
<FlatList
7171
data={data?.routes}
7272
renderItem={renderItem}
73-
keyExtractor={(route: Route) => route.routeId}
73+
keyExtractor={(route: IRoute) => route.routeId}
7474
/>
7575
</View>
7676
);

src/screens/map/MapScreen.tsx

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ import StopMarker from 'components/StopMarker';
1111
import { GET_TRIP } from 'screens/route/RouteScreen';
1212
import Stop from 'components/Stop';
1313
import { setActiveStop } from 'slices/stops';
14+
import { STOP_FIELDS } from 'apollo/fragments';
1415

1516
MapboxGL.setAccessToken(MAPBOX_ACCESS_TOKEN);
1617

1718
const DEFAULT_COORD = [-73.94594865587045, 40.7227534777328];
1819
const DEFAULT_ZOOM = 11;
1920
const STOP_ZOOM = 15;
20-
const ANIMATION_DURATION = 2000;
21+
const ANIMATION_DURATION = 1500;
2122

2223
interface ShapeVars {
2324
shapeId: string;
@@ -58,8 +59,8 @@ const MapScreen: FC = () => {
5859
},
5960
});
6061

61-
const goToStop = useCallback(
62-
stop => {
62+
const onStopPress = useCallback(
63+
(stop: IStop) => {
6364
setMarkerVisible(false);
6465
setTimeout(() => setMarkerVisible(true), ANIMATION_DURATION);
6566
dispatch(
@@ -81,11 +82,12 @@ const MapScreen: FC = () => {
8182

8283
const { nextTrip: trip }: { nextTrip: ITrip } = tripInCache || {};
8384

84-
const stopTime: IStopTime | any =
85-
trip?.stopTimes.find(
86-
(st: IStopTime) => st.stop.stopId === activeStop?.stopId,
87-
) || null;
88-
const { stop }: { stop: IStop } = stopTime || {};
85+
const stop = client.readFragment({
86+
id: `Stop:${activeStop?.stopId}`,
87+
fragment: gql`
88+
${STOP_FIELDS}
89+
`,
90+
});
8991

9092
useEffect(() => {
9193
if (stop) {
@@ -121,9 +123,10 @@ const MapScreen: FC = () => {
121123
trip?.stopTimes.map((st: IStopTime) => (
122124
<Stop
123125
key={`stop-time-${st.stop.stopId}`}
124-
stopId={st.stop.stopId}
125-
trip={trip}
126-
goToStop={goToStop}
126+
stop={st.stop}
127+
color={trip.route.routeColor}
128+
aboveLayerID={`line-layer-${trip.shapeId}`}
129+
onPress={onStopPress}
127130
/>
128131
))}
129132
</MapboxGL.MapView>

src/screens/route/RouteScreen.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { useAppDispatch } from 'store';
1414
import { setActiveStop } from 'slices/stops';
1515
import styles from './styles';
1616
import { setActiveTrip } from 'slices/trips';
17+
import { STOP_FIELDS } from 'apollo/fragments';
1718

1819
type Props = {
1920
route: IRoute;
@@ -25,6 +26,7 @@ interface TripVars {
2526
}
2627

2728
export const GET_TRIP = gql`
29+
${STOP_FIELDS}
2830
query GetTrip($feedIndex: Int!, $routeId: String!) {
2931
nextTrip(feedIndex: $feedIndex, routeId: $routeId) {
3032
feedIndex
@@ -47,11 +49,7 @@ export const GET_TRIP = gql`
4749
seconds
4850
}
4951
stop {
50-
stopId
51-
stopName
52-
geom {
53-
coordinates
54-
}
52+
...StopFields
5553
}
5654
}
5755
}

0 commit comments

Comments
 (0)