Skip to content

Commit

Permalink
fix(board): fetch departures with correct start time (#1734)
Browse files Browse the repository at this point in the history
* fix(board): fetch departures with correct start time

* chore(): remove unused hooks

* fix(board): fetch deps with offset in quays
  • Loading branch information
emilielr authored Nov 20, 2024
1 parent 504bd4d commit 0a17d79
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 71 deletions.
6 changes: 1 addition & 5 deletions tavla/src/Board/scenarios/QuayTile/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { TableHeader } from '../Table/components/TableHeader'
import { isNotNullOrUndefined } from 'utils/typeguards'
import { TileLoader } from 'Board/components/TileLoader'
import { useQuery } from 'hooks/useQuery'
import { addMinutesToDate, formatDateToISO } from 'utils/time'

export function QuayTile({
placeId,
Expand All @@ -23,11 +22,8 @@ export function QuayTile({
quayId: placeId,
whitelistedLines,
whitelistedTransportModes,
startTime: formatDateToISO(
addMinutesToDate(new Date(), offset ?? 0),
),
},
{ poll: true },
{ poll: true, offset: offset ?? 0 },
)

if (!data) {
Expand Down
6 changes: 1 addition & 5 deletions tavla/src/Board/scenarios/StopPlaceTile/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { Tile } from 'components/Tile'
import { TableHeader } from '../Table/components/TableHeader'
import { TileLoader } from 'Board/components/TileLoader'
import { useQuery } from 'hooks/useQuery'
import { addMinutesToDate, formatDateToISO } from 'utils/time'

export function StopPlaceTile({
placeId,
Expand All @@ -22,11 +21,8 @@ export function StopPlaceTile({
stopPlaceId: placeId,
whitelistedTransportModes,
whitelistedLines,
startTime: formatDateToISO(
addMinutesToDate(new Date(), offset ?? 0),
),
},
{ poll: true },
{ poll: true, offset: offset },
)

if (!data) {
Expand Down
17 changes: 14 additions & 3 deletions tavla/src/Shared/graphql/utils.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import { CLIENT_NAME, GRAPHQL_ENDPOINTS, TEndpointNames } from 'assets/env'
import { TypedDocumentString } from './index'
import { formatDateToISO, addMinutesToDate } from 'utils/time'

export async function fetcher<Data, Variables>([
query,
variables,
endpointName,
]: [TypedDocumentString<Data, Variables>, Variables, TEndpointNames]) {
offset,
]: [TypedDocumentString<Data, Variables>, Variables, TEndpointNames, number]) {
const startTime = formatDateToISO(addMinutesToDate(new Date(), offset))
const mergedVariables = { ...variables, startTime }

return fetch(GRAPHQL_ENDPOINTS[endpointName], {
headers: {
'Content-Type': 'application/json',
'ET-Client-Name': CLIENT_NAME,
},
body: JSON.stringify({ query, variables }),
body: JSON.stringify({ query, variables: mergedVariables }),
method: 'POST',
})
.then((res) => {
Expand All @@ -24,6 +29,7 @@ export async function fetcher<Data, Variables>([

type TFetchQueryOptions = {
endpoint: TEndpointNames
offset?: number
}

export async function fetchQuery<Data, Variables>(
Expand All @@ -35,5 +41,10 @@ export async function fetchQuery<Data, Variables>(
endpoint: 'journey-planner',
...options,
}
return fetcher([query, variables, mergedOptions.endpoint])
return fetcher([
query,
variables,
mergedOptions.endpoint,
mergedOptions.offset ?? 0,
])
}
24 changes: 0 additions & 24 deletions tavla/src/Shared/hooks/useHash.ts

This file was deleted.

3 changes: 2 additions & 1 deletion tavla/src/Shared/hooks/useQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import useSWR from 'swr'
type TUseQueryOptions = {
poll: boolean
endpoint: TEndpointNames
offset?: number
}

export function useQuery<Data, Variables>(
Expand All @@ -20,7 +21,7 @@ export function useQuery<Data, Variables>(
}

const { data, error, isLoading } = useSWR<Data>(
[query, variables, mergedOptions.endpoint],
[query, variables, mergedOptions.endpoint, mergedOptions.offset ?? 0],
fetcher,
{
revalidateOnFocus: true,
Expand Down
13 changes: 0 additions & 13 deletions tavla/src/Shared/hooks/useToggle.ts

This file was deleted.

20 changes: 0 additions & 20 deletions tavla/src/Shared/hooks/useUpdateLastActive.ts

This file was deleted.

0 comments on commit 0a17d79

Please sign in to comment.