Skip to content

Commit 3ae16a0

Browse files
committed
fix: move timepoints fetch to ScheduleController
1 parent 469fbd1 commit 3ae16a0

File tree

17 files changed

+662
-253
lines changed

17 files changed

+662
-253
lines changed

assets/src/api.ts

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import "whatwg-fetch"
22
import appData from "./appData"
3-
import { Block, Run } from "./minischedule"
3+
import { ScheduleBlock, ScheduleRun } from "./minischedule"
44
import { reload } from "./models/browser"
5-
import { blockFromData, runFromData } from "./models/minischeduleData"
5+
import {
6+
scheduleBlockFromData,
7+
scheduleRunFromData,
8+
} from "./models/minischeduleData"
69
import { SwingData, swingsFromData } from "./models/swingsData"
710
import { NotificationId, NotificationState, RunId } from "./realtime"
811
import {
@@ -411,26 +414,28 @@ export const fetchTimepointsForRoute = (
411414
export const fetchScheduleRun = (
412415
tripId: TripId,
413416
runId: RunId | null
414-
): Promise<Run | null> => {
417+
): Promise<ScheduleRun | null> => {
415418
if (runId) {
416419
return apiCall({
417420
url: `/api/schedule/run?trip_id=${tripId}&run_id=${runId}`,
418-
parser: nullableParser(runFromData),
421+
parser: nullableParser(scheduleRunFromData),
419422
defaultResult: null,
420423
})
421424
} else {
422425
return apiCall({
423426
url: `/api/schedule/run?trip_id=${tripId}`,
424-
parser: nullableParser(runFromData),
427+
parser: nullableParser(scheduleRunFromData),
425428
defaultResult: null,
426429
})
427430
}
428431
}
429432

430-
export const fetchScheduleBlock = (tripId: TripId): Promise<Block | null> =>
433+
export const fetchScheduleBlock = (
434+
tripId: TripId
435+
): Promise<ScheduleBlock | null> =>
431436
apiCall({
432437
url: `/api/schedule/block?trip_id=${tripId}`,
433-
parser: nullableParser(blockFromData),
438+
parser: nullableParser(scheduleBlockFromData),
434439
defaultResult: null,
435440
})
436441

assets/src/components/propertiesPanel/minischedule.tsx

+36-22
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ import {
2626
} from "../../hooks/useMinischedule"
2727
import {
2828
AsDirected,
29-
Block,
3029
Break,
3130
Piece,
3231
Run,
32+
ScheduleRun,
33+
ScheduleBlock,
3334
Time,
3435
Trip,
3536
} from "../../minischedule"
@@ -59,68 +60,77 @@ import Loading from "../loading"
5960
import { currentRouteTab } from "../../models/routeTab"
6061
import { isVehicleInScheduledService } from "../../models/vehicle"
6162
import inTestGroup, { TestGroups } from "../../userInTestGroup"
62-
import { useTimepointsByIdForRoute } from "../../hooks/useTimepoints"
6363

6464
export interface Props {
6565
vehicleOrGhost: VehicleInScheduledService | Ghost
6666
}
6767

6868
export const MinischeduleRun = ({ vehicleOrGhost }: Props): ReactElement => {
69-
const run: Run | null | undefined = useMinischeduleRun(
69+
const run: ScheduleRun | null | undefined = useMinischeduleRun(
7070
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
7171
vehicleOrGhost.tripId!,
7272
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
7373
vehicleOrGhost.runId!
7474
)
75-
const timepoints = useTimepointsByIdForRoute(vehicleOrGhost.routeId)
7675

7776
return (
78-
<Minischedule
79-
runOrBlock={run}
80-
vehicleOrGhost={vehicleOrGhost}
81-
timepoints={timepoints}
82-
view="run"
83-
/>
77+
<Minischedule runOrBlock={run} vehicleOrGhost={vehicleOrGhost} view="run" />
8478
)
8579
}
8680

8781
export const MinischeduleBlock = ({ vehicleOrGhost }: Props): ReactElement => {
88-
const block: Block | null | undefined = useMinischeduleBlock(
82+
const block: ScheduleBlock | null | undefined = useMinischeduleBlock(
8983
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
9084
vehicleOrGhost.tripId!
9185
)
92-
const timepoints = useTimepointsByIdForRoute(vehicleOrGhost.routeId)
9386

9487
return (
9588
<Minischedule
9689
runOrBlock={block}
9790
vehicleOrGhost={vehicleOrGhost}
98-
timepoints={timepoints}
9991
view="block"
10092
/>
10193
)
10294
}
10395

96+
const activitiesFromRunOrBlock = (runOrBlock: ScheduleBlock | ScheduleRun) => {
97+
if ("run" in runOrBlock) {
98+
return runOrBlock.run.activities
99+
}
100+
return runOrBlock.block.pieces
101+
}
102+
103+
const idFromRunOrBlock = (runOrBlock: ScheduleBlock | ScheduleRun) => {
104+
if ("run" in runOrBlock) {
105+
return runOrBlock.run.id
106+
}
107+
return runOrBlock.block.id
108+
}
109+
110+
const runFromRunOrBlock = (runOrBlock: ScheduleBlock | ScheduleRun) => {
111+
if ("run" in runOrBlock) {
112+
return runOrBlock.run
113+
}
114+
return undefined
115+
}
116+
104117
export const Minischedule = ({
105118
runOrBlock,
106119
vehicleOrGhost,
107-
timepoints,
108120
view,
109121
}: {
110-
runOrBlock: Run | Block | null | undefined
122+
runOrBlock: ScheduleRun | ScheduleBlock | null | undefined
111123
vehicleOrGhost: VehicleInScheduledService | Ghost
112-
timepoints: TimepointNameById | null
113124
view: "run" | "block"
114125
}) => {
115126
const [showPast, setShowPast] = useState<boolean>(false)
116127

117-
if (runOrBlock === undefined || timepoints === null) {
128+
if (runOrBlock === undefined) {
118129
return <Loading />
119130
} else if (runOrBlock === null) {
120131
return view === "run" ? <>No run found</> : <>No block found</>
121132
} else {
122-
const activities: (Piece | Break)[] =
123-
(runOrBlock as Run).activities || (runOrBlock as Block).pieces
133+
const activities: (Piece | Break)[] = activitiesFromRunOrBlock(runOrBlock)
124134
const activeIndex = getActiveIndex(
125135
activities,
126136
vehicleOrGhost.tripId,
@@ -135,9 +145,13 @@ export const Minischedule = ({
135145
>
136146
<Header
137147
label={view === "run" ? "Run" : "Block"}
138-
value={runOrBlock.id}
148+
value={idFromRunOrBlock(runOrBlock)}
139149
/>
140-
{view === "run" ? <DutyDetails run={runOrBlock as Run} /> : null}
150+
{view === "run" ? (
151+
<DutyDetails
152+
run={runFromRunOrBlock(runOrBlock) as ScheduleRun["run"]}
153+
/>
154+
) : null}
141155
<DeparturePointHeader />
142156
<PastToggle showPast={showPast} setShowPast={setShowPast} />
143157
<div>
@@ -150,7 +164,7 @@ export const Minischedule = ({
150164
pieceIndex={index}
151165
activeIndex={activeIndex}
152166
key={activity.startTime}
153-
timepoints={timepoints}
167+
timepoints={runOrBlock.timepoints}
154168
/>
155169
) : (
156170
<BreakRow

assets/src/hooks/useMinischedule.ts

+20-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useEffect, useState } from "react"
22
import { fetchScheduleBlock, fetchScheduleRun } from "../api"
3-
import { Block, Run } from "../minischedule"
3+
import { ScheduleBlock, ScheduleRun, Run } from "../minischedule"
44
import { TripId } from "../schedule"
55
import { RunId } from "../realtime"
66
import { equalByElements } from "../helpers/array"
@@ -12,12 +12,14 @@ import { equalByElements } from "../helpers/array"
1212
export const useMinischeduleRun = (
1313
tripId: TripId,
1414
runId: RunId
15-
): Run | null | undefined => {
16-
const [run, setRun] = useState<Run | null | undefined>(undefined)
15+
): ScheduleRun | null | undefined => {
16+
const [scheduleRun, setScheduleRun] = useState<
17+
ScheduleRun | null | undefined
18+
>(undefined)
1719
useEffect(() => {
18-
fetchScheduleRun(tripId, runId).then(setRun)
20+
fetchScheduleRun(tripId, runId).then(setScheduleRun)
1921
}, [tripId, runId])
20-
return run
22+
return scheduleRun
2123
}
2224

2325
/**
@@ -35,9 +37,13 @@ export const useMinischeduleRuns = (
3537
}
3638

3739
useEffect(() => {
38-
Promise.all(
39-
currentTripIds.map((tripId) => fetchScheduleRun(tripId, null))
40-
).then(setRuns)
40+
Promise.all(currentTripIds.map((tripId) => fetchScheduleRun(tripId, null)))
41+
.then((scheduleRuns) =>
42+
scheduleRuns.map((scheduleRun) =>
43+
scheduleRun !== null ? scheduleRun.run : null
44+
)
45+
)
46+
.then(setRuns)
4147
}, [currentTripIds])
4248

4349
return runs
@@ -49,10 +55,12 @@ export const useMinischeduleRuns = (
4955
*/
5056
export const useMinischeduleBlock = (
5157
tripId: TripId
52-
): Block | null | undefined => {
53-
const [block, setBlock] = useState<Block | null | undefined>(undefined)
58+
): ScheduleBlock | null | undefined => {
59+
const [scheduledBlock, setScheduledBlock] = useState<
60+
ScheduleBlock | null | undefined
61+
>(undefined)
5462
useEffect(() => {
55-
fetchScheduleBlock(tripId).then(setBlock)
63+
fetchScheduleBlock(tripId).then(setScheduledBlock)
5664
}, [tripId])
57-
return block
65+
return scheduledBlock
5866
}

assets/src/minischedule.d.ts

+11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
RouteId,
66
StopId,
77
TimepointId,
8+
TimepointNameById,
89
TripId,
910
ViaVariant,
1011
} from "./schedule"
@@ -21,6 +22,16 @@ export interface Block {
2122
pieces: Piece[]
2223
}
2324

25+
export interface ScheduleRun {
26+
run: Run
27+
timepoints: TimepointNameById
28+
}
29+
30+
export interface ScheduleBlock {
31+
block: Block
32+
timepoints: TimepointNameById
33+
}
34+
2435
export interface Break {
2536
breakType: string
2637
startTime: Time

assets/src/models/minischeduleData.ts

+41-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import {
44
Break,
55
Piece,
66
Run,
7+
ScheduleBlock,
8+
ScheduleRun,
79
StopTime,
810
Time,
911
Trip,
@@ -14,13 +16,25 @@ import {
1416
DirectionId,
1517
RouteId,
1618
StopId,
19+
Timepoint,
1720
TimepointId,
21+
TimepointNameById,
1822
TripId,
1923
ViaVariant,
2024
} from "../schedule"
2125

2226
type ActivityData = BreakData | PieceData
2327

28+
interface ScheduleRunData {
29+
run: RunData
30+
timepoints: Timepoint[]
31+
}
32+
33+
interface ScheduleBlockData {
34+
block: BlockData
35+
timepoints: Timepoint[]
36+
}
37+
2438
interface RunData {
2539
id: RunId
2640
activities: ActivityData[]
@@ -82,18 +96,39 @@ const isBreakData = (
8296
): activityData is BreakData =>
8397
Object.prototype.hasOwnProperty.call(activityData, "break_type")
8498

85-
export const runFromData = (runData: RunData): Run => ({
86-
id: runData.id,
87-
activities: runData.activities.map((activityData) =>
99+
export const scheduleRunFromData = (
100+
scheduleRunData: ScheduleRunData
101+
): ScheduleRun => ({
102+
run: runFromData(scheduleRunData),
103+
timepoints: timepointsFromData(scheduleRunData),
104+
})
105+
106+
const runFromData = ({ run }: ScheduleRunData): Run => ({
107+
id: run.id,
108+
activities: run.activities.map((activityData) =>
88109
isBreakData(activityData)
89110
? breakFromData(activityData)
90111
: pieceFromData(activityData)
91112
),
92113
})
93114

94-
export const blockFromData = (blockData: BlockData): Block => ({
95-
id: blockData.id,
96-
pieces: blockData.pieces.map(pieceFromData),
115+
const timepointsFromData = ({
116+
timepoints,
117+
}: ScheduleRunData | ScheduleBlockData): TimepointNameById =>
118+
new Map(
119+
timepoints.map((timepoint: Timepoint) => [timepoint.id, timepoint.name])
120+
)
121+
122+
export const scheduleBlockFromData = (
123+
scheduleBlockData: ScheduleBlockData
124+
): ScheduleBlock => ({
125+
block: blockFromData(scheduleBlockData),
126+
timepoints: timepointsFromData(scheduleBlockData),
127+
})
128+
129+
export const blockFromData = ({ block }: ScheduleBlockData): Block => ({
130+
id: block.id,
131+
pieces: block.pieces.map(pieceFromData),
97132
})
98133

99134
const breakFromData = (breakData: BreakData): Break => ({

0 commit comments

Comments
 (0)