@opentripplanner/trip-details 2.0.0 → 2.1.0-alpha.1

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.
@@ -0,0 +1,157 @@
1
+ /* eslint-disable import/prefer-default-export */
2
+ import { Leg, Money } from "@opentripplanner/types";
3
+
4
+ import React from "react";
5
+ import styled from "styled-components";
6
+ import { FormattedNumber } from "react-intl";
7
+ import { FareDetailsProps } from "./types";
8
+
9
+ type LegWithFare = Leg & { fares: { [key: string]: { price: Money } } };
10
+
11
+ interface FareTypeTableProps {
12
+ header: string;
13
+ cols: {
14
+ key: string;
15
+ header: string;
16
+ }[];
17
+ legs: LegWithFare[];
18
+ fareTotals: { [fareKey: string]: Money };
19
+ }
20
+
21
+ const TableHeader = styled.thead`
22
+ th {
23
+ max-width: 5ch;
24
+ min-width: 5ch;
25
+ padding: 0.75em 1.5em;
26
+ text-align: center;
27
+ }
28
+ th:nth-of-type(2n + 1) {
29
+ background: #cccccc22;
30
+ }
31
+ th.main {
32
+ color: #ffffffcc;
33
+ background: #333333;
34
+ }
35
+ `;
36
+
37
+ const TotalRow = styled.tr`
38
+ border-top: 2px solid #333;
39
+ font-weight: 600;
40
+ `;
41
+
42
+ const Table = styled.table`
43
+ border-collapse: collapse;
44
+ display: block;
45
+ font-family: sans-serif;
46
+ margin: -8px;
47
+ /* This line can not be sorted alphabetically, because it must come after the margin rule */
48
+ margin-bottom: 16px;
49
+ padding: 0;
50
+
51
+ td {
52
+ text-align: right;
53
+ }
54
+ td:nth-of-type(2n + 1) {
55
+ background: #cccccc22;
56
+ }
57
+ td.no-zebra {
58
+ background: transparent;
59
+ }
60
+ th:first-of-type {
61
+ height: 40px;
62
+ }
63
+ `;
64
+
65
+ const FareTypeTable = (props: FareTypeTableProps): JSX.Element => {
66
+ const { header, fareTotals, cols, legs } = props;
67
+ return (
68
+ <Table>
69
+ <TableHeader>
70
+ <th className="main">{header}</th>
71
+ {cols.map(col => (
72
+ <th key={col.header}>{col.header}</th>
73
+ ))}
74
+ </TableHeader>
75
+ {legs.map((leg, index) => (
76
+ <tr key={index}>
77
+ <td className="no-zebra">{leg.routeShortName}</td>
78
+ {cols.map(col => {
79
+ const fare = leg.fares[col.key];
80
+ return (
81
+ <td key={col.key}>
82
+ <FormattedNumber
83
+ value={fare?.price?.cents / 100 || 0}
84
+ // This isn't a "real" style prop
85
+ // eslint-disable-next-line react/style-prop-object
86
+ style="currency"
87
+ currency={
88
+ fare?.price.currency.currencyCode ||
89
+ // This is how the itinerary-wide currency code is calculated on the back-end
90
+ legs?.[0].fares?.[col.key]?.price?.currency.currencyCode
91
+ }
92
+ currencyDisplay="narrowSymbol"
93
+ />
94
+ </td>
95
+ );
96
+ })}
97
+ </tr>
98
+ ))}
99
+ <TotalRow>
100
+ <td className="no-zebra">Total</td>
101
+ {cols.map(col => {
102
+ const fare = fareTotals[col.key];
103
+ return (
104
+ <td key={col.key}>
105
+ <FormattedNumber
106
+ value={(fare?.cents || 0) / 100}
107
+ // This isn't a "real" style prop
108
+ // eslint-disable-next-line react/style-prop-object
109
+ style="currency"
110
+ currency={fare?.currency?.currencyCode}
111
+ currencyDisplay="narrowSymbol"
112
+ />
113
+ </td>
114
+ );
115
+ })}
116
+ </TotalRow>
117
+ </Table>
118
+ );
119
+ };
120
+
121
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
122
+ export const FareDetails = (props: FareDetailsProps): JSX.Element => {
123
+ const { legs, transitFares, layout } = props;
124
+ const fareKeys = Object.keys(transitFares.details);
125
+
126
+ const legsWithFares = legs
127
+ .map((leg, index) => {
128
+ const fares = fareKeys.reduce((prev, key) => {
129
+ const fareForKey = transitFares.details[key]?.find(
130
+ detail => detail.legIndex === index
131
+ );
132
+ if (fareForKey) {
133
+ prev[key] = fareForKey;
134
+ }
135
+ return prev;
136
+ }, {});
137
+ return {
138
+ ...leg,
139
+ fares
140
+ };
141
+ })
142
+ .filter(leg => leg.transitLeg);
143
+
144
+ return (
145
+ <div>
146
+ {layout.map(config => (
147
+ <FareTypeTable
148
+ header={config.header}
149
+ cols={config.cols}
150
+ key={config.header}
151
+ legs={legsWithFares}
152
+ fareTotals={transitFares.fare}
153
+ />
154
+ ))}
155
+ </div>
156
+ );
157
+ };
package/src/index.tsx CHANGED
@@ -1,5 +1,6 @@
1
1
  import flatten from "flat";
2
- // @ts-expect-error FIXME: Create TypeScript types for core-utils packages.
2
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
3
+ // @ts-ignore FIXME: Create TypeScript types for core-utils packages.
3
4
  import coreUtils from "@opentripplanner/core-utils";
4
5
  import { FlexBookingInfo } from "@opentripplanner/types";
5
6
  import React, { ReactElement } from "react";
@@ -10,6 +11,7 @@ import { Heartbeat } from "@styled-icons/fa-solid/Heartbeat";
10
11
  import { MoneyBillAlt } from "@styled-icons/fa-solid/MoneyBillAlt";
11
12
  import { PhoneVolume } from "@styled-icons/fa-solid/PhoneVolume";
12
13
  import { Route } from "@styled-icons/fa-solid/Route";
14
+ import { FareDetails } from "./fare-detail";
13
15
 
14
16
  import * as S from "./styled";
15
17
  import TripDetail from "./trip-detail";
@@ -131,8 +133,8 @@ const TransitFare = ({
131
133
  name: fareKeyNameMap[fareKey] || fareNameFallback || fareKey,
132
134
  strong: boldText,
133
135
  value: renderFare(
134
- currentFare.currencyCode,
135
- currentFare.transitFare / 100
136
+ currentFare.currency.currencyCode,
137
+ currentFare.cents / 100
136
138
  )
137
139
  }}
138
140
  />
@@ -148,18 +150,14 @@ export function TripDetails({
148
150
  className = "",
149
151
  DepartureDetails = null,
150
152
  defaultFareKey = "regular",
151
- FareDetails = null,
152
153
  fareKeyNameMap = {},
153
- itinerary
154
+ itinerary,
155
+ fareDetailsLayout
154
156
  }: TripDetailsProps): ReactElement {
155
157
  // process the transit fare
156
- const fareResult = coreUtils.itinerary.calculateFares(itinerary, true);
157
- const { maxTNCFare, minTNCFare, tncCurrencyCode, transitFares } = fareResult;
158
-
159
- let defaultFare = defaultFareKey;
160
- if (!transitFares[defaultFareKey]) {
161
- defaultFare = "regular";
162
- }
158
+ const fareResult = coreUtils.itinerary.calculateTncFares(itinerary);
159
+ const { maxTNCFare, minTNCFare, tncCurrencyCode } = fareResult;
160
+ const transitFares = itinerary?.fare?.fare;
163
161
 
164
162
  let companies = "";
165
163
  itinerary.legs.forEach(leg => {
@@ -172,6 +170,11 @@ export function TripDetails({
172
170
  const fareKeys = transitFares && Object.keys(transitFares).sort();
173
171
 
174
172
  if (transitFares && fareKeys.length > 0) {
173
+ let defaultFare = defaultFareKey;
174
+ if (!transitFares[defaultFareKey]) {
175
+ defaultFare = "regular";
176
+ }
177
+
175
178
  // Depending on if there are additional fares to display either render a <span> or a <details>
176
179
  const TransitFareWrapper =
177
180
  transitFares && fareKeys.length > 1 ? S.TransitFare : S.TransitFareSingle;
@@ -179,7 +182,10 @@ export function TripDetails({
179
182
  fare = (
180
183
  <S.Fare>
181
184
  <TransitFareWrapper>
182
- <summary style={{ display: fareKeys.length > 1 ? "list-item" : "" }}>
185
+ <summary
186
+ role="button"
187
+ style={{ display: fareKeys.length > 1 ? "list-item" : "" }}
188
+ >
183
189
  <TransitFare
184
190
  fareNameFallback={
185
191
  <FormattedMessage
@@ -289,12 +295,14 @@ export function TripDetails({
289
295
  {fare && (
290
296
  <TripDetail
291
297
  // Any custom description for the transit fare needs to be handled by the slot.
298
+ // TODO: add leg info
292
299
  description={
293
- FareDetails && (
300
+ fareDetailsLayout &&
301
+ Object.keys(itinerary.fare.details).length > 0 && (
294
302
  <FareDetails
295
- maxTNCFare={maxTNCFare}
296
- minTNCFare={minTNCFare}
297
- transitFares={transitFares}
303
+ transitFares={itinerary.fare}
304
+ legs={itinerary.legs}
305
+ layout={fareDetailsLayout}
298
306
  />
299
307
  )
300
308
  }
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 } from "@opentripplanner/types";
3
+ import type { Itinerary, Leg, Fare } from "@opentripplanner/types";
4
4
  import type { ReactElement } from "react";
5
5
 
6
6
  export interface CaloriesDetailsProps {
@@ -13,17 +13,18 @@ export interface DepartureDetailsProps {
13
13
  departureDate: Date;
14
14
  }
15
15
 
16
- export interface TransitFareData {
17
- [key: string]: {
18
- currencyCode: string;
19
- transitFare: number;
20
- }
16
+ export interface FareDetailsLayout {
17
+ header: string;
18
+ cols: {
19
+ key: string;
20
+ header: string;
21
+ }[];
21
22
  }
22
23
 
23
24
  export interface FareDetailsProps {
24
- maxTNCFare: number;
25
- minTNCFare: number;
26
- transitFares: TransitFareData;
25
+ transitFares: Fare;
26
+ legs: Leg[];
27
+ layout: FareDetailsLayout[];
27
28
  }
28
29
 
29
30
  export interface TransitFareProps {
@@ -32,7 +33,7 @@ export interface TransitFareProps {
32
33
  fareKeyNameMap: {
33
34
  [key: string]: string;
34
35
  };
35
- transitFares: TransitFareData;
36
+ transitFares: Fare;
36
37
  }
37
38
 
38
39
  export interface TripDetailsProps {
@@ -52,16 +53,16 @@ export interface TripDetailsProps {
52
53
  * Slot for a custom component to render the expandable section for departure.
53
54
  */
54
55
  DepartureDetails?: React.ElementType<DepartureDetailsProps>;
55
- /**
56
- * Slot for a custom component to render the expandable section for fares.
57
- */
58
- FareDetails?: React.ElementType<FareDetailsProps>;
59
56
  /**
60
57
  * Mapping between fare keys and human-readable names for them.
61
58
  */
62
59
  fareKeyNameMap?: {
63
60
  [name: string]: string;
64
61
  };
62
+ /**
63
+ * Column and table configuration for fare details/fare by leg table.
64
+ */
65
+ fareDetailsLayout?: FareDetailsLayout[];
65
66
  /**
66
67
  * Itinerary that the user has selected to view, contains multiple legs.
67
68
  */