-
Notifications
You must be signed in to change notification settings - Fork 1k
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 #6133 from realCity/debug-ui-place-id-planning
debug client: support stop ids in from / to inputs
- Loading branch information
Showing
7 changed files
with
180 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Location } from '../../gql/graphql.ts'; | ||
import { useQuayCoordinateQuery } from '../../hooks/useQuayCoordinateQuery.ts'; | ||
|
||
interface Coordinates { | ||
latitude: number; | ||
longitude: number; | ||
} | ||
|
||
export function useCoordinateResolver(location: Location): Coordinates | undefined { | ||
const quay = useQuayCoordinateQuery(location); | ||
|
||
if (quay) { | ||
const { latitude, longitude } = quay; | ||
|
||
if (latitude && longitude) { | ||
return { | ||
latitude, | ||
longitude, | ||
}; | ||
} | ||
} | ||
|
||
if (location.coordinates) { | ||
return { | ||
...location.coordinates, | ||
}; | ||
} | ||
|
||
return undefined; | ||
} |
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,36 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { request } from 'graphql-request'; | ||
import { Location, QueryType } from '../gql/graphql.ts'; | ||
import { getApiUrl } from '../util/getApiUrl.ts'; | ||
import { graphql } from '../gql'; | ||
|
||
const query = graphql(` | ||
query quayCoordinate($id: String!) { | ||
quay(id: $id) { | ||
latitude | ||
longitude | ||
} | ||
} | ||
`); | ||
|
||
export const useQuayCoordinateQuery = (location: Location) => { | ||
const [data, setData] = useState<QueryType | null>(null); | ||
|
||
useEffect(() => { | ||
const fetchData = async () => { | ||
if (location.place) { | ||
const variables = { id: location.place }; | ||
try { | ||
setData((await request(getApiUrl(), query, variables)) as QueryType); | ||
} catch (e) { | ||
console.error('Error at useQuayCoordinateQuery', e); | ||
} | ||
} else { | ||
setData(null); | ||
} | ||
}; | ||
fetchData(); | ||
}, [location]); | ||
|
||
return data?.quay; | ||
}; |
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,37 @@ | ||
import { COORDINATE_PRECISION } from '../components/SearchBar/constants.ts'; | ||
import { Location } from '../gql/graphql.ts'; | ||
|
||
const DOUBLE_PATTERN = '-{0,1}\\d+(\\.\\d+){0,1}'; | ||
|
||
const LAT_LON_PATTERN = '(' + DOUBLE_PATTERN + ')(\\s+)(' + DOUBLE_PATTERN + ')'; | ||
|
||
export function parseLocation(value: string): Location | null { | ||
const latLonMatch = value.match(LAT_LON_PATTERN); | ||
|
||
if (latLonMatch) { | ||
return { | ||
coordinates: { | ||
latitude: +latLonMatch[1], | ||
longitude: +latLonMatch[4], | ||
}, | ||
}; | ||
} | ||
|
||
return { | ||
place: value, | ||
}; | ||
} | ||
|
||
export function toString(location: Location): string | null { | ||
if (location.coordinates) { | ||
return `${location.coordinates?.latitude.toPrecision( | ||
COORDINATE_PRECISION, | ||
)} ${location.coordinates?.longitude.toPrecision(COORDINATE_PRECISION)}`; | ||
} | ||
|
||
if (location.place) { | ||
return location.place; | ||
} | ||
|
||
return null; | ||
} |