@opentripplanner/trip-details 2.1.0-alpha.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.
- package/__mocks__/{custom-messages.yml → custom-english-messages.yml} +7 -0
- package/__mocks__/custom-french-messages.yml +10 -0
- package/esm/fare-table.js +153 -0
- package/esm/fare-table.js.map +1 -0
- package/esm/index.js +23 -43
- package/esm/index.js.map +1 -1
- package/esm/types.js +11 -0
- package/esm/types.js.map +1 -1
- package/esm/utils.js +29 -0
- package/esm/utils.js.map +1 -0
- package/i18n/en-US.yml +7 -0
- package/i18n/fr.yml +7 -0
- package/lib/fare-table.d.ts +4 -0
- package/lib/fare-table.d.ts.map +1 -0
- package/lib/fare-table.js +160 -0
- package/lib/fare-table.js.map +1 -0
- package/lib/index.d.ts +1 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +30 -50
- package/lib/index.js.map +1 -1
- package/lib/types.d.ts +24 -11
- package/lib/types.d.ts.map +1 -1
- package/lib/types.js +18 -0
- package/lib/types.js.map +1 -1
- package/lib/utils.d.ts +13 -0
- package/lib/utils.d.ts.map +1 -0
- package/lib/utils.js +41 -0
- package/lib/utils.js.map +1 -0
- package/package.json +1 -1
- package/src/TripDetails.story.tsx +54 -38
- package/src/fare-table.tsx +222 -0
- package/src/index.tsx +35 -61
- package/src/types.ts +25 -11
- package/src/utils.tsx +30 -0
- package/tsconfig.tsbuildinfo +58 -68
- package/esm/fare-detail.js +0 -111
- package/esm/fare-detail.js.map +0 -1
- package/lib/fare-detail.d.ts +0 -3
- package/lib/fare-detail.d.ts.map +0 -1
- package/lib/fare-detail.js +0 -115
- package/lib/fare-detail.js.map +0 -1
- package/src/fare-detail.tsx +0 -154
package/lib/utils.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.boldText = boldText;
|
|
9
|
+
exports.renderFare = renderFare;
|
|
10
|
+
|
|
11
|
+
var _react = _interopRequireDefault(require("react"));
|
|
12
|
+
|
|
13
|
+
var _reactIntl = require("react-intl");
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Format text bold (used with FormattedMessage).
|
|
17
|
+
*/
|
|
18
|
+
function boldText(contents) {
|
|
19
|
+
return /*#__PURE__*/_react.default.createElement("strong", null, contents);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Render formatted fare.
|
|
23
|
+
* @param currencyCode The ISO currency code to use (USD, GBP, EUR).
|
|
24
|
+
* @param fare The fare value, in currency units, to be shown.
|
|
25
|
+
* @returns The formatted fare value according to the selected locale.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
function renderFare(currencyCode, fare) {
|
|
30
|
+
return /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedNumber, {
|
|
31
|
+
currency: currencyCode // For dollars in locales such as 'fr',
|
|
32
|
+
// this will limit the display to just the dollar sign
|
|
33
|
+
// (otherwise it will render e.g. '2,50 $US' instead of '2,50 $').
|
|
34
|
+
,
|
|
35
|
+
currencyDisplay: "narrowSymbol",
|
|
36
|
+
value: fare // eslint-disable-next-line react/style-prop-object
|
|
37
|
+
,
|
|
38
|
+
style: "currency"
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=utils.js.map
|
package/lib/utils.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/utils.tsx"],"names":["boldText","contents","renderFare","currencyCode","fare"],"mappings":";;;;;;;;;;AAAA;;AACA;;AAEA;AACA;AACA;AACO,SAASA,QAAT,CAAkBC,QAAlB,EAAwD;AAC7D,sBAAO,6CAASA,QAAT,CAAP;AACD;AAED;AACA;AACA;AACA;AACA;AACA;;;AACO,SAASC,UAAT,CAAoBC,YAApB,EAA0CC,IAA1C,EAAsE;AAC3E,sBACE,6BAAC,0BAAD;AACE,IAAA,QAAQ,EAAED,YADZ,CAEE;AACA;AACA;AAJF;AAKE,IAAA,eAAe,EAAC,cALlB;AAME,IAAA,KAAK,EAAEC,IANT,CAOE;AAPF;AAQE,IAAA,KAAK,EAAC;AARR,IADF;AAYD","sourcesContent":["import React, { ReactElement } from \"react\";\nimport { FormattedNumber } from \"react-intl\";\n\n/**\n * Format text bold (used with FormattedMessage).\n */\nexport function boldText(contents: ReactElement): ReactElement {\n return <strong>{contents}</strong>;\n}\n\n/**\n * Render formatted fare.\n * @param currencyCode The ISO currency code to use (USD, GBP, EUR).\n * @param fare The fare value, in currency units, to be shown.\n * @returns The formatted fare value according to the selected locale.\n */\nexport function renderFare(currencyCode: string, fare: number): ReactElement {\n return (\n <FormattedNumber\n currency={currencyCode}\n // For dollars in locales such as 'fr',\n // this will limit the display to just the dollar sign\n // (otherwise it will render e.g. '2,50 $US' instead of '2,50 $').\n currencyDisplay=\"narrowSymbol\"\n value={fare}\n // eslint-disable-next-line react/style-prop-object\n style=\"currency\"\n />\n );\n}\n"],"file":"utils.js"}
|
package/package.json
CHANGED
|
@@ -14,11 +14,14 @@ import * as TripDetailsClasses from "./styled";
|
|
|
14
14
|
import {
|
|
15
15
|
CaloriesDetailsProps,
|
|
16
16
|
DepartureDetailsProps,
|
|
17
|
+
FareDetailsProps,
|
|
18
|
+
FareTableLayout,
|
|
19
|
+
FareTableText,
|
|
17
20
|
TripDetailsProps
|
|
18
21
|
} from "./types";
|
|
19
22
|
|
|
20
|
-
import
|
|
21
|
-
import
|
|
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,47 +54,47 @@ const StyledTripDetails = styled(TripDetails)`
|
|
|
48
54
|
}
|
|
49
55
|
`;
|
|
50
56
|
|
|
51
|
-
const fareByLegLayout = [
|
|
57
|
+
const fareByLegLayout: FareTableLayout[] = [
|
|
52
58
|
{
|
|
53
|
-
header: "
|
|
59
|
+
header: "regular" as FareTableText,
|
|
54
60
|
cols: [
|
|
55
61
|
{
|
|
56
|
-
|
|
57
|
-
|
|
62
|
+
header: "cash" as FareTableText,
|
|
63
|
+
key: "regular"
|
|
58
64
|
},
|
|
59
65
|
{
|
|
60
|
-
|
|
61
|
-
|
|
66
|
+
header: "electronic" as FareTableText,
|
|
67
|
+
key: "electronicRegular"
|
|
62
68
|
},
|
|
63
69
|
{
|
|
64
|
-
|
|
65
|
-
|
|
70
|
+
header: "special" as FareTableText,
|
|
71
|
+
key: "electronicSpecial"
|
|
66
72
|
}
|
|
67
73
|
]
|
|
68
74
|
},
|
|
69
75
|
{
|
|
70
|
-
header: "
|
|
76
|
+
header: "youth" as FareTableText,
|
|
71
77
|
cols: [
|
|
72
78
|
{
|
|
73
|
-
|
|
74
|
-
|
|
79
|
+
header: "cash" as FareTableText,
|
|
80
|
+
key: "youth"
|
|
75
81
|
},
|
|
76
82
|
{
|
|
77
|
-
|
|
78
|
-
|
|
83
|
+
header: "electronic" as FareTableText,
|
|
84
|
+
key: "electronicYouth"
|
|
79
85
|
}
|
|
80
86
|
]
|
|
81
87
|
},
|
|
82
88
|
{
|
|
83
|
-
header: "
|
|
89
|
+
header: "senior" as FareTableText,
|
|
84
90
|
cols: [
|
|
85
91
|
{
|
|
86
|
-
|
|
87
|
-
|
|
92
|
+
header: "cash" as FareTableText,
|
|
93
|
+
key: "cash"
|
|
88
94
|
},
|
|
89
95
|
{
|
|
90
|
-
|
|
91
|
-
|
|
96
|
+
header: "electronic" as FareTableText,
|
|
97
|
+
key: "electronic"
|
|
92
98
|
}
|
|
93
99
|
]
|
|
94
100
|
}
|
|
@@ -122,6 +128,15 @@ const frenchFareKeyMap = {
|
|
|
122
128
|
electronicSenior: "Tarif Orca séniors"
|
|
123
129
|
};
|
|
124
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
|
+
|
|
125
140
|
// Custom slots for expandable detail sections.
|
|
126
141
|
const CustomDepartureDetails = ({
|
|
127
142
|
departureDate
|
|
@@ -156,8 +171,9 @@ function createTripDetailsTemplate(
|
|
|
156
171
|
{
|
|
157
172
|
CaloriesDetails,
|
|
158
173
|
DepartureDetails,
|
|
159
|
-
|
|
160
|
-
fareDetailsLayout
|
|
174
|
+
FareDetails,
|
|
175
|
+
fareDetailsLayout,
|
|
176
|
+
itinerary
|
|
161
177
|
}: TripDetailsProps,
|
|
162
178
|
{ globals, parameters }: StoryContext
|
|
163
179
|
): ReactElement => {
|
|
@@ -172,9 +188,10 @@ function createTripDetailsTemplate(
|
|
|
172
188
|
<Component
|
|
173
189
|
CaloriesDetails={CaloriesDetails}
|
|
174
190
|
DepartureDetails={DepartureDetails}
|
|
191
|
+
FareDetails={FareDetails}
|
|
192
|
+
fareDetailsLayout={fareDetailsLayout}
|
|
175
193
|
fareKeyNameMap={fareKeyNameMap}
|
|
176
194
|
itinerary={itinerary}
|
|
177
|
-
fareDetailsLayout={fareDetailsLayout}
|
|
178
195
|
/>
|
|
179
196
|
);
|
|
180
197
|
};
|
|
@@ -238,11 +255,19 @@ export const BikeTransitBikeItinerary = makeStory({
|
|
|
238
255
|
export const WalkInterlinedTransitItinerary = makeStory(
|
|
239
256
|
{
|
|
240
257
|
defaultFareKey: "electronicRegular",
|
|
241
|
-
|
|
242
|
-
|
|
258
|
+
fareDetailsLayout: fareByLegLayout,
|
|
259
|
+
itinerary: walkInterlinedTransitItinerary
|
|
243
260
|
},
|
|
244
261
|
{
|
|
245
|
-
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
|
+
}
|
|
246
271
|
}
|
|
247
272
|
);
|
|
248
273
|
|
|
@@ -278,6 +303,7 @@ export const OTP2EScooterRentalTransitItinerary = makeStory({
|
|
|
278
303
|
|
|
279
304
|
export const TncTransitItinerary = makeStory(
|
|
280
305
|
{
|
|
306
|
+
FareDetails: CustomFareDetails,
|
|
281
307
|
itinerary: tncTransitTncItinerary
|
|
282
308
|
},
|
|
283
309
|
{
|
|
@@ -285,7 +311,6 @@ export const TncTransitItinerary = makeStory(
|
|
|
285
311
|
}
|
|
286
312
|
);
|
|
287
313
|
|
|
288
|
-
const flattenedMessages = flatten(customMessages);
|
|
289
314
|
export const TncTransitItineraryWithCustomMessages = makeStory(
|
|
290
315
|
{
|
|
291
316
|
CaloriesDetails: CustomCaloriesDetails,
|
|
@@ -298,8 +323,8 @@ export const TncTransitItineraryWithCustomMessages = makeStory(
|
|
|
298
323
|
// override a subset of localized strings with custom messages.
|
|
299
324
|
reactIntl: {
|
|
300
325
|
messages: {
|
|
301
|
-
"en-US":
|
|
302
|
-
fr:
|
|
326
|
+
"en-US": flattenedEnglishMessages,
|
|
327
|
+
fr: flattenedFrenchMessages
|
|
303
328
|
}
|
|
304
329
|
},
|
|
305
330
|
useCustomFareKeyMap: true
|
|
@@ -310,12 +335,3 @@ export const TncTransitItineraryWithCustomMessages = makeStory(
|
|
|
310
335
|
export const FlexItinerary = makeStory({
|
|
311
336
|
itinerary: fareComponentsItinerary
|
|
312
337
|
});
|
|
313
|
-
|
|
314
|
-
const exampleItinerary = walkInterlinedTransitItinerary;
|
|
315
|
-
export const FareDetailsComponent = () => (
|
|
316
|
-
<FareDetails
|
|
317
|
-
transitFares={exampleItinerary.fare}
|
|
318
|
-
legs={exampleItinerary.legs}
|
|
319
|
-
layout={fareByLegLayout}
|
|
320
|
-
/>
|
|
321
|
-
);
|
|
@@ -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,17 +2,18 @@ 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
|
|
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";
|
|
9
9
|
import { MoneyBillAlt } from "@styled-icons/fa-solid/MoneyBillAlt";
|
|
10
10
|
import { PhoneVolume } from "@styled-icons/fa-solid/PhoneVolume";
|
|
11
11
|
import { Route } from "@styled-icons/fa-solid/Route";
|
|
12
|
-
import { FareDetails } from "./fare-detail";
|
|
13
12
|
|
|
14
13
|
import * as S from "./styled";
|
|
15
14
|
import TripDetail from "./trip-detail";
|
|
15
|
+
import FareLegTable from "./fare-table";
|
|
16
|
+
import { boldText, renderFare } from "./utils";
|
|
16
17
|
|
|
17
18
|
import {
|
|
18
19
|
CaloriesDetailsProps,
|
|
@@ -29,35 +30,6 @@ import defaultEnglishMessages from "../i18n/en-US.yml";
|
|
|
29
30
|
// - the yaml loader for jest returns messages with flattened ids.
|
|
30
31
|
const defaultMessages: Record<string, string> = flatten(defaultEnglishMessages);
|
|
31
32
|
|
|
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
33
|
/**
|
|
62
34
|
* Helper function to specify the link to dietary table.
|
|
63
35
|
*/
|
|
@@ -146,16 +118,18 @@ const TransitFare = ({
|
|
|
146
118
|
export function TripDetails({
|
|
147
119
|
CaloriesDetails = DefaultCaloriesDetails,
|
|
148
120
|
className = "",
|
|
149
|
-
DepartureDetails = null,
|
|
150
121
|
defaultFareKey = "regular",
|
|
122
|
+
DepartureDetails = null,
|
|
123
|
+
FareDetails = null,
|
|
124
|
+
fareDetailsLayout,
|
|
151
125
|
fareKeyNameMap = {},
|
|
152
|
-
itinerary
|
|
153
|
-
fareDetailsLayout
|
|
126
|
+
itinerary
|
|
154
127
|
}: TripDetailsProps): ReactElement {
|
|
155
128
|
// process the transit fare
|
|
156
129
|
const fareResult = coreUtils.itinerary.calculateTncFares(itinerary);
|
|
157
130
|
const { maxTNCFare, minTNCFare, tncCurrencyCode } = fareResult;
|
|
158
131
|
const transitFares = itinerary?.fare?.fare;
|
|
132
|
+
const fareDetails = itinerary.fare?.details;
|
|
159
133
|
|
|
160
134
|
let companies = "";
|
|
161
135
|
itinerary.legs.forEach(leg => {
|
|
@@ -180,10 +154,7 @@ export function TripDetails({
|
|
|
180
154
|
fare = (
|
|
181
155
|
<S.Fare>
|
|
182
156
|
<TransitFareWrapper>
|
|
183
|
-
<summary
|
|
184
|
-
role="button"
|
|
185
|
-
style={{ display: fareKeys.length > 1 ? "list-item" : "" }}
|
|
186
|
-
>
|
|
157
|
+
<summary style={{ display: fareKeys.length > 1 ? "list-item" : "" }}>
|
|
187
158
|
<TransitFare
|
|
188
159
|
fareNameFallback={
|
|
189
160
|
<FormattedMessage
|
|
@@ -199,20 +170,31 @@ export function TripDetails({
|
|
|
199
170
|
transitFares={transitFares}
|
|
200
171
|
/>
|
|
201
172
|
</summary>
|
|
202
|
-
{
|
|
203
|
-
//
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
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
|
+
)}
|
|
216
198
|
</TransitFareWrapper>
|
|
217
199
|
{minTNCFare !== 0 && (
|
|
218
200
|
<S.TNCFare>
|
|
@@ -293,16 +275,8 @@ export function TripDetails({
|
|
|
293
275
|
{fare && (
|
|
294
276
|
<TripDetail
|
|
295
277
|
// Any custom description for the transit fare needs to be handled by the slot.
|
|
296
|
-
// TODO: add leg info
|
|
297
278
|
description={
|
|
298
|
-
|
|
299
|
-
Object.keys(itinerary.fare.details).length > 0 && (
|
|
300
|
-
<FareDetails
|
|
301
|
-
transitFares={itinerary.fare}
|
|
302
|
-
legs={itinerary.legs}
|
|
303
|
-
layout={fareDetailsLayout}
|
|
304
|
-
/>
|
|
305
|
-
)
|
|
279
|
+
FareDetails && <FareDetails transitFares={transitFares} />
|
|
306
280
|
}
|
|
307
281
|
icon={<MoneyBillAlt size={17} />}
|
|
308
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 {
|
|
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,31 @@ export interface DepartureDetailsProps {
|
|
|
13
13
|
departureDate: Date;
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
export
|
|
17
|
-
|
|
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
|
-
|
|
29
|
-
legs
|
|
30
|
-
|
|
37
|
+
layout?: FareTableLayout[];
|
|
38
|
+
legs?: Leg[];
|
|
39
|
+
transitFareDetails?: FareDetails;
|
|
40
|
+
transitFares?: TransitFareData;
|
|
31
41
|
}
|
|
32
42
|
|
|
33
43
|
export interface TransitFareProps {
|
|
@@ -56,16 +66,20 @@ export interface TripDetailsProps {
|
|
|
56
66
|
* Slot for a custom component to render the expandable section for departure.
|
|
57
67
|
*/
|
|
58
68
|
DepartureDetails?: React.ElementType<DepartureDetailsProps>;
|
|
69
|
+
/**
|
|
70
|
+
* Slot for a custom component to render the expandable section for fares.
|
|
71
|
+
*/
|
|
72
|
+
FareDetails?: React.ElementType<FareDetailsProps>;
|
|
73
|
+
/**
|
|
74
|
+
* Column and table configuration for fare details/fare by leg table.
|
|
75
|
+
*/
|
|
76
|
+
fareDetailsLayout?: FareTableLayout[];
|
|
59
77
|
/**
|
|
60
78
|
* Mapping between fare keys and human-readable names for them.
|
|
61
79
|
*/
|
|
62
80
|
fareKeyNameMap?: {
|
|
63
81
|
[name: string]: string;
|
|
64
82
|
};
|
|
65
|
-
/**
|
|
66
|
-
* Column and table configuration for fare details/fare by leg table.
|
|
67
|
-
*/
|
|
68
|
-
fareDetailsLayout?: FareDetailsLayout[];
|
|
69
83
|
/**
|
|
70
84
|
* Itinerary that the user has selected to view, contains multiple legs.
|
|
71
85
|
*/
|