|
| 1 | +import React from 'react'; |
| 2 | +import { useRouter } from 'found'; |
| 3 | +import { DateTime } from 'luxon'; |
| 4 | +import PropTypes from 'prop-types'; |
| 5 | +import { useIntl } from 'react-intl'; |
| 6 | +import { useConfigContext } from '../../configurations/ConfigContext'; |
| 7 | +import { PREFIX_TIMETABLE, TRAFFICNOW, routePagePath } from '../../util/path'; |
| 8 | +import Card from '../Card'; |
| 9 | +import Icon from '../Icon'; |
| 10 | +import CanceledDepartures from './components/CanceledDepartures'; |
| 11 | +import RouteBadgeGroup from './components/RouteBadgeGroup'; |
| 12 | +import DisruptionBadge from './DisruptionBadge'; |
| 13 | + |
| 14 | +const CanceledTripCard = ({ mode, totalCount, trips }) => { |
| 15 | + const { router } = useRouter(); |
| 16 | + const { colors } = useConfigContext(); |
| 17 | + const intl = useIntl(); |
| 18 | + |
| 19 | + const handleRouteBadgeClick = url => e => { |
| 20 | + e.preventDefault(); |
| 21 | + e.stopPropagation(); |
| 22 | + router.push(url); |
| 23 | + }; |
| 24 | + |
| 25 | + /* eslint-disable no-param-reassign */ |
| 26 | + const groupedTrips = trips.reduce((container, { start, trip }) => { |
| 27 | + if (!trip?.route?.gtfsId || !start?.schedule?.time?.departure) { |
| 28 | + return container; |
| 29 | + } |
| 30 | + |
| 31 | + const shortName = trip?.route?.shortName || 'unknown'; |
| 32 | + if (container[shortName]) { |
| 33 | + container[shortName].trips.push({ |
| 34 | + ...trip, |
| 35 | + departureTime: DateTime.fromISO( |
| 36 | + start?.schedule.time.departure, |
| 37 | + ).toFormat('HH:mm'), |
| 38 | + }); |
| 39 | + } else { |
| 40 | + container[shortName] = { |
| 41 | + routeGtfsId: trip.route.gtfsId, |
| 42 | + trips: [ |
| 43 | + { |
| 44 | + ...trip, |
| 45 | + departureTime: DateTime.fromISO( |
| 46 | + start?.schedule.time.departure, |
| 47 | + ).toFormat('HH:mm'), |
| 48 | + }, |
| 49 | + ], |
| 50 | + }; |
| 51 | + } |
| 52 | + return container; |
| 53 | + }, {}); |
| 54 | + |
| 55 | + const isSingleRoute = Object.keys(groupedTrips).length === 1; |
| 56 | + |
| 57 | + return ( |
| 58 | + <Card |
| 59 | + className="disruption-card clickable" |
| 60 | + onClick={handleRouteBadgeClick(`/${TRAFFICNOW}/peruutukset/${mode}`)} |
| 61 | + > |
| 62 | + <header> |
| 63 | + <DisruptionBadge showIcon variant="WARNING" label="NO_SERVICE" /> |
| 64 | + <button type="button"> |
| 65 | + <Icon |
| 66 | + img="icon_arrow-collapse--right" |
| 67 | + color={colors.primary} |
| 68 | + className="disruption-card__icon" |
| 69 | + /> |
| 70 | + </button> |
| 71 | + </header> |
| 72 | + <div className="badges"> |
| 73 | + <RouteBadgeGroup |
| 74 | + mode={mode} |
| 75 | + stopPropagation |
| 76 | + routes={Object.entries(groupedTrips).map( |
| 77 | + ([shortName, { routeGtfsId, trips: groupedRouteTrips }]) => ({ |
| 78 | + id: shortName, |
| 79 | + name: shortName, |
| 80 | + url: routePagePath(routeGtfsId, PREFIX_TIMETABLE), |
| 81 | + gtfsId: routeGtfsId, |
| 82 | + trips: groupedRouteTrips, |
| 83 | + }), |
| 84 | + )} |
| 85 | + renderRouteSuffix={({ trips: groupedRouteTrips }) => |
| 86 | + isSingleRoute ? ( |
| 87 | + <CanceledDepartures |
| 88 | + departures={groupedRouteTrips.map( |
| 89 | + ({ tripId, departureTime }) => ({ |
| 90 | + tripId, |
| 91 | + departureTime, |
| 92 | + }), |
| 93 | + )} |
| 94 | + /> |
| 95 | + ) : null |
| 96 | + } |
| 97 | + renderSuffix={ |
| 98 | + totalCount > trips.length ? ( |
| 99 | + <span style={{ backgroundColor: '#F2F5F7' }}> |
| 100 | + <Icon img="icon_three-dots" width={1.3} height={1.3} /> |
| 101 | + </span> |
| 102 | + ) : null |
| 103 | + } |
| 104 | + /> |
| 105 | + </div> |
| 106 | + <div className="disruption-card__body-row-validity-icon text-xs"> |
| 107 | + <Icon img="icon_clock" /> |
| 108 | + {intl.formatMessage({ id: 'valid', defaultMessage: 'Active' })} |
| 109 | + </div> |
| 110 | + </Card> |
| 111 | + ); |
| 112 | +}; |
| 113 | + |
| 114 | +CanceledTripCard.propTypes = { |
| 115 | + mode: PropTypes.string.isRequired, |
| 116 | + totalCount: PropTypes.number.isRequired, |
| 117 | + trips: PropTypes.arrayOf(PropTypes.shape({})).isRequired, |
| 118 | +}; |
| 119 | + |
| 120 | +export default CanceledTripCard; |
0 commit comments