Skip to content

Commit

Permalink
Improve PassengerInformationMessagesDialog opening and closing
Browse files Browse the repository at this point in the history
  • Loading branch information
viliket committed Sep 16, 2023
1 parent bc6660a commit 37e2ad7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
12 changes: 8 additions & 4 deletions src/components/PassengerInformationMessagesDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useEffect, useState } from 'react';

import { Alert, DialogActions, DialogContent } from '@mui/material';
import Dialog from '@mui/material/Dialog';
Expand All @@ -9,13 +9,17 @@ import { PassengerInformationMessage } from '../utils/passengerInformationMessag

type PassengerInformationMessagesDialogProps = {
onClose: () => void;
passengerInformationMessages: PassengerInformationMessage[] | null;
open: boolean;
passengerInformationMessages:
| PassengerInformationMessage[]
| null
| undefined;
};

const PassengerInformationMessagesDialog = (
props: PassengerInformationMessagesDialogProps
) => {
const { onClose, passengerInformationMessages } = props;
const { onClose, open, passengerInformationMessages } = props;
const { i18n } = useTranslation();
const { t } = useTranslation();

Expand All @@ -34,7 +38,7 @@ const PassengerInformationMessagesDialog = (
};

return (
<Dialog onClose={handleClose} open={!!passengerInformationMessages}>
<Dialog onClose={handleClose} open={open}>
<DialogTitle>{t('alerts')}</DialogTitle>
<DialogContent>
{passengerInformationMessages?.map((m) => (
Expand Down
15 changes: 13 additions & 2 deletions src/components/TrainInfoContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function TrainInfoContainer({ train }: TrainInfoContainerProps) {
const [wagonDialogOpen, setWagonDialogOpen] = useState(false);
const [selectedWagon, setSelectedWagon] = useState<Wagon | null>(null);
const [selectedStation, setSelectedStation] = useState<string | null>(null);
const [stationAlertDialogOpen, setStationAlertDialogOpen] = useState(false);
const departureDate = train ? getTrainScheduledDepartureTime(train) : null;
const {
error,
Expand Down Expand Up @@ -67,6 +68,15 @@ function TrainInfoContainer({ train }: TrainInfoContainerProps) {
setWagonDialogOpen(true);
};

const handleStationAlertDialogClose = () => {
setStationAlertDialogOpen(false);
};

const handleStationAlertClick = (stationCode: string) => {
setSelectedStation(stationCode);
setStationAlertDialogOpen(true);
};

return (
<>
<Box
Expand Down Expand Up @@ -105,7 +115,7 @@ function TrainInfoContainer({ train }: TrainInfoContainerProps) {
train={train}
realTimeTrain={realTimeTrain}
onWagonClick={handleWagonClick}
onStationAlertClick={(stationCode) => setSelectedStation(stationCode)}
onStationAlertClick={handleStationAlertClick}
stationMessages={stationMessages}
/>
{train && (
Expand All @@ -117,12 +127,13 @@ function TrainInfoContainer({ train }: TrainInfoContainerProps) {
/>
)}
<PassengerInformationMessagesDialog
open={stationAlertDialogOpen}
passengerInformationMessages={
selectedStation && stationMessages?.[selectedStation]
? stationMessages[selectedStation]
: null
}
onClose={() => setSelectedStation(null)}
onClose={handleStationAlertDialogClose}
/>
</>
);
Expand Down
10 changes: 3 additions & 7 deletions src/pages/[station].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ const Station: NextPageWithLayout = () => {
null
);
const [selectedTrainNo, setSelectedTrainNo] = useState<number | null>(null);
const [stationAlertDialogOpen, setStationAlertDialogOpen] =
useState<boolean>();
const [stationAlertDialogOpen, setStationAlertDialogOpen] = useState(false);
const [executeRouteSearch, { data: routeData }] = useRoutesForRailLazyQuery();
const station = stationName
? trainStations.find(
Expand Down Expand Up @@ -214,11 +213,8 @@ const Station: NextPageWithLayout = () => {
<Box sx={{ width: '100%', textAlign: 'center' }}>{error.message}</Box>
)}
<PassengerInformationMessagesDialog
passengerInformationMessages={
passengerInformationMessages && stationAlertDialogOpen
? passengerInformationMessages
: null
}
open={stationAlertDialogOpen}
passengerInformationMessages={passengerInformationMessages}
onClose={() => setStationAlertDialogOpen(false)}
/>
</div>
Expand Down

0 comments on commit 37e2ad7

Please sign in to comment.