import React from 'react'; import { FormattedMessage } from 'react-intl'; import classNames from 'classnames'; import { txHasBeenAccepted, txHasFirstReview, txIsAccepted, txIsCanceled, txIsCompleted, txIsDeclined, txIsEnquired, txIsExpired, txIsRequested, txIsReviewed, } from '../../util/types'; import { userDisplayName } from '../../util/data'; import { createSlug, stringify } from '../../util/urlHelpers'; import { ActivityFeed, BookingBreakdown, BookingPanel, ExternalLink, NamedLink, PrimaryButton, SecondaryButton, } from '../../components'; import config from '../../config'; import css from './TransactionPanel.css'; // Functional component as a helper to build ActivityFeed section export const FeedSection = props => { const { className, rootClassName, currentTransaction, currentUser, fetchMessagesError, fetchMessagesInProgress, initialMessageFailed, messages, oldestMessagePageFetched, onShowMoreMessages, onOpenReviewModal, totalMessagePages, } = props; const txTransitions = currentTransaction.attributes.transitions ? currentTransaction.attributes.transitions : []; const hasOlderMessages = totalMessagePages > oldestMessagePageFetched; const showFeed = messages.length > 0 || txTransitions.length > 0 || initialMessageFailed || fetchMessagesError; const classes = classNames(rootClassName || css.feedContainer, className); return showFeed ? (

{initialMessageFailed ? (

) : null} {fetchMessagesError ? (

) : null} { onShowMoreMessages(currentTransaction.id); }} fetchMessagesInProgress={fetchMessagesInProgress} />
) : null; }; // Functional component as a helper to build AddressLinkMaybe export const AddressLinkMaybe = props => { const { transaction, transactionRole, currentListing } = props; const isCustomer = transactionRole === 'customer'; const txIsAcceptedForCustomer = isCustomer && txHasBeenAccepted(transaction); const { address, building } = currentListing.attributes.publicData.location || {}; const geolocation = currentListing.attributes.geolocation; const { lat, lng } = geolocation || {}; const hrefToGoogleMaps = geolocation ? `https://maps.google.com/?q=${lat},${lng}` : address ? `https://maps.google.com/?q=${encodeURIComponent(address)}` : null; const fullAddress = typeof building === 'string' && building.length > 0 ? `${building}, ${address}` : address; return txIsAcceptedForCustomer && hrefToGoogleMaps ? (

{fullAddress}

) : null; }; // Functional component as a helper to build BookingBreakdown export const BreakdownMaybe = props => { const { className, rootClassName, transaction, transactionRole } = props; const loaded = transaction && transaction.id && transaction.booking && transaction.booking.id; const classes = classNames(rootClassName || css.breakdown, className); return loaded ? (

) : null; }; const createListingLink = (listing, label, searchParams = {}, className = '') => { const listingLoaded = !!listing.id; if (listingLoaded && !listing.attributes.deleted) { const title = listing.attributes.title; const params = { id: listing.id.uuid, slug: createSlug(title) }; const to = { search: stringify(searchParams) }; return ( {label} ); } else { return ; } }; // Functional component as a helper to build detail card headings export const DetailCardHeadingsMaybe = props => { const { transaction, transactionRole, listing, listingTitle, subTitle } = props; const isCustomer = transactionRole === 'customer'; const canShowDetailCardHeadings = isCustomer && !txIsEnquired(transaction); return canShowDetailCardHeadings ? (

{listingTitle}

{subTitle}

) : null; }; // Functional component as a helper to build a BookingPanel export const BookingPanelMaybe = props => { const { authorDisplayName, transaction, transactionRole, listing, listingTitle, subTitle, provider, onSubmit, onManageDisableScrolling, timeSlots, fetchTimeSlotsError, } = props; const isProviderLoaded = !!provider.id; const isProviderBanned = isProviderLoaded && provider.attributes.banned; const isCustomer = transactionRole === 'customer'; const canShowBookingPanel = isCustomer && txIsEnquired(transaction) && !isProviderBanned; return canShowBookingPanel ? ( console.log('submit')} title={listingTitle} subTitle={subTitle} authorDisplayName={authorDisplayName} onSubmit={onSubmit} onManageDisableScrolling={onManageDisableScrolling} timeSlots={timeSlots} fetchTimeSlotsError={fetchTimeSlotsError} /> ) : null; }; // Functional component as a helper to build ActionButtons for // provider when state is preauthorized export const SaleActionButtonsMaybe = props => { const { className, rootClassName, canShowButtons, transaction, acceptInProgress, declineInProgress, acceptSaleError, declineSaleError, onAcceptSale, onDeclineSale, } = props; const buttonsDisabled = acceptInProgress || declineInProgress; const acceptErrorMessage = acceptSaleError ? (

) : null; const declineErrorMessage = declineSaleError ? (

) : null; const classes = classNames(rootClassName || css.actionButtons, className); return canShowButtons ? (
{acceptErrorMessage} {declineErrorMessage}
onDeclineSale(transaction.id)} > onAcceptSale(transaction.id)} >
) : null; }; // Functional component as a helper to build order title export const OrderTitle = props => { const { className, rootClassName, transaction, customerDisplayName: customerName, currentListing, listingTitle, } = props; const listingLink = createListingLink(currentListing, listingTitle); const classes = classNames(rootClassName || css.headingOrder, className); if (txIsEnquired(transaction)) { return (

); } else if (txIsRequested(transaction)) { return (

); } else if (txIsAccepted(transaction)) { return (

); } else if (txIsDeclined(transaction)) { return (

); } else if (txIsExpired(transaction)) { return (

); } else if (txIsCanceled(transaction)) { return (

); } else if ( txIsCompleted(transaction) || txHasFirstReview(transaction) || txIsReviewed(transaction) ) { return (

); } else { return null; } }; // Functional component as a helper to build order message below title export const OrderMessage = props => { const { className, rootClassName, transaction, authorDisplayName: providerName, listingDeleted, } = props; const classes = classNames(rootClassName || css.transactionInfoMessage, className); if (!listingDeleted && txIsRequested(transaction)) { return (

); } else if (listingDeleted) { return (

); } return null; }; // Functional component as a helper to build sale title export const SaleTitle = props => { const { className, rootClassName, transaction, customerDisplayName: customerName, currentListing, listingTitle, } = props; const listingLink = createListingLink(currentListing, listingTitle); const classes = classNames(rootClassName || css.headingSale, className); if (txIsEnquired(transaction)) { return (

); } else if (txIsRequested(transaction)) { return (

); } else if (txIsAccepted(transaction)) { return (

); } else if (txIsDeclined(transaction)) { return (

); } else if (txIsExpired(transaction)) { return (

); } else if (txIsCanceled(transaction)) { return (

); } else if ( txIsCompleted(transaction) || txHasFirstReview(transaction) || txIsReviewed(transaction) ) { return (

); } else { return null; } }; // Functional component as a helper to build sale message below title export const SaleMessage = props => { const { className, rootClassName, transaction, customerDisplayName: customerName, isCustomerBanned, } = props; const classes = classNames(rootClassName || css.transactionInfoMessage, className); if (!isCustomerBanned && txIsRequested(transaction)) { return (

); } else if (isCustomerBanned) { return (

); } return null; }; // Functional component as a helper to choose and show Order or Sale title export const TransactionPageTitle = props => { return props.transactionRole === 'customer' ? ( ) : ( ); }; // Functional component as a helper to choose and show Order or Sale message export const TransactionPageMessage = props => { return props.transactionRole === 'customer' ? ( ) : ( ); }; // Helper function to get display names for different roles export const displayNames = ( currentUser, currentProvider, currentCustomer, bannedUserDisplayName ) => { const authorDisplayName = userDisplayName(currentProvider, bannedUserDisplayName); const customerDisplayName = userDisplayName(currentCustomer, bannedUserDisplayName); let otherUserDisplayName = ''; const currentUserIsCustomer = currentUser.id && currentCustomer.id && currentUser.id.uuid === currentCustomer.id.uuid; const currentUserIsProvider = currentUser.id && currentProvider.id && currentUser.id.uuid === currentProvider.id.uuid; if (currentUserIsCustomer) { otherUserDisplayName = authorDisplayName; } else if (currentUserIsProvider) { otherUserDisplayName = customerDisplayName; } return { authorDisplayName, customerDisplayName, otherUserDisplayName, }; };