Skip to content

Commit

Permalink
refactor: Simplify how train departureDate is passed on callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
viliket committed Sep 14, 2023
1 parent af9e09c commit 7065050
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 23 deletions.
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

0 comments on commit 7065050

Please sign in to comment.