Skip to content

Commit

Permalink
refactor: move queries
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianflatner committed Nov 19, 2024
1 parent 01bf909 commit b4a4d55
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 144 deletions.
149 changes: 5 additions & 144 deletions tavla/pages/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,10 @@ import Head from 'next/head'
import { useEffect } from 'react'
import { fetchQuery } from 'graphql/utils'
import { addMinutesToDate, formatDateToISO } from 'utils/time'
import {
TGetQuayQuery,
TGetQuayQueryVariables,
TStopPlaceQuery,
TStopPlaceQueryVariables,
TypedDocumentString,
} from 'graphql/index'
import { TGetQuayQuery, TStopPlaceQuery } from 'graphql/index'
import { isUnsupportedBrowser } from 'utils/browserDetection'
import { GetServerSideProps } from 'next'
import { SSRQuayQuery, SSRStopPlaceQuery } from 'graphql/ssrQueries'

export const getServerSideProps: GetServerSideProps = async (context) => {
const { params, req } = context
Expand Down Expand Up @@ -105,6 +100,7 @@ function BoardPage({
export default BoardPage

// Fetch data for each tile on the board
// Used for server side rendering on unsupported browsers
const getTileData = async (board: TBoard) => {
const tileData = await Promise.all(
board.tiles.map(async (tile) => {
Expand All @@ -117,7 +113,7 @@ const getTileData = async (board: TBoard) => {
addMinutesToDate(new Date(), tile.offset ?? 0),
),
}
const data = await fetchQuery(StopPlaceQuery, variables)
const data = await fetchQuery(SSRStopPlaceQuery, variables)
return data
} else if (tile.type === 'quay') {
const variables = {
Expand All @@ -128,7 +124,7 @@ const getTileData = async (board: TBoard) => {
addMinutesToDate(new Date(), tile.offset ?? 0),
),
}
const data = await fetchQuery(QuayQuery, variables)
const data = await fetchQuery(SSRQuayQuery, variables)
return data
} else {
return null
Expand All @@ -137,138 +133,3 @@ const getTileData = async (board: TBoard) => {
)
return tileData
}

const StopPlaceQuery = `
query StopPlace($stopPlaceId: String!, $whitelistedTransportModes: [TransportMode], $whitelistedLines: [ID!], $numberOfDepartures: Int = 20, $startTime: DateTime) {
stopPlace(id: $stopPlaceId) {
name
transportMode
estimatedCalls(
numberOfDepartures: $numberOfDepartures
whiteListedModes: $whitelistedTransportModes
whiteListed: {lines: $whitelistedLines}
includeCancelledTrips: true
startTime: $startTime
) {
...departure
}
situations {
...situation
}
}
}
fragment departure on EstimatedCall {
quay {
publicCode
}
destinationDisplay {
frontText
via
}
aimedDepartureTime
expectedDepartureTime
expectedArrivalTime
serviceJourney {
id
transportMode
transportSubmode
line {
id
publicCode
presentation {
textColour
colour
}
}
}
cancellation
realtime
situations {
...situation
}
}
fragment situation on PtSituationElement {
id
description {
value
language
}
summary {
value
language
}
}` as unknown as TypedDocumentString<
TStopPlaceQuery,
TStopPlaceQueryVariables
>

const QuayQuery = `
query getQuay($quayId: String!, $whitelistedTransportModes: [TransportMode], $whitelistedLines: [ID!], $numberOfDepartures: Int = 20, $startTime: DateTime) {
quay(id: $quayId) {
name
description
publicCode
...lines
estimatedCalls(
numberOfDepartures: $numberOfDepartures
whiteListedModes: $whitelistedTransportModes
whiteListed: {lines: $whitelistedLines}
includeCancelledTrips: true
startTime: $startTime
) {
...departure
}
situations {
...situation
}
}
}
fragment departure on EstimatedCall {
quay {
publicCode
}
destinationDisplay {
frontText
via
}
aimedDepartureTime
expectedDepartureTime
expectedArrivalTime
serviceJourney {
id
transportMode
transportSubmode
line {
id
publicCode
presentation {
textColour
colour
}
}
}
cancellation
realtime
situations {
...situation
}
}
fragment lines on Quay {
lines {
id
publicCode
name
transportMode
}
}
fragment situation on PtSituationElement {
id
description {
value
language
}
summary {
value
language
}
}` as unknown as TypedDocumentString<TGetQuayQuery, TGetQuayQueryVariables>
144 changes: 144 additions & 0 deletions tavla/src/Shared/graphql/ssrQueries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// Queries used when server side rendering
// Used for boards on unsupported browsers
import {
TypedDocumentString,
TStopPlaceQuery,
TStopPlaceQueryVariables,
TGetQuayQuery,
TGetQuayQueryVariables,
} from '.'

export const SSRStopPlaceQuery = `
query StopPlace($stopPlaceId: String!, $whitelistedTransportModes: [TransportMode], $whitelistedLines: [ID!], $numberOfDepartures: Int = 20, $startTime: DateTime) {
stopPlace(id: $stopPlaceId) {
name
transportMode
estimatedCalls(
numberOfDepartures: $numberOfDepartures
whiteListedModes: $whitelistedTransportModes
whiteListed: {lines: $whitelistedLines}
includeCancelledTrips: true
startTime: $startTime
) {
...departure
}
situations {
...situation
}
}
}
fragment departure on EstimatedCall {
quay {
publicCode
}
destinationDisplay {
frontText
via
}
aimedDepartureTime
expectedDepartureTime
expectedArrivalTime
serviceJourney {
id
transportMode
transportSubmode
line {
id
publicCode
presentation {
textColour
colour
}
}
}
cancellation
realtime
situations {
...situation
}
}
fragment situation on PtSituationElement {
id
description {
value
language
}
summary {
value
language
}
}` as unknown as TypedDocumentString<
TStopPlaceQuery,
TStopPlaceQueryVariables
>

export const SSRQuayQuery = `
query getQuay($quayId: String!, $whitelistedTransportModes: [TransportMode], $whitelistedLines: [ID!], $numberOfDepartures: Int = 20, $startTime: DateTime) {
quay(id: $quayId) {
name
description
publicCode
...lines
estimatedCalls(
numberOfDepartures: $numberOfDepartures
whiteListedModes: $whitelistedTransportModes
whiteListed: {lines: $whitelistedLines}
includeCancelledTrips: true
startTime: $startTime
) {
...departure
}
situations {
...situation
}
}
}
fragment departure on EstimatedCall {
quay {
publicCode
}
destinationDisplay {
frontText
via
}
aimedDepartureTime
expectedDepartureTime
expectedArrivalTime
serviceJourney {
id
transportMode
transportSubmode
line {
id
publicCode
presentation {
textColour
colour
}
}
}
cancellation
realtime
situations {
...situation
}
}
fragment lines on Quay {
lines {
id
publicCode
name
transportMode
}
}
fragment situation on PtSituationElement {
id
description {
value
language
}
summary {
value
language
}
}` as unknown as TypedDocumentString<TGetQuayQuery, TGetQuayQueryVariables>

0 comments on commit b4a4d55

Please sign in to comment.