-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3703 from TrainLCD/release/v7.7.2
v7.7.2🎉
- Loading branch information
Showing
7 changed files
with
85 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,55 @@ | ||
import firestore from '@react-native-firebase/firestore' | ||
import { useCallback, useEffect, useState } from 'react' | ||
import useSWR from 'swr' | ||
import useSWRMutation from 'swr/dist/mutation' | ||
import { GetStationByIdListRequest } from '../../gen/proto/stationapi_pb' | ||
import { grpcClient } from '../lib/grpc' | ||
import { SavedRoute } from '../models/SavedRoute' | ||
import useCachedInitAnonymousUser from './useCachedAnonymousUser' | ||
|
||
export const useSavedRoutes = () => { | ||
useCachedInitAnonymousUser() | ||
const [routes, setRoutes] = useState<SavedRoute[]>([]) | ||
const [loading, setLoading] = useState(false) | ||
|
||
useEffect(() => { | ||
const fetchRoutesAsync = async () => { | ||
setLoading(true) | ||
const routesSnapshot = await firestore() | ||
.collection('uploadedCommunityRoutes') | ||
.orderBy('createdAt', 'desc') | ||
.get() | ||
const routes = routesSnapshot.docs.map((doc) => ({ | ||
id: doc.id, | ||
...doc.data(), | ||
const { | ||
data: routes, | ||
isLoading: isRoutesLoading, | ||
error: fetchRoutesError, | ||
} = useSWR<SavedRoute[]>('/firestore/uploadedCommunityRoutes', async () => { | ||
const routesSnapshot = await firestore() | ||
.collection('uploadedCommunityRoutes') | ||
.orderBy('createdAt', 'desc') | ||
.get() | ||
|
||
return routesSnapshot.docs.map((doc) => ({ | ||
id: doc.id, | ||
...doc.data(), | ||
})) as SavedRoute[] | ||
}) | ||
|
||
const { | ||
isMutating: isStationsLoading, | ||
error: fetchStationsError, | ||
trigger: fetchStationsByRoute, | ||
} = useSWRMutation( | ||
'/app.trainlcd.grpc/GetStationByIdListRequest', | ||
async (_, { arg: route }: { arg: SavedRoute }) => { | ||
const req = new GetStationByIdListRequest() | ||
req.ids = route.stations.map((sta) => sta.id) | ||
const res = await grpcClient.getStationByIdList(req, {}) | ||
const stations = res?.stations ?? [] | ||
|
||
return stations.map((sta) => ({ | ||
...sta, | ||
stopCondition: route.stations.find((rs) => rs.id === sta.id) | ||
?.stopCondition, | ||
trainType: route.trainType, | ||
})) | ||
setRoutes(routes as SavedRoute[]) | ||
setLoading(false) | ||
} | ||
fetchRoutesAsync() | ||
}, []) | ||
|
||
const fetchStationsByRoute = useCallback(async (route: SavedRoute) => { | ||
const req = new GetStationByIdListRequest() | ||
req.ids = route.stations.map((sta) => sta.id) | ||
const res = await grpcClient.getStationByIdList(req, {}) | ||
const stations = res?.stations ?? [] | ||
return stations.map((sta, idx) => ({ | ||
...sta, | ||
stopCondition: route.stations[idx].stopCondition, | ||
trainType: route.trainType, | ||
})) | ||
}, []) | ||
) | ||
|
||
return { routes, loading, fetchStationsByRoute } | ||
return { | ||
routes, | ||
loading: isRoutesLoading || isStationsLoading, | ||
error: fetchRoutesError || fetchStationsError, | ||
fetchStationsByRoute, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters