-
Notifications
You must be signed in to change notification settings - Fork 2
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 #72 from tsdataclinic/api-route-filter
re-enable route filtering client side, add requisite API endpoint
- Loading branch information
Showing
4 changed files
with
76 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { useQuery } from "@tanstack/react-query"; | ||
import { SelectedRoute } from "../components/MainPage"; | ||
|
||
const BACKEND_URI = process.env.REACT_APP_PROD_BACKEND_URI ?? process.env.REACT_APP_DEV_BACKEND_URI; | ||
|
||
export const useRemoteRouteFilter = (selectedRoutes: Array<SelectedRoute>) : { data: string[] , status: string} => { | ||
const shouldEnableQuery = selectedRoutes.length > 0; | ||
const result = useQuery({ | ||
queryKey: [`routeFilter-${JSON.stringify(selectedRoutes)}`], | ||
queryFn: async () => { | ||
let stopsOnRouteURL = new URL(`${BACKEND_URI}/stops-on-route`); | ||
|
||
const params = new URLSearchParams(); | ||
selectedRoutes.forEach((route) => { | ||
params.append('cities', route.city); | ||
params.append('routes', route.routeServiced); | ||
}); | ||
|
||
stopsOnRouteURL.search = params.toString(); | ||
|
||
const response = await fetch(stopsOnRouteURL); | ||
const data = await response.json(); | ||
return data; | ||
}, | ||
staleTime: 1000 * 60 * 60, // one hour, | ||
enabled: shouldEnableQuery, | ||
}); | ||
return { data: result.data , status: result.status}; | ||
}; | ||
|