|
| 1 | +import React from 'react' |
| 2 | + |
| 3 | +import { Link } from 'react-router-dom' |
| 4 | + |
| 5 | +import Card from '@mui/material/Card' |
| 6 | +import CardActions from '@mui/material/CardActions' |
| 7 | +import CardContent from '@mui/material/CardContent' |
| 8 | +import Button from '@mui/material/Button' |
| 9 | +import IconButton from '@mui/material/IconButton' |
| 10 | +import Typography from '@mui/material/Typography' |
| 11 | + |
| 12 | +import ArrowRightIcon from '@mui/icons-material/ArrowRightTwoTone' |
| 13 | +import InfoIcon from '@mui/icons-material/InfoOutlined' |
| 14 | +import LocationOnIcon from '@mui/icons-material/LocationOnRounded' |
| 15 | + |
| 16 | +import { lighten } from '@mui/material' |
| 17 | + |
| 18 | +import { useSearchStateValue } from './context/searchState' |
| 19 | +import { useViewStateValue } from './context/viewState' |
| 20 | +import { useApplicationStateValue } from './context/applicationState' |
| 21 | + |
| 22 | +function PostcodeInfoMobileStop () { |
| 23 | + const [ |
| 24 | + { searchType, searchPostcode, nearestMobileLibraryStops }, |
| 25 | + dispatchSearch |
| 26 | + ] = useSearchStateValue() |
| 27 | + const [{}, dispatchView] = useViewStateValue() //eslint-disable-line |
| 28 | + const [{ serviceLookup }] = useApplicationStateValue() |
| 29 | + |
| 30 | + const nearestMobileLibraryStop = nearestMobileLibraryStops?.[0] |
| 31 | + |
| 32 | + const viewStop = () => { |
| 33 | + dispatchSearch({ |
| 34 | + type: 'SetCurrentStop', |
| 35 | + currentStopId: nearestMobileLibraryStop.id, |
| 36 | + currentPoint: [ |
| 37 | + nearestMobileLibraryStop.longitude, |
| 38 | + nearestMobileLibraryStop.latitude |
| 39 | + ] |
| 40 | + }) |
| 41 | + dispatchView({ type: 'SetStopDialog', stopDialogOpen: true }) |
| 42 | + } |
| 43 | + |
| 44 | + const viewMap = () => { |
| 45 | + dispatchView({ |
| 46 | + type: 'FlyTo', |
| 47 | + mapFlyToPosition: [ |
| 48 | + nearestMobileLibraryStop.longitude, |
| 49 | + nearestMobileLibraryStop.latitude |
| 50 | + ], |
| 51 | + mapZoom: 18 |
| 52 | + }) |
| 53 | + } |
| 54 | + |
| 55 | + const serviceSystemName = |
| 56 | + serviceLookup[nearestMobileLibraryStop?.organisationCode]?.systemName |
| 57 | + |
| 58 | + return ( |
| 59 | + <> |
| 60 | + {searchType === 'postcode' && searchPostcode && nearestMobileLibraryStop && ( |
| 61 | + <Card |
| 62 | + elevation={0} |
| 63 | + sx={theme => ({ |
| 64 | + border: 2, |
| 65 | + borderColor: lighten(theme.palette.mobileLibraries.main, 0.6) |
| 66 | + })} |
| 67 | + > |
| 68 | + <CardContent> |
| 69 | + <Typography variant='h5' component='span' color='text.secondary'> |
| 70 | + {`${Math.round( |
| 71 | + nearestMobileLibraryStop?.distance / 1609 |
| 72 | + )} miles from nearest mobile`} |
| 73 | + </Typography> |
| 74 | + <Typography |
| 75 | + color='mobileLibraries.main' |
| 76 | + variant='h5' |
| 77 | + component='p' |
| 78 | + sx={{ fontWeight: 600 }} |
| 79 | + > |
| 80 | + {`${nearestMobileLibraryStop?.name}`} |
| 81 | + </Typography> |
| 82 | + </CardContent> |
| 83 | + <CardActions |
| 84 | + sx={{ |
| 85 | + backgroundColor: theme => |
| 86 | + lighten(theme.palette.mobileLibraries.main, 0.9), |
| 87 | + justifyContent: 'space-between' |
| 88 | + }} |
| 89 | + > |
| 90 | + <Button |
| 91 | + color='secondary' |
| 92 | + variant='text' |
| 93 | + endIcon={<ArrowRightIcon />} |
| 94 | + to={`/service/${serviceSystemName}/${nearestMobileLibraryStop?.systemName} `} |
| 95 | + component={Link} |
| 96 | + disableElevation |
| 97 | + > |
| 98 | + Stop page |
| 99 | + </Button> |
| 100 | + <Button |
| 101 | + size='small' |
| 102 | + endIcon={<LocationOnIcon />} |
| 103 | + onClick={viewMap} |
| 104 | + component={Link} |
| 105 | + to='/map' |
| 106 | + color='secondary' |
| 107 | + > |
| 108 | + On map |
| 109 | + </Button> |
| 110 | + <IconButton onClick={viewStop} color='secondary' size='small'> |
| 111 | + <InfoIcon /> |
| 112 | + </IconButton> |
| 113 | + </CardActions> |
| 114 | + </Card> |
| 115 | + )} |
| 116 | + </> |
| 117 | + ) |
| 118 | +} |
| 119 | + |
| 120 | +export default PostcodeInfoMobileStop |
0 commit comments