From 37e2ad78bab6dfb15702773291c42ab3ad756972 Mon Sep 17 00:00:00 2001 From: Vili Ketonen <25618592+viliket@users.noreply.github.com> Date: Sat, 16 Sep 2023 10:48:18 +0300 Subject: [PATCH] Improve PassengerInformationMessagesDialog opening and closing --- .../PassengerInformationMessagesDialog.tsx | 12 ++++++++---- src/components/TrainInfoContainer.tsx | 15 +++++++++++++-- src/pages/[station].tsx | 10 +++------- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/components/PassengerInformationMessagesDialog.tsx b/src/components/PassengerInformationMessagesDialog.tsx index 82eff817..0a384082 100644 --- a/src/components/PassengerInformationMessagesDialog.tsx +++ b/src/components/PassengerInformationMessagesDialog.tsx @@ -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'; @@ -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(); @@ -34,7 +38,7 @@ const PassengerInformationMessagesDialog = ( }; return ( - + {t('alerts')} {passengerInformationMessages?.map((m) => ( diff --git a/src/components/TrainInfoContainer.tsx b/src/components/TrainInfoContainer.tsx index b54b9cc0..d80eb8d1 100644 --- a/src/components/TrainInfoContainer.tsx +++ b/src/components/TrainInfoContainer.tsx @@ -26,6 +26,7 @@ function TrainInfoContainer({ train }: TrainInfoContainerProps) { const [wagonDialogOpen, setWagonDialogOpen] = useState(false); const [selectedWagon, setSelectedWagon] = useState(null); const [selectedStation, setSelectedStation] = useState(null); + const [stationAlertDialogOpen, setStationAlertDialogOpen] = useState(false); const departureDate = train ? getTrainScheduledDepartureTime(train) : null; const { error, @@ -67,6 +68,15 @@ function TrainInfoContainer({ train }: TrainInfoContainerProps) { setWagonDialogOpen(true); }; + const handleStationAlertDialogClose = () => { + setStationAlertDialogOpen(false); + }; + + const handleStationAlertClick = (stationCode: string) => { + setSelectedStation(stationCode); + setStationAlertDialogOpen(true); + }; + return ( <> setSelectedStation(stationCode)} + onStationAlertClick={handleStationAlertClick} stationMessages={stationMessages} /> {train && ( @@ -117,12 +127,13 @@ function TrainInfoContainer({ train }: TrainInfoContainerProps) { /> )} setSelectedStation(null)} + onClose={handleStationAlertDialogClose} /> ); diff --git a/src/pages/[station].tsx b/src/pages/[station].tsx index a7fa8ed8..8ee4a929 100644 --- a/src/pages/[station].tsx +++ b/src/pages/[station].tsx @@ -43,8 +43,7 @@ const Station: NextPageWithLayout = () => { null ); const [selectedTrainNo, setSelectedTrainNo] = useState(null); - const [stationAlertDialogOpen, setStationAlertDialogOpen] = - useState(); + const [stationAlertDialogOpen, setStationAlertDialogOpen] = useState(false); const [executeRouteSearch, { data: routeData }] = useRoutesForRailLazyQuery(); const station = stationName ? trainStations.find( @@ -214,11 +213,8 @@ const Station: NextPageWithLayout = () => { {error.message} )} setStationAlertDialogOpen(false)} />