Skip to content
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

refactor: Simplify how train departureDate is passed on callbacks #19

Merged
merged 1 commit into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/StationTimeTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type StationTimeTableProps = {
stationCode: string;
timeTableType: TimeTableRowType;
trains: TrainByStationFragment[];
tableRowOnClick: (trainNumber: number, scheduledTime: Date) => void;
tableRowOnClick: (trainNumber: number, departureDate: string) => void;
};

function StationTimeTable({
Expand Down
22 changes: 5 additions & 17 deletions src/components/StationTimeTableRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,8 @@ import {
TimeTableRowType,
TrainByStationFragment,
} from '../graphql/generated/digitraffic';
import { formatEET } from '../utils/date';
import getTimeTableRowForStation from '../utils/getTimeTableRowForStation';
import {
getTrainDestinationStationName,
getTrainScheduledDepartureTime,
} from '../utils/train';
import { getTrainDestinationStationName } from '../utils/train';

import TimeTableRowTime from './TimeTableRowTime';
import VehicleTrackingIcon from './VehicleTrackingIcon';
Expand All @@ -29,7 +25,7 @@ type StationTimeTableRowProps = {
train: TrainByStationFragment;
stationCode: string;
timeTableType: TimeTableRowType;
tableRowOnClick: (trainNumber: number, scheduledTime: Date) => void;
tableRowOnClick: (trainNumber: number, departureDate: string) => void;
};

function StationTimeTableRow({
Expand All @@ -41,11 +37,11 @@ function StationTimeTableRow({
const handleStationClick = (e: React.MouseEvent) => e.stopPropagation();

const trainNumber = train.trainNumber;
const departureDate = train.departureDate;
const trainName = train.commuterLineid
? train.commuterLineid
: train.trainType.name + train.trainNumber;
const destinationStationName = getTrainDestinationStationName(train);
const departureTime = getTrainScheduledDepartureTime(train);
const stationRow = getTimeTableRowForStation(
stationCode,
train,
Expand All @@ -59,11 +55,7 @@ function StationTimeTableRow({
sx={{
cursor: 'pointer',
}}
onClick={() => {
if (departureTime) {
tableRowOnClick(trainNumber, departureTime);
}
}}
onClick={() => tableRowOnClick(trainNumber, departureDate)}
>
<TableCell scope="row">
<span
Expand Down Expand Up @@ -94,11 +86,7 @@ function StationTimeTableRow({
{trainName}
<VehicleTrackingIcon
trainNumber={trainNumber}
departureDate={
departureTime
? formatEET(departureTime, 'yyyy-MM-dd')
: undefined
}
departureDate={departureDate}
/>
</Box>
</span>
Expand Down
7 changes: 2 additions & 5 deletions src/pages/[station].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,9 @@ const Station: NextPageWithLayout = () => {
}, []);

const handleTimeTableRowClick = useCallback(
(trainNumber: number, scheduledTime: Date) => {
(trainNumber: number, departureDate: string) => {
router.push(
`/train/${trainNumber}/${formatEET(
scheduledTime,
'yyyy-MM-dd'
)}?station=${stationCode}`
`/train/${trainNumber}/${departureDate}?station=${stationCode}`
);
},
[router, stationCode]
Expand Down