@opentripplanner/trip-details 2.1.3-alpha.1 → 2.2.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/src/index.tsx CHANGED
@@ -1,10 +1,11 @@
1
1
  import flatten from "flat";
2
2
  import coreUtils from "@opentripplanner/core-utils";
3
3
  import React, { ReactElement } from "react";
4
- import { FormattedMessage } from "react-intl";
4
+ import { FormattedMessage, FormattedNumber } from "react-intl";
5
5
  import { CalendarAlt } from "@styled-icons/fa-solid/CalendarAlt";
6
6
  import { Heartbeat } from "@styled-icons/fa-solid/Heartbeat";
7
7
  import { MoneyBillAlt } from "@styled-icons/fa-solid/MoneyBillAlt";
8
+ import { Leaf } from "@styled-icons/fa-solid/Leaf";
8
9
  import { Route } from "@styled-icons/fa-solid/Route";
9
10
 
10
11
  import * as S from "./styled";
@@ -27,6 +28,8 @@ import defaultEnglishMessages from "../i18n/en-US.yml";
27
28
  // - the yaml loader for jest returns messages with flattened ids.
28
29
  const defaultMessages: Record<string, string> = flatten(defaultEnglishMessages);
29
30
 
31
+ const subText = contents => <sub>{contents}</sub>;
32
+
30
33
  /**
31
34
  * Helper function to specify the link to dietary table.
32
35
  */
@@ -41,6 +44,19 @@ function dietaryLink(contents: ReactElement): ReactElement {
41
44
  </a>
42
45
  );
43
46
  }
47
+
48
+ function CO2DescriptionLink(contents: ReactElement): ReactElement {
49
+ return (
50
+ <a
51
+ href="https://www.itf-oecd.org/sites/default/files/life-cycle-assessment-calculations-2020.xlsx"
52
+ rel="noopener noreferrer"
53
+ target="_blank"
54
+ >
55
+ {contents}
56
+ </a>
57
+ );
58
+ }
59
+
44
60
  /**
45
61
  * Default rendering if no component is provided for the CaloriesDetails
46
62
  * slot in the TripDetails component.
@@ -107,11 +123,12 @@ export function TripDetails({
107
123
  FareDetails = null,
108
124
  fareDetailsLayout,
109
125
  fareKeyNameMap = {},
110
- itinerary
126
+ itinerary,
127
+ co2Config
111
128
  }: TripDetailsProps): ReactElement {
112
129
  // process the transit fare
113
130
  const fareResult = coreUtils.itinerary.calculateTncFares(itinerary);
114
- const { maxTNCFare, minTNCFare, tncCurrencyCode } = fareResult;
131
+ const { currencyCode, maxTNCFare, minTNCFare } = fareResult;
115
132
  const transitFares = itinerary?.fare?.fare;
116
133
  const fareDetails = itinerary.fare?.details;
117
134
 
@@ -138,10 +155,7 @@ export function TripDetails({
138
155
  fare = (
139
156
  <S.Fare>
140
157
  <TransitFareWrapper>
141
- <summary
142
- role="button"
143
- style={{ display: fareKeys.length > 1 ? "list-item" : "" }}
144
- >
158
+ <summary style={{ display: fareKeys.length > 1 ? "list-item" : "" }}>
145
159
  <TransitFare
146
160
  fareNameFallback={
147
161
  <FormattedMessage
@@ -198,8 +212,8 @@ export function TripDetails({
198
212
  {companies.toLowerCase()}
199
213
  </S.TNCFareCompanies>
200
214
  ),
201
- maxTNCFare: renderFare(tncCurrencyCode, maxTNCFare),
202
- minTNCFare: renderFare(tncCurrencyCode, minTNCFare),
215
+ maxTNCFare: renderFare(currencyCode, maxTNCFare),
216
+ minTNCFare: renderFare(currencyCode, minTNCFare),
203
217
  strong: boldText
204
218
  }}
205
219
  />
@@ -218,6 +232,12 @@ export function TripDetails({
218
232
  walkDuration
219
233
  } = coreUtils.itinerary.calculatePhysicalActivity(itinerary);
220
234
 
235
+ const co2 = coreUtils.itinerary.calculateEmissions(
236
+ itinerary,
237
+ co2Config?.carbonIntensity,
238
+ co2Config?.units
239
+ );
240
+
221
241
  // Parse flex info and generate appropriate strings
222
242
  const containsFlex = itinerary.legs.some(coreUtils.itinerary.isFlex);
223
243
  const pickupBookingInfo = itinerary.legs
@@ -226,6 +246,10 @@ export function TripDetails({
226
246
  const dropOffBookingInfo = itinerary.legs
227
247
  .map(leg => leg.dropOffBookingInfo)
228
248
  .filter(info => !!info);
249
+ const totalDistance = itinerary.legs.reduce(
250
+ (total, leg) => total + leg.distance,
251
+ 0
252
+ );
229
253
 
230
254
  return (
231
255
  <S.TripDetails className={className}>
@@ -302,6 +326,47 @@ export function TripDetails({
302
326
  }
303
327
  />
304
328
  )}
329
+ {co2 > 0 && co2Config?.enabled && (
330
+ <TripDetail
331
+ icon={<Leaf size={17} />}
332
+ summary={
333
+ <S.CO2Summary>
334
+ <FormattedMessage
335
+ defaultMessage={defaultMessages["otpUi.TripDetails.co2"]}
336
+ description="Text showing the quantity of CO2 emitted by this trip."
337
+ id="otpUi.TripDetails.co2"
338
+ values={{
339
+ co2: (
340
+ <FormattedNumber
341
+ value={Math.round(co2)}
342
+ // eslint-disable-next-line react/style-prop-object
343
+ style="unit"
344
+ unit={co2Config?.units || "gram"}
345
+ unitDisplay="narrow"
346
+ />
347
+ ),
348
+ strong: boldText,
349
+ sub: subText
350
+ }}
351
+ />
352
+ </S.CO2Summary>
353
+ }
354
+ description={
355
+ <FormattedMessage
356
+ defaultMessage={
357
+ defaultMessages["otpUi.TripDetails.co2description"]
358
+ }
359
+ values={{
360
+ link: CO2DescriptionLink,
361
+ sub: subText,
362
+ totalDistance
363
+ }}
364
+ description="Text explaining how the CO2 emissions is calculated."
365
+ id="otpUi.TripDetails.co2description"
366
+ />
367
+ }
368
+ />
369
+ )}
305
370
  {containsFlex && (
306
371
  <TripDetail
307
372
  summary={
package/src/styled.ts CHANGED
@@ -21,6 +21,10 @@ export const CaloriesDescription = styled.span``;
21
21
 
22
22
  export const CaloriesSummary = styled.span``;
23
23
 
24
+ export const CO2Description = styled.span``;
25
+
26
+ export const CO2Summary = styled.span``;
27
+
24
28
  export const ExpandButton = styled(BaseButton).attrs({
25
29
  "aria-label": "What does this mean?"
26
30
  })`
@@ -1,8 +1,8 @@
1
1
  import React, { Component, ReactElement } from "react";
2
2
  import { QuestionCircle } from "@styled-icons/fa-solid/QuestionCircle";
3
3
  import { TimesCircle } from "@styled-icons/fa-solid/TimesCircle";
4
- import { VelocityTransitionGroup } from "velocity-react";
5
4
 
5
+ import AnimateHeight from "react-animate-height";
6
6
  import * as S from "./styled";
7
7
 
8
8
  type Props = {
@@ -51,19 +51,14 @@ export default class TripDetail extends Component<Props, State> {
51
51
  </S.ExpandButton>
52
52
  )}
53
53
  </S.TripDetailSummary>
54
- <VelocityTransitionGroup
55
- enter={{ animation: "slideDown" }}
56
- leave={{ animation: "slideUp" }}
57
- >
58
- {expanded && (
59
- <S.TripDetailDescription>
60
- <S.HideButton onClick={this.onHideClick}>
61
- <TimesCircle size="0.92em" />
62
- </S.HideButton>
63
- {description}
64
- </S.TripDetailDescription>
65
- )}
66
- </VelocityTransitionGroup>
54
+ <AnimateHeight duration={300} height={expanded ? "auto" : 0}>
55
+ <S.TripDetailDescription>
56
+ <S.HideButton onClick={this.onHideClick}>
57
+ <TimesCircle size="0.92em" />
58
+ </S.HideButton>
59
+ {description}
60
+ </S.TripDetailDescription>
61
+ </AnimateHeight>
67
62
  </S.TripDetail>
68
63
  );
69
64
  }
package/src/types.ts CHANGED
@@ -9,6 +9,12 @@ export interface CaloriesDetailsProps {
9
9
  walkSeconds: number;
10
10
  }
11
11
 
12
+ export interface CO2ConfigType {
13
+ carbonIntensity?: { [index: string]: number };
14
+ units?: string;
15
+ enabled?: boolean;
16
+ }
17
+
12
18
  export interface DepartureDetailsProps {
13
19
  departureDate: Date;
14
20
  }
@@ -90,4 +96,8 @@ export interface TripDetailsProps {
90
96
  * Itinerary that the user has selected to view, contains multiple legs.
91
97
  */
92
98
  itinerary: Itinerary;
99
+ /**
100
+ * Object containing the CO2 config.
101
+ */
102
+ co2Config?: CO2ConfigType;
93
103
  }