@opentripplanner/trip-details 2.0.2 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -15,10 +15,13 @@ import {
15
15
  CaloriesDetailsProps,
16
16
  DepartureDetailsProps,
17
17
  FareDetailsProps,
18
+ FareTableLayout,
19
+ FareTableText,
18
20
  TripDetailsProps
19
21
  } from "./types";
20
22
 
21
- import customMessages from "../__mocks__/custom-messages.yml";
23
+ import customEnglishMessages from "../__mocks__/custom-english-messages.yml";
24
+ import customFrenchMessages from "../__mocks__/custom-french-messages.yml";
22
25
 
23
26
  // import mock itinaries. These are all trip plan outputs from OTP.
24
27
  const bikeOnlyItinerary = require("@opentripplanner/itinerary-body/src/__mocks__/itineraries/bike-only.json");
@@ -36,6 +39,9 @@ const walkTransitWalkTransitWalkItinerary = require("@opentripplanner/itinerary-
36
39
  const fareComponentsItinerary = require("@opentripplanner/itinerary-body/src/__mocks__/itineraries/fare-components.json");
37
40
  const otp2ScooterItinerary = require("@opentripplanner/itinerary-body/src/__mocks__/itineraries/otp2-scooter.json");
38
41
 
42
+ const flattenedEnglishMessages = flatten(customEnglishMessages);
43
+ const flattenedFrenchMessages = flatten(customFrenchMessages);
44
+
39
45
  // Change currency code on one of the itineraries for illustration.
40
46
  // (other currency fields in the itinerary object are not used for display).
41
47
  const itinCurrency =
@@ -48,6 +54,52 @@ const StyledTripDetails = styled(TripDetails)`
48
54
  }
49
55
  `;
50
56
 
57
+ const fareByLegLayout: FareTableLayout[] = [
58
+ {
59
+ header: "regular" as FareTableText,
60
+ cols: [
61
+ {
62
+ header: "cash" as FareTableText,
63
+ key: "regular"
64
+ },
65
+ {
66
+ header: "electronic" as FareTableText,
67
+ key: "electronicRegular"
68
+ },
69
+ {
70
+ header: "special" as FareTableText,
71
+ key: "electronicSpecial"
72
+ }
73
+ ]
74
+ },
75
+ {
76
+ header: "youth" as FareTableText,
77
+ cols: [
78
+ {
79
+ header: "cash" as FareTableText,
80
+ key: "youth"
81
+ },
82
+ {
83
+ header: "electronic" as FareTableText,
84
+ key: "electronicYouth"
85
+ }
86
+ ]
87
+ },
88
+ {
89
+ header: "senior" as FareTableText,
90
+ cols: [
91
+ {
92
+ header: "cash" as FareTableText,
93
+ key: "cash"
94
+ },
95
+ {
96
+ header: "electronic" as FareTableText,
97
+ key: "electronic"
98
+ }
99
+ ]
100
+ }
101
+ ];
102
+
51
103
  const longDateFormat = "MMMM D, YYYY";
52
104
 
53
105
  const englishFareKeyMap = {
@@ -76,6 +128,15 @@ const frenchFareKeyMap = {
76
128
  electronicSenior: "Tarif Orca séniors"
77
129
  };
78
130
 
131
+ const CustomFareDetails = ({
132
+ transitFares
133
+ }: FareDetailsProps): ReactElement => (
134
+ <>
135
+ Custom details about fares (transitFares: {JSON.stringify(transitFares)}){" "}
136
+ (cents), can be constructed dynamically using any markup.
137
+ </>
138
+ );
139
+
79
140
  // Custom slots for expandable detail sections.
80
141
  const CustomDepartureDetails = ({
81
142
  departureDate
@@ -86,18 +147,6 @@ const CustomDepartureDetails = ({
86
147
  </>
87
148
  );
88
149
 
89
- const CustomFareDetails = ({
90
- maxTNCFare,
91
- minTNCFare,
92
- transitFares
93
- }: FareDetailsProps): ReactElement => (
94
- <>
95
- Custom details about fares (transitFares: {JSON.stringify(transitFares)}{" "}
96
- (cents), minTNCFare: {minTNCFare} and maxTNCFare: {maxTNCFare} can be
97
- constructed dynamically using any markup.
98
- </>
99
- );
100
-
101
150
  const CustomCaloriesDetails = ({
102
151
  bikeSeconds,
103
152
  calories,
@@ -121,9 +170,9 @@ function createTripDetailsTemplate(
121
170
  const TripDetailsTemplate = (
122
171
  {
123
172
  CaloriesDetails,
124
- defaultFare,
125
173
  DepartureDetails,
126
174
  FareDetails,
175
+ fareDetailsLayout,
127
176
  itinerary
128
177
  }: TripDetailsProps,
129
178
  { globals, parameters }: StoryContext
@@ -138,9 +187,9 @@ function createTripDetailsTemplate(
138
187
  return (
139
188
  <Component
140
189
  CaloriesDetails={CaloriesDetails}
141
- defaultFare={defaultFare}
142
190
  DepartureDetails={DepartureDetails}
143
191
  FareDetails={FareDetails}
192
+ fareDetailsLayout={fareDetailsLayout}
144
193
  fareKeyNameMap={fareKeyNameMap}
145
194
  itinerary={itinerary}
146
195
  />
@@ -154,7 +203,7 @@ function createTripDetailsTemplate(
154
203
  */
155
204
  function makeStory(
156
205
  args: TripDetailsProps,
157
- parameters: Parameters,
206
+ parameters?: Parameters,
158
207
  Component?: typeof TripDetails
159
208
  ): ComponentStory<typeof TripDetails> {
160
209
  const BoundTripDetails = createTripDetailsTemplate(Component).bind({});
@@ -206,10 +255,19 @@ export const BikeTransitBikeItinerary = makeStory({
206
255
  export const WalkInterlinedTransitItinerary = makeStory(
207
256
  {
208
257
  defaultFareKey: "electronicRegular",
258
+ fareDetailsLayout: fareByLegLayout,
209
259
  itinerary: walkInterlinedTransitItinerary
210
260
  },
211
261
  {
212
- useCustomFareKeyMap: true
262
+ useCustomFareKeyMap: true,
263
+ // For illustration purposes,
264
+ // override a subset of localized strings with custom messages.
265
+ reactIntl: {
266
+ messages: {
267
+ "en-US": flattenedEnglishMessages,
268
+ fr: flattenedFrenchMessages
269
+ }
270
+ }
213
271
  }
214
272
  );
215
273
 
@@ -245,6 +303,7 @@ export const OTP2EScooterRentalTransitItinerary = makeStory({
245
303
 
246
304
  export const TncTransitItinerary = makeStory(
247
305
  {
306
+ FareDetails: CustomFareDetails,
248
307
  itinerary: tncTransitTncItinerary
249
308
  },
250
309
  {
@@ -252,13 +311,11 @@ export const TncTransitItinerary = makeStory(
252
311
  }
253
312
  );
254
313
 
255
- const flattenedMessages = flatten(customMessages);
256
- export const TncTransitItineraryWithCustomMessagesAndDetails = makeStory(
314
+ export const TncTransitItineraryWithCustomMessages = makeStory(
257
315
  {
258
316
  CaloriesDetails: CustomCaloriesDetails,
259
317
  defaultFareKey: "electronicRegular",
260
318
  DepartureDetails: CustomDepartureDetails,
261
- FareDetails: CustomFareDetails,
262
319
  itinerary: tncTransitTncItinerary
263
320
  },
264
321
  {
@@ -266,8 +323,8 @@ export const TncTransitItineraryWithCustomMessagesAndDetails = makeStory(
266
323
  // override a subset of localized strings with custom messages.
267
324
  reactIntl: {
268
325
  messages: {
269
- "en-US": flattenedMessages,
270
- fr: flattenedMessages
326
+ "en-US": flattenedEnglishMessages,
327
+ fr: flattenedFrenchMessages
271
328
  }
272
329
  },
273
330
  useCustomFareKeyMap: true
@@ -0,0 +1,222 @@
1
+ import { Leg, Money } from "@opentripplanner/types";
2
+ import flatten from "flat";
3
+ import React from "react";
4
+ import styled from "styled-components";
5
+ import { FormattedMessage } from "react-intl";
6
+ import { Transfer } from "@styled-icons/boxicons-regular/Transfer";
7
+
8
+ import { boldText, renderFare } from "./utils";
9
+
10
+ import { FareDetailsProps, FareTableLayout, FareTableText } from "./types";
11
+
12
+ // Load the default messages.
13
+ import defaultEnglishMessages from "../i18n/en-US.yml";
14
+
15
+ // HACK: We should flatten the messages loaded above because
16
+ // the YAML loaders behave differently between webpack and our version of jest:
17
+ // - the yaml loader for webpack returns a nested object,
18
+ // - the yaml loader for jest returns messages with flattened ids.
19
+ const defaultMessages: Record<string, string> = flatten(defaultEnglishMessages);
20
+
21
+ type LegAndFare = Leg & {
22
+ fares: Record<string, { price: Money; isTransfer?: boolean }>;
23
+ };
24
+
25
+ interface FareTypeTableProps extends FareTableLayout {
26
+ fareTotals: Record<string, Money>;
27
+ legs: LegAndFare[];
28
+ }
29
+
30
+ const TableHeader = styled.thead`
31
+ th {
32
+ font-weight: normal;
33
+ min-width: 5ch;
34
+ padding: 0.75em 1.5em;
35
+ text-align: center;
36
+ }
37
+ th:nth-of-type(2n + 1) {
38
+ background: #cccccc22;
39
+ }
40
+ th.main {
41
+ background: #333333;
42
+ color: #ffffffcc;
43
+ }
44
+ `;
45
+
46
+ const Table = styled.table`
47
+ border-collapse: collapse;
48
+ display: block;
49
+ margin-bottom: 16px;
50
+ padding: 0;
51
+
52
+ td {
53
+ text-align: right;
54
+ }
55
+ td:nth-of-type(2n + 1) {
56
+ background: #cccccc22;
57
+ }
58
+ td.no-zebra {
59
+ background: none;
60
+ }
61
+ th:first-of-type {
62
+ height: 40px;
63
+ }
64
+ `;
65
+
66
+ const TransferIcon = styled(Transfer)`
67
+ padding-left: 4px;
68
+ `;
69
+
70
+ const getFormattedTextForConfigKey = (textKey: FareTableText) => {
71
+ switch (textKey) {
72
+ case FareTableText.cash:
73
+ return (
74
+ <FormattedMessage
75
+ defaultMessage={
76
+ defaultMessages["otpUi.TripDetails.fareDetailsHeaders.cash"]
77
+ }
78
+ id="otpUi.TripDetails.fareDetailsHeaders.cash"
79
+ />
80
+ );
81
+ case FareTableText.electronic:
82
+ return (
83
+ <FormattedMessage
84
+ defaultMessage={
85
+ defaultMessages["otpUi.TripDetails.fareDetailsHeaders.electronic"]
86
+ }
87
+ id="otpUi.TripDetails.fareDetailsHeaders.electronic"
88
+ />
89
+ );
90
+ case FareTableText.youth:
91
+ return (
92
+ <FormattedMessage
93
+ defaultMessage={
94
+ defaultMessages["otpUi.TripDetails.fareDetailsHeaders.youth"]
95
+ }
96
+ id="otpUi.TripDetails.fareDetailsHeaders.youth"
97
+ />
98
+ );
99
+ case FareTableText.senior:
100
+ return (
101
+ <FormattedMessage
102
+ defaultMessage={
103
+ defaultMessages["otpUi.TripDetails.fareDetailsHeaders.senior"]
104
+ }
105
+ id="otpUi.TripDetails.fareDetailsHeaders.senior"
106
+ />
107
+ );
108
+ case FareTableText.special:
109
+ return (
110
+ <FormattedMessage
111
+ defaultMessage={
112
+ defaultMessages["otpUi.TripDetails.fareDetailsHeaders.special"]
113
+ }
114
+ id="otpUi.TripDetails.fareDetailsHeaders.special"
115
+ />
116
+ );
117
+ case FareTableText.regular:
118
+ default:
119
+ return (
120
+ <FormattedMessage
121
+ defaultMessage={
122
+ defaultMessages["otpUi.TripDetails.fareDetailsHeaders.regular"]
123
+ }
124
+ id="otpUi.TripDetails.fareDetailsHeaders.regular"
125
+ />
126
+ );
127
+ }
128
+ };
129
+
130
+ const FareTypeTable = ({
131
+ cols,
132
+ fareTotals,
133
+ header,
134
+ legs
135
+ }: FareTypeTableProps): JSX.Element => {
136
+ const colsToRender = cols.filter(col => fareTotals[col.key]);
137
+ if (colsToRender.length) {
138
+ return (
139
+ <Table>
140
+ <TableHeader>
141
+ <th className="main">
142
+ {boldText(getFormattedTextForConfigKey(header))}
143
+ </th>
144
+ {colsToRender.map(col => {
145
+ const fare = fareTotals[col.key];
146
+ return (
147
+ <th key={col.key}>
148
+ {boldText(getFormattedTextForConfigKey(col.header))}
149
+ <br />
150
+ {renderFare(
151
+ fare?.currency?.currencyCode,
152
+ (fare?.cents || 0) / 100
153
+ )}
154
+ </th>
155
+ );
156
+ })}
157
+ </TableHeader>
158
+ {legs.map((leg, index) => (
159
+ <tr key={index}>
160
+ <td className="no-zebra">{leg.routeShortName}</td>
161
+ {colsToRender.map(col => {
162
+ const fare = leg.fares[col.key];
163
+ return (
164
+ <td key={col.key}>
165
+ {renderFare(
166
+ fare?.price.currency?.currencyCode,
167
+ (fare?.price.cents || 0) / 100
168
+ )}
169
+ {fare?.isTransfer && <TransferIcon size={16} />}
170
+ </td>
171
+ );
172
+ })}
173
+ </tr>
174
+ ))}
175
+ </Table>
176
+ );
177
+ }
178
+ return null;
179
+ };
180
+
181
+ const FareLegDetails = ({
182
+ layout,
183
+ transitFareDetails,
184
+ transitFares,
185
+ legs
186
+ }: FareDetailsProps): JSX.Element => {
187
+ const fareKeys = Object.keys(transitFareDetails);
188
+
189
+ const legsWithFares = legs
190
+ .map((leg, index) => {
191
+ const fares = fareKeys.reduce((prev, key) => {
192
+ const fareForKey = transitFareDetails[key]?.find(
193
+ detail => detail.legIndex === index
194
+ );
195
+ if (fareForKey) {
196
+ prev[key] = fareForKey;
197
+ }
198
+ return prev;
199
+ }, {});
200
+ return {
201
+ ...leg,
202
+ fares
203
+ };
204
+ })
205
+ .filter(leg => leg.transitLeg);
206
+
207
+ return (
208
+ <div>
209
+ {layout.map(config => (
210
+ <FareTypeTable
211
+ cols={config.cols}
212
+ fareTotals={transitFares}
213
+ header={config.header}
214
+ key={config.header}
215
+ legs={legsWithFares}
216
+ />
217
+ ))}
218
+ </div>
219
+ );
220
+ };
221
+
222
+ export default FareLegDetails;
package/src/index.tsx CHANGED
@@ -2,7 +2,7 @@ import flatten from "flat";
2
2
  import coreUtils from "@opentripplanner/core-utils";
3
3
  import { FlexBookingInfo } from "@opentripplanner/types";
4
4
  import React, { ReactElement } from "react";
5
- import { FormattedMessage, FormattedNumber } from "react-intl";
5
+ import { FormattedMessage } from "react-intl";
6
6
  import { CalendarAlt } from "@styled-icons/fa-solid/CalendarAlt";
7
7
  import { HandPaper } from "@styled-icons/fa-solid/HandPaper";
8
8
  import { Heartbeat } from "@styled-icons/fa-solid/Heartbeat";
@@ -12,6 +12,8 @@ import { Route } from "@styled-icons/fa-solid/Route";
12
12
 
13
13
  import * as S from "./styled";
14
14
  import TripDetail from "./trip-detail";
15
+ import FareLegTable from "./fare-table";
16
+ import { boldText, renderFare } from "./utils";
15
17
 
16
18
  import {
17
19
  CaloriesDetailsProps,
@@ -28,35 +30,6 @@ import defaultEnglishMessages from "../i18n/en-US.yml";
28
30
  // - the yaml loader for jest returns messages with flattened ids.
29
31
  const defaultMessages: Record<string, string> = flatten(defaultEnglishMessages);
30
32
 
31
- /**
32
- * Format text bold (used with FormattedMessage).
33
- */
34
- // TODO: Find a better place for this utility.
35
- function boldText(contents: ReactElement): ReactElement {
36
- return <strong>{contents}</strong>;
37
- }
38
-
39
- /**
40
- * Render formatted fare.
41
- * @param currencyCode The ISO currency code to use (USD, GBP, EUR).
42
- * @param fare The fare value, in currency units, to be shown.
43
- * @returns The formatted fare value according to the selected locale.
44
- */
45
- function renderFare(currencyCode: string, fare: number): ReactElement {
46
- return (
47
- <FormattedNumber
48
- currency={currencyCode}
49
- // For dollars in locales such as 'fr',
50
- // this will limit the display to just the dollar sign
51
- // (otherwise it will render e.g. '2,50 $US' instead of '2,50 $').
52
- currencyDisplay="narrowSymbol"
53
- value={fare}
54
- // eslint-disable-next-line react/style-prop-object
55
- style="currency"
56
- />
57
- );
58
- }
59
-
60
33
  /**
61
34
  * Helper function to specify the link to dietary table.
62
35
  */
@@ -145,9 +118,10 @@ const TransitFare = ({
145
118
  export function TripDetails({
146
119
  CaloriesDetails = DefaultCaloriesDetails,
147
120
  className = "",
148
- DepartureDetails = null,
149
121
  defaultFareKey = "regular",
122
+ DepartureDetails = null,
150
123
  FareDetails = null,
124
+ fareDetailsLayout,
151
125
  fareKeyNameMap = {},
152
126
  itinerary
153
127
  }: TripDetailsProps): ReactElement {
@@ -155,6 +129,7 @@ export function TripDetails({
155
129
  const fareResult = coreUtils.itinerary.calculateTncFares(itinerary);
156
130
  const { maxTNCFare, minTNCFare, tncCurrencyCode } = fareResult;
157
131
  const transitFares = itinerary?.fare?.fare;
132
+ const fareDetails = itinerary.fare?.details;
158
133
 
159
134
  let companies = "";
160
135
  itinerary.legs.forEach(leg => {
@@ -195,20 +170,31 @@ export function TripDetails({
195
170
  transitFares={transitFares}
196
171
  />
197
172
  </summary>
198
- {fareKeys.map(fareKey => {
199
- // Don't show the default fare twice!
200
- if (fareKey === defaultFare) {
201
- return null;
202
- }
203
- return (
204
- <TransitFare
205
- fareKey={fareKey}
206
- key={fareKey}
207
- fareKeyNameMap={fareKeyNameMap}
208
- transitFares={transitFares}
209
- />
210
- );
211
- })}
173
+ {fareDetailsLayout ? (
174
+ // Show full ƒare details by leg
175
+ <FareLegTable
176
+ transitFares={transitFares}
177
+ transitFareDetails={fareDetails}
178
+ legs={itinerary.legs}
179
+ layout={fareDetailsLayout}
180
+ />
181
+ ) : (
182
+ // Just show the fares for each payment type
183
+ fareKeys.map(fareKey => {
184
+ // Don't show the default fare twice!
185
+ if (fareKey === defaultFare) {
186
+ return null;
187
+ }
188
+ return (
189
+ <TransitFare
190
+ fareKey={fareKey}
191
+ key={fareKey}
192
+ fareKeyNameMap={fareKeyNameMap}
193
+ transitFares={transitFares}
194
+ />
195
+ );
196
+ })
197
+ )}
212
198
  </TransitFareWrapper>
213
199
  {minTNCFare !== 0 && (
214
200
  <S.TNCFare>
@@ -290,13 +276,7 @@ export function TripDetails({
290
276
  <TripDetail
291
277
  // Any custom description for the transit fare needs to be handled by the slot.
292
278
  description={
293
- FareDetails && (
294
- <FareDetails
295
- maxTNCFare={maxTNCFare}
296
- minTNCFare={minTNCFare}
297
- transitFares={transitFares}
298
- />
299
- )
279
+ FareDetails && <FareDetails transitFares={transitFares} />
300
280
  }
301
281
  icon={<MoneyBillAlt size={17} />}
302
282
  summary={fare}
package/src/types.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  // Prettier does not recognize the import type syntax.
2
2
  // eslint-disable-next-line prettier/prettier
3
- import type { Itinerary, Money } from "@opentripplanner/types";
3
+ import type { FareDetails, Money, Itinerary, Leg, Fare, } from "@opentripplanner/types";
4
4
  import type { ReactElement } from "react";
5
5
 
6
6
  export interface CaloriesDetailsProps {
@@ -13,14 +13,31 @@ export interface DepartureDetailsProps {
13
13
  departureDate: Date;
14
14
  }
15
15
 
16
+ export enum FareTableText {
17
+ regular = "regular",
18
+ youth = "youth",
19
+ senior = "senior",
20
+ special = "special",
21
+ cash = "cash",
22
+ electronic = "electronic"
23
+ }
24
+
25
+ export interface FareTableLayout {
26
+ cols: {
27
+ header: FareTableText;
28
+ key: string;
29
+ }[];
30
+ header: FareTableText;
31
+ }
16
32
  export interface TransitFareData {
17
33
  [key: string]: Money
18
34
  }
19
35
 
20
36
  export interface FareDetailsProps {
21
- maxTNCFare: number;
22
- minTNCFare: number;
23
- transitFares: TransitFareData;
37
+ layout?: FareTableLayout[];
38
+ legs?: Leg[];
39
+ transitFareDetails?: FareDetails;
40
+ transitFares?: TransitFareData;
24
41
  }
25
42
 
26
43
  export interface TransitFareProps {
@@ -29,7 +46,7 @@ export interface TransitFareProps {
29
46
  fareKeyNameMap: {
30
47
  [key: string]: string;
31
48
  };
32
- transitFares: TransitFareData;
49
+ transitFares: Fare;
33
50
  }
34
51
 
35
52
  export interface TripDetailsProps {
@@ -53,6 +70,10 @@ export interface TripDetailsProps {
53
70
  * Slot for a custom component to render the expandable section for fares.
54
71
  */
55
72
  FareDetails?: React.ElementType<FareDetailsProps>;
73
+ /**
74
+ * Column and table configuration for fare details/fare by leg table.
75
+ */
76
+ fareDetailsLayout?: FareTableLayout[];
56
77
  /**
57
78
  * Mapping between fare keys and human-readable names for them.
58
79
  */
package/src/utils.tsx ADDED
@@ -0,0 +1,30 @@
1
+ import React, { ReactElement } from "react";
2
+ import { FormattedNumber } from "react-intl";
3
+
4
+ /**
5
+ * Format text bold (used with FormattedMessage).
6
+ */
7
+ export function boldText(contents: ReactElement): ReactElement {
8
+ return <strong>{contents}</strong>;
9
+ }
10
+
11
+ /**
12
+ * Render formatted fare.
13
+ * @param currencyCode The ISO currency code to use (USD, GBP, EUR).
14
+ * @param fare The fare value, in currency units, to be shown.
15
+ * @returns The formatted fare value according to the selected locale.
16
+ */
17
+ export function renderFare(currencyCode: string, fare: number): ReactElement {
18
+ return (
19
+ <FormattedNumber
20
+ currency={currencyCode}
21
+ // For dollars in locales such as 'fr',
22
+ // this will limit the display to just the dollar sign
23
+ // (otherwise it will render e.g. '2,50 $US' instead of '2,50 $').
24
+ currencyDisplay="narrowSymbol"
25
+ value={fare}
26
+ // eslint-disable-next-line react/style-prop-object
27
+ style="currency"
28
+ />
29
+ );
30
+ }