-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from mjaakko/search_trains_by_route
Search trains by route
- Loading branch information
Showing
18 changed files
with
475 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import React from 'react'; | ||
import { useHistory, useRouteMatch } from 'react-router-dom'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { Tab, Container, Header } from 'semantic-ui-react'; | ||
|
||
import SearchTrainByRoute from './pages/SearchTrainByRoute'; | ||
import SearchTrainByNumber from './pages/SearchTrainByNumber'; | ||
|
||
import DocumentTitle from './DocumentTitle'; | ||
|
||
const tabs = t => [ | ||
{ | ||
menuItem: t('searchTrains.byNumber'), | ||
render: () => <SearchTrainByNumber />, | ||
}, | ||
{ | ||
menuItem: t('searchTrains.byRoute'), | ||
render: () => <SearchTrainByRoute />, | ||
}, | ||
]; | ||
|
||
const SearchTrain = () => { | ||
const { t } = useTranslation(); | ||
|
||
const history = useHistory(); | ||
|
||
const byNumber = useRouteMatch('/searchtrainbynumber'); | ||
const byRoute = useRouteMatch('/searchtrainbyroute'); | ||
|
||
const onTabChange = (_, { activeIndex: index }) => { | ||
if (index === 0) { | ||
history.push('/searchtrainbynumber'); | ||
} else if (index === 1) { | ||
history.push('/searchtrainbyroute'); | ||
} | ||
}; | ||
|
||
const activeIndex = byNumber ? 0 : byRoute ? 1 : -1; | ||
|
||
return ( | ||
<> | ||
<DocumentTitle title={t('searchTrains.search')} /> | ||
<Container as="main"> | ||
<Header as="h1">{t('searchTrains.search')}</Header> | ||
<Tab | ||
panes={tabs(t)} | ||
onTabChange={onTabChange} | ||
activeIndex={activeIndex} | ||
/> | ||
</Container> | ||
</> | ||
); | ||
}; | ||
|
||
export default SearchTrain; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import SearchTrainByNumber from './SearchTrainByNumber'; | ||
|
||
export default SearchTrainByNumber; |
128 changes: 128 additions & 0 deletions
128
src/components/pages/SearchTrainByRoute/SearchTrainByRoute.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import React, { useState, useContext } from 'react'; | ||
import { useLocation, useHistory } from 'react-router-dom'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { Container, Form, Divider } from 'semantic-ui-react'; | ||
import SemanticDatepicker from 'react-semantic-ui-datepickers'; | ||
import 'react-semantic-ui-datepickers/dist/react-semantic-ui-datepickers.css'; | ||
import moment from 'moment'; | ||
|
||
import { MetadataContext } from '../../../App'; | ||
|
||
import TrainResults from './TrainResults'; | ||
import { formatStationName } from '../../../utils/format'; | ||
|
||
const validateDate = date => { | ||
return !isNaN(new Date(date).getTime()); | ||
}; | ||
|
||
const toISODate = date => | ||
`${date.getFullYear()}-${String(date.getMonth() + 1).padStart( | ||
2, | ||
'0' | ||
)}-${String(date.getDate()).padStart(2, '0')}`; | ||
|
||
const SearchTrainByRoute = () => { | ||
const { stations } = useContext(MetadataContext); | ||
|
||
const { t, i18n } = useTranslation(); | ||
|
||
const history = useHistory(); | ||
|
||
const { search } = useLocation(); | ||
const queryParams = new URLSearchParams(search); | ||
|
||
const [fromStation, setFromStation] = useState( | ||
queryParams.has('fromStation') ? queryParams.get('fromStation') : '' | ||
); | ||
const [toStation, setToStation] = useState( | ||
queryParams.has('toStation') ? queryParams.get('toStation') : '' | ||
); | ||
const [date, setDate] = useState( | ||
queryParams.has('departureDate') && | ||
validateDate(queryParams.get('departureDate')) | ||
? queryParams.get('departureDate') | ||
: toISODate(new Date()) | ||
); | ||
|
||
const onChangeFromStation = (_, { value }) => setFromStation(value); | ||
const onChangeToStation = (_, { value }) => setToStation(value); | ||
const onChangeDate = (_, { value }) => { | ||
if (value) { | ||
setDate(toISODate(value)); | ||
} | ||
}; | ||
|
||
if (!stations) { | ||
return null; | ||
} | ||
|
||
const onSubmit = () => { | ||
history.replace( | ||
`?fromStation=${fromStation}&toStation=${toStation}&date=${date}` | ||
); | ||
}; | ||
|
||
const options = Array.from(stations.values()) | ||
.filter(station => station.passengerTraffic) | ||
.map(station => ({ | ||
text: formatStationName(station.stationName), | ||
value: station.stationShortCode, | ||
})); | ||
|
||
return ( | ||
<Container style={{ marginTop: '1rem' }}> | ||
<Form onSubmit={onSubmit}> | ||
<Form.Group widths={4}> | ||
<Form.Select | ||
className="stationdropdown" | ||
options={options} | ||
placeholder={t('searchTrainsByRoute.from')} | ||
label={t('searchTrainsByRoute.from')} | ||
noResultsMessage={t('common.noResults')} | ||
value={fromStation} | ||
search | ||
required | ||
onChange={onChangeFromStation} | ||
/> | ||
<Form.Select | ||
className="stationdropdown" | ||
options={options} | ||
placeholder={t('searchTrainsByRoute.to')} | ||
label={t('searchTrainsByRoute.to')} | ||
noResultsMessage={t('common.noResults')} | ||
value={toStation} | ||
search | ||
required | ||
onChange={onChangeToStation} | ||
/> | ||
</Form.Group> | ||
<SemanticDatepicker | ||
size="small" | ||
locale={i18n.language} | ||
label={t('searchTrains.departureDate')} | ||
value={new Date(date)} | ||
date={new Date(date)} | ||
required | ||
firstDayOfWeek={moment.localeData().firstDayOfWeek()} | ||
onChange={onChangeDate} | ||
style={{ width: '15rem' }} | ||
/> | ||
<Form.Button>{t('searchTrains.search')}</Form.Button> | ||
</Form> | ||
{queryParams.has('fromStation') && | ||
queryParams.has('toStation') && | ||
queryParams.has('date') && ( | ||
<> | ||
<Divider section /> | ||
<TrainResults | ||
fromStation={queryParams.get('fromStation')} | ||
toStation={queryParams.get('toStation')} | ||
date={queryParams.get('date')} | ||
/> | ||
</> | ||
)} | ||
</Container> | ||
); | ||
}; | ||
|
||
export default SearchTrainByRoute; |
Oops, something went wrong.