@opentripplanner/trip-details 2.1.0-alpha.2 → 2.1.2

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.
Files changed (50) hide show
  1. package/__mocks__/{custom-messages.yml → custom-english-messages.yml} +7 -0
  2. package/__mocks__/custom-french-messages.yml +10 -0
  3. package/esm/fare-table.js +153 -0
  4. package/esm/fare-table.js.map +1 -0
  5. package/esm/index.js +34 -91
  6. package/esm/index.js.map +1 -1
  7. package/esm/styled.js +7 -15
  8. package/esm/styled.js.map +1 -1
  9. package/esm/types.js +11 -0
  10. package/esm/types.js.map +1 -1
  11. package/esm/utils.js +29 -0
  12. package/esm/utils.js.map +1 -0
  13. package/i18n/en-US.yml +9 -18
  14. package/i18n/fr.yml +7 -17
  15. package/lib/fare-table.d.ts +4 -0
  16. package/lib/fare-table.d.ts.map +1 -0
  17. package/lib/fare-table.js +160 -0
  18. package/lib/fare-table.js.map +1 -0
  19. package/lib/index.d.ts +1 -1
  20. package/lib/index.d.ts.map +1 -1
  21. package/lib/index.js +37 -97
  22. package/lib/index.js.map +1 -1
  23. package/lib/styled.d.ts +0 -2
  24. package/lib/styled.d.ts.map +1 -1
  25. package/lib/styled.js +8 -22
  26. package/lib/styled.js.map +1 -1
  27. package/lib/types.d.ts +29 -11
  28. package/lib/types.d.ts.map +1 -1
  29. package/lib/types.js +18 -0
  30. package/lib/types.js.map +1 -1
  31. package/lib/utils.d.ts +13 -0
  32. package/lib/utils.d.ts.map +1 -0
  33. package/lib/utils.js +41 -0
  34. package/lib/utils.js.map +1 -0
  35. package/package.json +1 -1
  36. package/src/TripDetails.story.tsx +57 -38
  37. package/src/fare-table.tsx +222 -0
  38. package/src/index.tsx +46 -120
  39. package/src/styled.ts +0 -4
  40. package/src/types.ts +31 -11
  41. package/src/utils.tsx +30 -0
  42. package/tsconfig.json +1 -0
  43. package/tsconfig.tsbuildinfo +69 -100
  44. package/esm/fare-detail.js +0 -111
  45. package/esm/fare-detail.js.map +0 -1
  46. package/lib/fare-detail.d.ts +0 -3
  47. package/lib/fare-detail.d.ts.map +0 -1
  48. package/lib/fare-detail.js +0 -115
  49. package/lib/fare-detail.js.map +0 -1
  50. package/src/fare-detail.tsx +0 -154
@@ -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 { FareLegTableProps, 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
+ legs,
184
+ transitFareDetails,
185
+ transitFares
186
+ }: FareLegTableProps): 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
@@ -1,18 +1,16 @@
1
1
  import flatten from "flat";
2
2
  import coreUtils from "@opentripplanner/core-utils";
3
- import { FlexBookingInfo } from "@opentripplanner/types";
4
3
  import React, { ReactElement } from "react";
5
- import { FormattedMessage, FormattedNumber } from "react-intl";
4
+ import { FormattedMessage } from "react-intl";
6
5
  import { CalendarAlt } from "@styled-icons/fa-solid/CalendarAlt";
7
- import { HandPaper } from "@styled-icons/fa-solid/HandPaper";
8
6
  import { Heartbeat } from "@styled-icons/fa-solid/Heartbeat";
9
7
  import { MoneyBillAlt } from "@styled-icons/fa-solid/MoneyBillAlt";
10
- import { PhoneVolume } from "@styled-icons/fa-solid/PhoneVolume";
11
8
  import { Route } from "@styled-icons/fa-solid/Route";
12
- import { FareDetails } from "./fare-detail";
13
9
 
14
10
  import * as S from "./styled";
15
11
  import TripDetail from "./trip-detail";
12
+ import FareLegTable from "./fare-table";
13
+ import { boldText, renderFare } from "./utils";
16
14
 
17
15
  import {
18
16
  CaloriesDetailsProps,
@@ -29,35 +27,6 @@ import defaultEnglishMessages from "../i18n/en-US.yml";
29
27
  // - the yaml loader for jest returns messages with flattened ids.
30
28
  const defaultMessages: Record<string, string> = flatten(defaultEnglishMessages);
31
29
 
32
- /**
33
- * Format text bold (used with FormattedMessage).
34
- */
35
- // TODO: Find a better place for this utility.
36
- function boldText(contents: ReactElement): ReactElement {
37
- return <strong>{contents}</strong>;
38
- }
39
-
40
- /**
41
- * Render formatted fare.
42
- * @param currencyCode The ISO currency code to use (USD, GBP, EUR).
43
- * @param fare The fare value, in currency units, to be shown.
44
- * @returns The formatted fare value according to the selected locale.
45
- */
46
- function renderFare(currencyCode: string, fare: number): ReactElement {
47
- return (
48
- <FormattedNumber
49
- currency={currencyCode}
50
- // For dollars in locales such as 'fr',
51
- // this will limit the display to just the dollar sign
52
- // (otherwise it will render e.g. '2,50 $US' instead of '2,50 $').
53
- currencyDisplay="narrowSymbol"
54
- value={fare}
55
- // eslint-disable-next-line react/style-prop-object
56
- style="currency"
57
- />
58
- );
59
- }
60
-
61
30
  /**
62
31
  * Helper function to specify the link to dietary table.
63
32
  */
@@ -72,19 +41,6 @@ function dietaryLink(contents: ReactElement): ReactElement {
72
41
  </a>
73
42
  );
74
43
  }
75
-
76
- /**
77
- * Helper function that assembles values for flex pickup/dropoff messages.
78
- */
79
- function getFlexMessageValues(info: FlexBookingInfo) {
80
- return {
81
- hasLeadTime: coreUtils.itinerary.isAdvanceBookingRequired(info),
82
- hasPhone: !!info?.contactInfo?.phoneNumber,
83
- leadDays: info.latestBookingTime.daysPrior,
84
- phoneNumber: info?.contactInfo?.phoneNumber
85
- };
86
- }
87
-
88
44
  /**
89
45
  * Default rendering if no component is provided for the CaloriesDetails
90
46
  * slot in the TripDetails component.
@@ -146,16 +102,18 @@ const TransitFare = ({
146
102
  export function TripDetails({
147
103
  CaloriesDetails = DefaultCaloriesDetails,
148
104
  className = "",
149
- DepartureDetails = null,
150
105
  defaultFareKey = "regular",
106
+ DepartureDetails = null,
107
+ FareDetails = null,
108
+ fareDetailsLayout,
151
109
  fareKeyNameMap = {},
152
- itinerary,
153
- fareDetailsLayout
110
+ itinerary
154
111
  }: TripDetailsProps): ReactElement {
155
112
  // process the transit fare
156
113
  const fareResult = coreUtils.itinerary.calculateTncFares(itinerary);
157
114
  const { maxTNCFare, minTNCFare, tncCurrencyCode } = fareResult;
158
115
  const transitFares = itinerary?.fare?.fare;
116
+ const fareDetails = itinerary.fare?.details;
159
117
 
160
118
  let companies = "";
161
119
  itinerary.legs.forEach(leg => {
@@ -180,10 +138,7 @@ export function TripDetails({
180
138
  fare = (
181
139
  <S.Fare>
182
140
  <TransitFareWrapper>
183
- <summary
184
- role="button"
185
- style={{ display: fareKeys.length > 1 ? "list-item" : "" }}
186
- >
141
+ <summary style={{ display: fareKeys.length > 1 ? "list-item" : "" }}>
187
142
  <TransitFare
188
143
  fareNameFallback={
189
144
  <FormattedMessage
@@ -199,20 +154,31 @@ export function TripDetails({
199
154
  transitFares={transitFares}
200
155
  />
201
156
  </summary>
202
- {fareKeys.map(fareKey => {
203
- // Don't show the default fare twice!
204
- if (fareKey === defaultFare) {
205
- return null;
206
- }
207
- return (
208
- <TransitFare
209
- fareKey={fareKey}
210
- key={fareKey}
211
- fareKeyNameMap={fareKeyNameMap}
212
- transitFares={transitFares}
213
- />
214
- );
215
- })}
157
+ {fareDetailsLayout ? (
158
+ // Show full ƒare details by leg
159
+ <FareLegTable
160
+ layout={fareDetailsLayout}
161
+ legs={itinerary.legs}
162
+ transitFareDetails={fareDetails}
163
+ transitFares={transitFares}
164
+ />
165
+ ) : (
166
+ // Just show the fares for each payment type
167
+ fareKeys.map(fareKey => {
168
+ // Don't show the default fare twice!
169
+ if (fareKey === defaultFare) {
170
+ return null;
171
+ }
172
+ return (
173
+ <TransitFare
174
+ fareKey={fareKey}
175
+ key={fareKey}
176
+ fareKeyNameMap={fareKeyNameMap}
177
+ transitFares={transitFares}
178
+ />
179
+ );
180
+ })
181
+ )}
216
182
  </TransitFareWrapper>
217
183
  {minTNCFare !== 0 && (
218
184
  <S.TNCFare>
@@ -293,14 +259,12 @@ export function TripDetails({
293
259
  {fare && (
294
260
  <TripDetail
295
261
  // Any custom description for the transit fare needs to be handled by the slot.
296
- // TODO: add leg info
297
262
  description={
298
- fareDetailsLayout &&
299
- Object.keys(itinerary.fare.details).length > 0 && (
263
+ FareDetails && (
300
264
  <FareDetails
301
- transitFares={itinerary.fare}
302
- legs={itinerary.legs}
303
- layout={fareDetailsLayout}
265
+ maxTNCFare={maxTNCFare}
266
+ minTNCFare={minTNCFare}
267
+ transitFares={transitFares}
304
268
  />
305
269
  )
306
270
  }
@@ -345,58 +309,20 @@ export function TripDetails({
345
309
  }
346
310
  description="Text stating that portions of the trip include a flex (on-demand) transit service."
347
311
  id="otpUi.TripDetails.tripIncludesFlex"
312
+ values={{
313
+ extraMessage: [
314
+ ...new Set([
315
+ ...pickupBookingInfo.map(info => info.message),
316
+ ...dropOffBookingInfo.map(info => info.message)
317
+ ])
318
+ ].join(" ")
319
+ }}
348
320
  />
349
321
  </S.FlexSummary>
350
322
  }
351
323
  icon={<Route size={17} />}
352
324
  />
353
325
  )}
354
- {pickupBookingInfo &&
355
- pickupBookingInfo.map(info => (
356
- <TripDetail
357
- key={info.pickupMessage}
358
- icon={<PhoneVolume size={17} />}
359
- summary={
360
- <S.FlexPickupSummary>
361
- <FormattedMessage
362
- defaultMessage={
363
- defaultMessages["otpUi.TripDetails.flexPickupMessage"]
364
- }
365
- description="Instructions for booking and boarding the flex (on-demand) transit service."
366
- id="otpUi.TripDetails.flexPickupMessage"
367
- values={getFlexMessageValues(info)}
368
- />
369
- </S.FlexPickupSummary>
370
- }
371
- description={info.pickupMessage}
372
- />
373
- ))}
374
- {dropOffBookingInfo &&
375
- dropOffBookingInfo.map(info => (
376
- <TripDetail
377
- description={info.dropOffMessage}
378
- icon={
379
- coreUtils.itinerary.isAdvanceBookingRequired(info) ? (
380
- <PhoneVolume size={17} />
381
- ) : (
382
- <HandPaper size={17} />
383
- )
384
- }
385
- key={info.dropOffMessage}
386
- summary={
387
- <S.FlexDropOffSummary>
388
- <FormattedMessage
389
- defaultMessage={
390
- defaultMessages["otpUi.TripDetails.flexDropOffMessage"]
391
- }
392
- description="Instructions for getting off the flex (on-demand) transit service."
393
- id="otpUi.TripDetails.flexDropOffMessage"
394
- values={getFlexMessageValues(info)}
395
- />
396
- </S.FlexDropOffSummary>
397
- }
398
- />
399
- ))}
400
326
  </S.TripDetailsBody>
401
327
  </S.TripDetails>
402
328
  );
package/src/styled.ts CHANGED
@@ -58,10 +58,6 @@ export const TransitFareSingle = styled.span``;
58
58
 
59
59
  export const FlexSummary = styled.span``;
60
60
 
61
- export const FlexPickupSummary = styled.span``;
62
-
63
- export const FlexDropOffSummary = styled.span``;
64
-
65
61
  export const TripDetail = styled.div`
66
62
  margin-top: 6px;
67
63
  `;
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 { Fare, Leg, 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,21 +13,37 @@ export interface DepartureDetailsProps {
13
13
  departureDate: Date;
14
14
  }
15
15
 
16
- export interface FareDetailsLayout {
17
- header: string;
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 {
18
26
  cols: {
27
+ header: FareTableText;
19
28
  key: string;
20
- header: string;
21
29
  }[];
30
+ header: FareTableText;
22
31
  }
23
32
  export interface TransitFareData {
24
33
  [key: string]: Money
25
34
  }
26
35
 
27
36
  export interface FareDetailsProps {
28
- transitFares: Fare;
29
- legs: Leg[];
30
- layout: FareDetailsLayout[];
37
+ maxTNCFare: number;
38
+ minTNCFare: number;
39
+ transitFares: TransitFareData;
40
+ }
41
+
42
+ export interface FareLegTableProps {
43
+ layout?: FareTableLayout[];
44
+ legs?: Leg[];
45
+ transitFareDetails?: FareDetails;
46
+ transitFares?: TransitFareData;
31
47
  }
32
48
 
33
49
  export interface TransitFareProps {
@@ -56,16 +72,20 @@ export interface TripDetailsProps {
56
72
  * Slot for a custom component to render the expandable section for departure.
57
73
  */
58
74
  DepartureDetails?: React.ElementType<DepartureDetailsProps>;
75
+ /**
76
+ * Slot for a custom component to render the expandable section for fares.
77
+ */
78
+ FareDetails?: React.ElementType<FareDetailsProps>;
79
+ /**
80
+ * Column and table configuration for fare details/fare by leg table.
81
+ */
82
+ fareDetailsLayout?: FareTableLayout[];
59
83
  /**
60
84
  * Mapping between fare keys and human-readable names for them.
61
85
  */
62
86
  fareKeyNameMap?: {
63
87
  [name: string]: string;
64
88
  };
65
- /**
66
- * Column and table configuration for fare details/fare by leg table.
67
- */
68
- fareDetailsLayout?: FareDetailsLayout[];
69
89
  /**
70
90
  * Itinerary that the user has selected to view, contains multiple legs.
71
91
  */
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
+ }
package/tsconfig.json CHANGED
@@ -4,6 +4,7 @@
4
4
  "composite": true,
5
5
  "outDir": "./lib",
6
6
  "rootDir": "./src",
7
+ "target": "ES2015",
7
8
  "skipLibCheck": true
8
9
  },
9
10
  "include": ["src/**/*"],