-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
debug client: support stop ids in from / to inputs #6133
base: dev-2.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 { longitude, latitude } = quay; | ||
|
||
if (longitude && latitude) { | ||
return { | ||
longitude, | ||
latitude, | ||
}; | ||
} | ||
} | ||
|
||
if (location.coordinates) { | ||
return { | ||
...location.coordinates, | ||
}; | ||
} | ||
|
||
return undefined; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { request } from 'graphql-request'; // eslint-disable-line import/no-unresolved | ||
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) { | ||
longitude | ||
latitude | ||
} | ||
} | ||
`); | ||
|
||
export const useQuayCoordinateQuery = (location: Location) => { | ||
const [data, setData] = useState<QueryType | null>(null); | ||
|
||
useEffect(() => { | ||
const fetchData = async () => { | ||
if (location.place) { | ||
const variables = { id: location.place }; | ||
setData((await request(getApiUrl(), query, variables)) as QueryType); | ||
} else { | ||
setData(null); | ||
} | ||
}; | ||
fetchData(); | ||
}, [location]); | ||
|
||
return data?.quay; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
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*,\\s*|\\s+)(' + DOUBLE_PATTERN + ')'; | ||
|
||
const ID_SEPARATOR = ':'; | ||
|
||
export function parseLocation(value: string): Location | null { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand what this function does. In particular, there seems to be something going on with "name" which seems out of scope. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These function is the typescript version of the LocationStringParser.java class. I removed the "name" parsing part. |
||
const latLonMatch = value.match(LAT_LON_PATTERN); | ||
|
||
if (latLonMatch) { | ||
return { | ||
coordinates: { | ||
latitude: +latLonMatch[1], | ||
longitude: +latLonMatch[4], | ||
}, | ||
}; | ||
} | ||
|
||
if (validFeedScopeIdString(value)) { | ||
return { | ||
place: value, | ||
}; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
function validFeedScopeIdString(value: string): boolean { | ||
return value.indexOf(ID_SEPARATOR) > -1; | ||
} | ||
|
||
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; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My knowledge of JS is limited - but it looks like the arguments order is swapped according to the interface definition above. As a practice to avoid errors I like to always do
lat, long
neverlong, lat
(doc, params, args, execution order and so on).