@opentripplanner/trip-details 2.0.0 → 2.0.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.
@@ -1,7 +1,11 @@
1
1
  import flatten from "flat";
2
2
  import React, { ReactElement } from "react";
3
- import { IntlProvider } from "react-intl";
4
- import { Meta, Story as StoryType } from "@storybook/react";
3
+ import {
4
+ ComponentMeta,
5
+ ComponentStory,
6
+ Parameters,
7
+ StoryContext
8
+ } from "@storybook/react";
5
9
  import styled from "styled-components";
6
10
  // The below eslint-disable is due to https://github.com/storybookjs/storybook/issues/13408
7
11
  // eslint-disable-next-line import/no-named-as-default
@@ -14,19 +18,8 @@ import {
14
18
  TripDetailsProps
15
19
  } from "./types";
16
20
 
17
- import englishMessages from "../i18n/en-US.yml";
18
- import frenchMessages from "../i18n/fr.yml";
19
21
  import customMessages from "../__mocks__/custom-messages.yml";
20
22
 
21
- /**
22
- * Describes args passed to stories.
23
- */
24
- interface StoryArgs {
25
- locale?: string;
26
- useCustomMessages?: boolean;
27
- useLocalizedMessages?: boolean;
28
- }
29
-
30
23
  // import mock itinaries. These are all trip plan outputs from OTP.
31
24
  const bikeOnlyItinerary = require("@opentripplanner/itinerary-body/src/__mocks__/itineraries/bike-only.json");
32
25
  const bikeRentalItinerary = require("@opentripplanner/itinerary-body/src/__mocks__/itineraries/bike-rental.json");
@@ -41,6 +34,7 @@ const walkOnlyItinerary = require("@opentripplanner/itinerary-body/src/__mocks__
41
34
  const walkTransitWalkItinerary = require("@opentripplanner/itinerary-body/src/__mocks__/itineraries/walk-transit-walk.json");
42
35
  const walkTransitWalkTransitWalkItinerary = require("@opentripplanner/itinerary-body/src/__mocks__/itineraries/walk-transit-walk-transit-walk.json");
43
36
  const fareComponentsItinerary = require("@opentripplanner/itinerary-body/src/__mocks__/itineraries/fare-components.json");
37
+ const otp2ScooterItinerary = require("@opentripplanner/itinerary-body/src/__mocks__/itineraries/otp2-scooter.json");
44
38
 
45
39
  // Change currency code on one of the itineraries for illustration.
46
40
  // (other currency fields in the itinerary object are not used for display).
@@ -122,17 +116,20 @@ const CustomCaloriesDetails = ({
122
116
  * @param Component The component to render.
123
117
  */
124
118
  function createTripDetailsTemplate(
125
- Component: React.ElementType<TripDetailsProps> = TripDetails
126
- ) {
127
- const TripDetailsTemplate = ({
128
- CaloriesDetails,
129
- defaultFare,
130
- DepartureDetails,
131
- FareDetails,
132
- itinerary,
133
- locale,
134
- useCustomFareKeyMap
135
- }: TripDetailsProps): ReactElement => {
119
+ Component: typeof TripDetails = TripDetails
120
+ ): ComponentStory<typeof TripDetails> {
121
+ const TripDetailsTemplate = (
122
+ {
123
+ CaloriesDetails,
124
+ defaultFare,
125
+ DepartureDetails,
126
+ FareDetails,
127
+ itinerary
128
+ }: TripDetailsProps,
129
+ { globals, parameters }: StoryContext
130
+ ): ReactElement => {
131
+ const { locale } = globals;
132
+ const { useCustomFareKeyMap } = parameters;
136
133
  const fareKeyNameMap = useCustomFareKeyMap
137
134
  ? locale === "en-US"
138
135
  ? englishFareKeyMap
@@ -152,93 +149,35 @@ function createTripDetailsTemplate(
152
149
  return TripDetailsTemplate;
153
150
  }
154
151
 
155
- const intlDecorator = (
156
- Story: StoryType,
157
- context: {
158
- args: StoryArgs;
159
- }
160
- ): ReactElement => {
161
- const { args } = context;
162
- const { locale, useCustomMessages, useLocalizedMessages } = args;
163
- const messages = locale === "en-US" ? englishMessages : frenchMessages;
164
-
165
- // Construct a messages object that customizes a subset
166
- // of the default messages of the desired locale.
167
- // The structure of the message objects is
168
- // flattened before it is passed to IntlProvider.
169
- // Note: the spread operator (...) is rejected if used with flatten,
170
- // so we use Object.assign instead
171
- // (see e.g. https://github.com/Microsoft/TypeScript/issues/10727#issuecomment-423712256).
172
- let mergedMessages: Record<string, string> = flatten(messages);
173
- if (useCustomMessages) {
174
- mergedMessages = Object.assign(mergedMessages, flatten(customMessages));
175
- }
176
- return (
177
- <IntlProvider
178
- locale={locale}
179
- messages={useLocalizedMessages ? mergedMessages : null}
180
- >
181
- <Story />
182
- </IntlProvider>
183
- );
184
- };
185
-
186
152
  /**
187
153
  * Helper to simplify story declaration.
188
154
  */
189
155
  function makeStory(
190
- args: StoryArgs | TripDetailsProps,
191
- Component?: React.ElementType<TripDetailsProps>
192
- ) {
156
+ args: TripDetailsProps,
157
+ parameters: Parameters,
158
+ Component?: typeof TripDetails
159
+ ): ComponentStory<typeof TripDetails> {
193
160
  const BoundTripDetails = createTripDetailsTemplate(Component).bind({});
194
161
  BoundTripDetails.args = args;
162
+ BoundTripDetails.parameters = parameters;
195
163
  return BoundTripDetails;
196
164
  }
197
165
 
198
- const decoratorPropDescription = "(This prop is used by the decorator.)";
199
- // Hide story controls for some props.
200
- const noControl = {
201
- control: { type: false }
202
- };
203
- // Hide some story args completely.
204
- const hiddenProp = {
205
- table: { disable: true }
206
- };
207
-
208
166
  export default {
209
- argTypes: {
210
- CaloriesDetails: noControl,
211
- className: noControl,
212
- Component: hiddenProp,
213
- defaultFareKey: noControl,
214
- DepartureDetails: noControl,
215
- FareDetails: noControl,
216
- fareKeyNameMap: noControl,
217
- itinerary: noControl,
218
- locale: {
219
- control: "radio",
220
- description: decoratorPropDescription,
221
- options: ["en-US", "fr"]
222
- },
223
- useCustomFareKeyMap: hiddenProp,
224
- useCustomMessages: {
225
- description: decoratorPropDescription
226
- },
227
- useLocalizedMessages: {
228
- description: decoratorPropDescription
229
- }
230
- },
231
167
  args: {
232
- defaultFareKey: "regular",
233
- locale: "en-US",
234
- useCustomMessages: false,
235
- useLocalizedMessages: true
168
+ defaultFareKey: "regular"
236
169
  },
237
170
  component: TripDetails,
238
- decorators: [intlDecorator],
239
- parameters: { controls: { sort: "alpha" } },
171
+ parameters: {
172
+ // Hide all controls
173
+ // (there are no args that the user can interactively change for this component).
174
+ controls: {
175
+ hideNoControlsWarning: true,
176
+ include: []
177
+ }
178
+ },
240
179
  title: "TripDetails"
241
- } as Meta;
180
+ } as ComponentMeta<typeof TripDetails>;
242
181
 
243
182
  export const WalkOnlyItinerary = makeStory({
244
183
  itinerary: walkOnlyItinerary
@@ -256,6 +195,7 @@ export const StyledWalkTransitWalkItinerary = makeStory(
256
195
  {
257
196
  itinerary: walkTransitWalkItinerary
258
197
  },
198
+ null,
259
199
  StyledTripDetails
260
200
  );
261
201
 
@@ -263,11 +203,15 @@ export const BikeTransitBikeItinerary = makeStory({
263
203
  itinerary: bikeTransitBikeItinerary
264
204
  });
265
205
 
266
- export const WalkInterlinedTransitItinerary = makeStory({
267
- defaultFareKey: "electronicRegular",
268
- itinerary: walkInterlinedTransitItinerary,
269
- useCustomFareKeyMap: true
270
- });
206
+ export const WalkInterlinedTransitItinerary = makeStory(
207
+ {
208
+ defaultFareKey: "electronicRegular",
209
+ itinerary: walkInterlinedTransitItinerary
210
+ },
211
+ {
212
+ useCustomFareKeyMap: true
213
+ }
214
+ );
271
215
 
272
216
  export const WalkTransitTransferItinerary = makeStory({
273
217
  itinerary: walkTransitWalkTransitWalkItinerary
@@ -293,18 +237,39 @@ export const EScooterRentalTransitItinerary = makeStory({
293
237
  itinerary: eScooterRentalTransiteScooterRentalItinerary
294
238
  });
295
239
 
296
- export const TncTransitItinerary = makeStory({
297
- itinerary: tncTransitTncItinerary,
298
- useCustomFareKeyMap: true
240
+ // The render of this itinerary is uninteresting, but the test
241
+ // is if we can parse an OTP2 itinerary without crashing
242
+ export const OTP2EScooterRentalTransitItinerary = makeStory({
243
+ itinerary: otp2ScooterItinerary
299
244
  });
300
245
 
301
- export const TncTransitItineraryWithCustomDetails = makeStory(
246
+ export const TncTransitItinerary = makeStory(
247
+ {
248
+ itinerary: tncTransitTncItinerary
249
+ },
250
+ {
251
+ useCustomFareKeyMap: true
252
+ }
253
+ );
254
+
255
+ const flattenedMessages = flatten(customMessages);
256
+ export const TncTransitItineraryWithCustomMessagesAndDetails = makeStory(
302
257
  {
303
258
  CaloriesDetails: CustomCaloriesDetails,
304
259
  defaultFareKey: "electronicRegular",
305
260
  DepartureDetails: CustomDepartureDetails,
306
261
  FareDetails: CustomFareDetails,
307
- itinerary: tncTransitTncItinerary,
262
+ itinerary: tncTransitTncItinerary
263
+ },
264
+ {
265
+ // For illustration purposes,
266
+ // override a subset of localized strings with custom messages.
267
+ reactIntl: {
268
+ messages: {
269
+ "en-US": flattenedMessages,
270
+ fr: flattenedMessages
271
+ }
272
+ },
308
273
  useCustomFareKeyMap: true
309
274
  },
310
275
  StyledTripDetails
package/src/index.tsx CHANGED
@@ -1,5 +1,4 @@
1
1
  import flatten from "flat";
2
- // @ts-expect-error FIXME: Create TypeScript types for core-utils packages.
3
2
  import coreUtils from "@opentripplanner/core-utils";
4
3
  import { FlexBookingInfo } from "@opentripplanner/types";
5
4
  import React, { ReactElement } from "react";
@@ -131,8 +130,8 @@ const TransitFare = ({
131
130
  name: fareKeyNameMap[fareKey] || fareNameFallback || fareKey,
132
131
  strong: boldText,
133
132
  value: renderFare(
134
- currentFare.currencyCode,
135
- currentFare.transitFare / 100
133
+ currentFare.currency.currencyCode,
134
+ currentFare.cents / 100
136
135
  )
137
136
  }}
138
137
  />
@@ -153,13 +152,9 @@ export function TripDetails({
153
152
  itinerary
154
153
  }: TripDetailsProps): ReactElement {
155
154
  // 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
- }
155
+ const fareResult = coreUtils.itinerary.calculateTncFares(itinerary);
156
+ const { maxTNCFare, minTNCFare, tncCurrencyCode } = fareResult;
157
+ const transitFares = itinerary?.fare?.fare;
163
158
 
164
159
  let companies = "";
165
160
  itinerary.legs.forEach(leg => {
@@ -172,6 +167,11 @@ export function TripDetails({
172
167
  const fareKeys = transitFares && Object.keys(transitFares).sort();
173
168
 
174
169
  if (transitFares && fareKeys.length > 0) {
170
+ let defaultFare = defaultFareKey;
171
+ if (!transitFares[defaultFareKey]) {
172
+ defaultFare = "regular";
173
+ }
174
+
175
175
  // Depending on if there are additional fares to display either render a <span> or a <details>
176
176
  const TransitFareWrapper =
177
177
  transitFares && fareKeys.length > 1 ? S.TransitFare : S.TransitFareSingle;
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, Money } from "@opentripplanner/types";
4
4
  import type { ReactElement } from "react";
5
5
 
6
6
  export interface CaloriesDetailsProps {
@@ -14,10 +14,7 @@ export interface DepartureDetailsProps {
14
14
  }
15
15
 
16
16
  export interface TransitFareData {
17
- [key: string]: {
18
- currencyCode: string;
19
- transitFare: number;
20
- }
17
+ [key: string]: Money
21
18
  }
22
19
 
23
20
  export interface FareDetailsProps {