Advertisement
Guest User

Untitled

a guest
Oct 13th, 2019
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.32 KB | None | 0 0
  1. // @flow
  2.  
  3. import * as React from 'react';
  4. import { createFragmentContainer, graphql } from 'react-relay';
  5. import { ThemeContext } from 'styled-components';
  6. import { withRouter } from 'react-router-dom';
  7. import {
  8. Ticket,
  9. Airplane,
  10. InformationCircle,
  11. City,
  12. } from '@kiwicom/orbit-components/lib/icons';
  13. import CarrierLogo from '@kiwicom/orbit-components/lib/CarrierLogo';
  14. import Translate from '@kiwicom/nitro/lib/components/Translate';
  15. import TextLink from '@kiwicom/orbit-components/lib/TextLink';
  16. import List from '@kiwicom/orbit-components/lib/List';
  17. import ListItem from '@kiwicom/orbit-components/lib/List/ListItem';
  18. import type { RouterHistory } from 'react-router-dom';
  19.  
  20. import PageVariantContext from '../../../services/contexts/PageVariant';
  21. import type { AccordionLegCitiesInfo_leg as AccordionLegCitiesInfoType } from './__generated__/AccordionLegCitiesInfo_leg.graphql';
  22. import bookingLegTypes from '../../../utils/enums/BookingLegTypes';
  23.  
  24. type Props = {|
  25. leg: AccordionLegCitiesInfoType,
  26. history: RouterHistory,
  27. |};
  28.  
  29. // FIXME: split this bloated component into smaller components
  30. const LegCitiesInfo = (props: Props) => {
  31. const { urlPrefix } = React.useContext(PageVariantContext);
  32. const theme = React.useContext(ThemeContext);
  33. const onClickShowMore = e => {
  34. e.preventDefault();
  35. props.history.push(`${urlPrefix}/search/article/127`);
  36. };
  37.  
  38. const { leg } = props;
  39. const flightNumber = leg.flightNumber ?? '';
  40. const transportationMode = leg.type;
  41. const reservationNumber = leg.pnr ?? '';
  42.  
  43. const carrier = {
  44. code: leg?.airline?.code ?? '',
  45. name: leg?.airline?.name ?? '',
  46. };
  47.  
  48. const carrierName =
  49. transportationMode === bookingLegTypes.AIRCRAFT ? (
  50. <Translate
  51. t="smartfaq.single_booking_page.accordion_cities_info.airline_title_name"
  52. values={{ carrierName: carrier.name }}
  53. />
  54. ) : (
  55. <Translate
  56. t="smartfaq.single_booking_page.accordion_cities_info.carrier_title_name"
  57. values={{ carrierName: carrier.name }}
  58. />
  59. );
  60.  
  61. const operatingAirline = {
  62. code: leg?.operatingAirline?.iata ?? '',
  63. name: leg?.operatingAirline?.name ?? '',
  64. };
  65.  
  66. const vehicle = {
  67. manufacturer: leg.vehicle?.manufacturer ?? '',
  68. model: leg.vehicle?.model ?? '',
  69. };
  70.  
  71. const departureStationName = leg?.departure?.airport?.name ?? '';
  72. const arrivalStationName = leg?.arrival?.airport?.name ?? '';
  73.  
  74. const isOperatingAirlineDisplayed =
  75. transportationMode === bookingLegTypes.AIRCRAFT &&
  76. operatingAirline.code &&
  77. operatingAirline.code !== carrier.code;
  78.  
  79. const isReservationNumberDisplayed =
  80. transportationMode === bookingLegTypes.AIRCRAFT && reservationNumber;
  81.  
  82. const isAircraftTypeDisplayed =
  83. transportationMode === bookingLegTypes.AIRCRAFT && vehicle.manufacturer;
  84.  
  85. const isGroundTransportation =
  86. transportationMode !== bookingLegTypes.AIRCRAFT;
  87.  
  88. const renderFlightNumber = () => {
  89. if (transportationMode === bookingLegTypes.BUS) return null;
  90.  
  91. let titleNumber = null;
  92.  
  93. if (transportationMode === bookingLegTypes.AIRCRAFT) {
  94. titleNumber = (
  95. <Translate
  96. t="smartfaq.single_booking_page.accordion_cities_info.flight_no"
  97. values={{
  98. flight_number: flightNumber,
  99. carrier_code: carrier.code,
  100. }}
  101. />
  102. );
  103. } else if (transportationMode === bookingLegTypes.TRAIN) {
  104. titleNumber = (
  105. <Translate
  106. t="smartfaq.single_booking_page.accordion_cities_info.train_no"
  107. values={{
  108. flight_number: flightNumber,
  109. carrier_code: carrier.code,
  110. }}
  111. />
  112. );
  113. }
  114.  
  115. return <ListItem icon={<InformationCircle />}>{titleNumber}</ListItem>;
  116. };
  117.  
  118. return (
  119. <List size="small" type="secondary" dataTest="legCities">
  120. <>
  121. <ListItem icon={<CarrierLogo carriers={[carrier]} />}>
  122. {carrierName}
  123. </ListItem>
  124. {isOperatingAirlineDisplayed && (
  125. <ListItem icon={<CarrierLogo carriers={[operatingAirline]} />}>
  126. <Translate
  127. t="smartfaq.single_booking_page.accordion_cities_info.operating_airline"
  128. values={{
  129. 'operatingAirline.name': operatingAirline.name,
  130. }}
  131. />
  132. </ListItem>
  133. )}
  134. {renderFlightNumber()}
  135. {isReservationNumberDisplayed && (
  136. <ListItem icon={<Ticket size="small" color="secondary" />}>
  137. <Translate
  138. t="smartfaq.single_booking_page.accordion_cities_info.reservation_number"
  139. values={{ reservationNumber }}
  140. />
  141. </ListItem>
  142. )}
  143. {isAircraftTypeDisplayed && (
  144. <ListItem icon={<Airplane size="small" color="secondary" />}>
  145. <Translate
  146. t="smartfaq.single_booking_page.accordion_cities_info.vehicle_type"
  147. values={{
  148. vehicle_manufacturer: vehicle.manufacturer,
  149. vehicle_model: vehicle.model,
  150. }}
  151. />
  152. </ListItem>
  153. )}
  154. {isGroundTransportation && (
  155. <>
  156. <ListItem icon={<City size="small" color="secondary" />}>
  157. {departureStationName}
  158. </ListItem>
  159. <ListItem icon={<City size="small" color="secondary" />}>
  160. {arrivalStationName}
  161. </ListItem>
  162. <ListItem
  163. icon={
  164. <InformationCircle
  165. size="small"
  166. customColor={theme.orbit.paletteProductNormal}
  167. />
  168. }
  169. dataTest="moreInfoLink"
  170. >
  171. <TextLink
  172. href=""
  173. dataTest="moreInfoLink"
  174. onClick={onClickShowMore}
  175. >
  176. <Translate t="smartfaq.single_booking_page.accordion_cities_info.trains_buses_info" />
  177. </TextLink>
  178. </ListItem>
  179. </>
  180. )}
  181. </>
  182. </List>
  183. );
  184. };
  185.  
  186. export default createFragmentContainer(
  187. withRouter(LegCitiesInfo),
  188. graphql`
  189. fragment AccordionLegCitiesInfo_leg on Leg {
  190. type
  191. airline {
  192. code
  193. name
  194. }
  195. operatingAirline {
  196. iata
  197. name
  198. }
  199. flightNumber
  200. vehicle {
  201. manufacturer
  202. model
  203. }
  204. pnr
  205. departure {
  206. airport {
  207. name
  208. }
  209. }
  210. arrival {
  211. airport {
  212. name
  213. }
  214. }
  215. }
  216. `,
  217. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement