"use strict";

require("core-js/modules/web.dom.iterable");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _auth0React = require("@auth0/auth0-react");

var _react = _interopRequireWildcard(require("react"));

var _reactBootstrap = require("react-bootstrap");

var _reactRedux = require("react-redux");

var _accountPage = _interopRequireDefault(require("../account-page"));

var _styled = require("../styled");

var uiActions = _interopRequireWildcard(require("../../../actions/ui"));

var userActions = _interopRequireWildcard(require("../../../actions/user"));

var _backToTripPlanner = _interopRequireDefault(require("../back-to-trip-planner"));

var _awaitingScreen = _interopRequireDefault(require("../awaiting-screen"));

var _tripSummaryPane = _interopRequireDefault(require("./trip-summary-pane"));

var _constants = require("../../../util/constants");

var _ui2 = require("../../../util/ui");

var _withLoggedInUserSupport = _interopRequireDefault(require("../with-logged-in-user-support"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

/**
 * This component displays the list of saved trips for the logged-in user.
 */
class SavedTripList extends _react.Component {
  render() {
    const {
      trips
    } = this.props;
    let content;

    if (!trips) {
      // Flash an indication while user trips are being loaded.
      content = /*#__PURE__*/_react.default.createElement(_awaitingScreen.default, null);
    } else if (trips.length === 0) {
      content = /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_backToTripPlanner.default, null), /*#__PURE__*/_react.default.createElement(_styled.PageHeading, null, "You have no saved trips"), /*#__PURE__*/_react.default.createElement("p", null, "Perform a trip search from the map first."));
    } else {
      // Stack the saved trip summaries and commands.
      content = /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_backToTripPlanner.default, null), /*#__PURE__*/_react.default.createElement(_styled.PageHeading, null, "My trips"), trips.map((trip, index) => /*#__PURE__*/_react.default.createElement(ConnectedTripListItem, {
        key: trip.id,
        trip: trip
      })));
    }

    return /*#__PURE__*/_react.default.createElement(_accountPage.default, null, content);
  }

} // connect to the redux store


const mapStateToProps = (state, ownProps) => {
  return {
    loggedInUser: state.user.loggedInUser,
    trips: state.user.loggedInUserMonitoredTrips
  };
};

const mapDispatchToProps = {};

var _default = (0, _withLoggedInUserSupport.default)((0, _auth0React.withAuthenticationRequired)((0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps)(SavedTripList), _ui2.RETURN_TO_CURRENT_ROUTE), true);
/**
 * This class manages events and rendering for one item in the saved trip list.
 */


exports.default = _default;

class TripListItem extends _react.Component {
  constructor(...args) {
    super(...args);

    _defineProperty(this, "_handleEditTrip", () => {
      const {
        routeTo,
        trip
      } = this.props;
      routeTo(`${_constants.TRIPS_PATH}/${trip.id}`);
    });

    _defineProperty(this, "_handleTogglePauseMonitoring", () => {
      const {
        togglePauseTrip,
        trip
      } = this.props;
      togglePauseTrip(trip);
    });

    _defineProperty(this, "_handleDeleteTrip", () => {
      const {
        confirmAndDeleteUserMonitoredTrip,
        trip
      } = this.props;
      confirmAndDeleteUserMonitoredTrip(trip.id);
    });
  }

  render() {
    const {
      trip
    } = this.props;
    const {
      itinerary
    } = trip;
    const {
      legs
    } = itinerary; // Assuming the monitored itinerary has at least one leg:
    // - get the 'from' location of the first leg,
    // - get the 'to' location of the last leg.

    const from = legs[0].from;
    const to = legs[legs.length - 1].to;
    return /*#__PURE__*/_react.default.createElement(_reactBootstrap.Panel, null, /*#__PURE__*/_react.default.createElement(_styled.TripPanelHeading, null, /*#__PURE__*/_react.default.createElement(_reactBootstrap.Panel.Title, null, /*#__PURE__*/_react.default.createElement(_styled.TripHeader, null, trip.tripName), /*#__PURE__*/_react.default.createElement("small", null, "From ", /*#__PURE__*/_react.default.createElement("b", null, from.name), " to ", /*#__PURE__*/_react.default.createElement("b", null, to.name)))), /*#__PURE__*/_react.default.createElement(_reactBootstrap.Panel.Body, null, /*#__PURE__*/_react.default.createElement(_tripSummaryPane.default, {
      monitoredTrip: trip
    })), /*#__PURE__*/_react.default.createElement(_styled.TripPanelFooter, null, /*#__PURE__*/_react.default.createElement(_reactBootstrap.Button, {
      onClick: this._handleTogglePauseMonitoring
    }, trip.isActive ? /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_reactBootstrap.Glyphicon, {
      glyph: "pause"
    }), " Pause") : /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_reactBootstrap.Glyphicon, {
      glyph: "play"
    }), " Resume")), /*#__PURE__*/_react.default.createElement(_reactBootstrap.Button, {
      onClick: this._handleEditTrip
    }, /*#__PURE__*/_react.default.createElement(_reactBootstrap.Glyphicon, {
      glyph: "pencil"
    }), " Edit"), /*#__PURE__*/_react.default.createElement(_reactBootstrap.Button, {
      onClick: this._handleDeleteTrip
    }, /*#__PURE__*/_react.default.createElement(_reactBootstrap.Glyphicon, {
      glyph: "trash"
    }), " Delete")));
  }

} // connect to the redux store


const itemMapStateToProps = () => ({});

const itemMapDispatchToProps = {
  confirmAndDeleteUserMonitoredTrip: userActions.confirmAndDeleteUserMonitoredTrip,
  routeTo: uiActions.routeTo,
  togglePauseTrip: userActions.togglePauseTrip
};
const ConnectedTripListItem = (0, _reactRedux.connect)(itemMapStateToProps, itemMapDispatchToProps)(TripListItem);
module.exports = exports.default;

//# sourceMappingURL=saved-trip-list.js