import { useMemo, useState } from 'react'; import { __ } from '@wordpress/i18n'; import { Button, Card, CardBody, CardHeader, Flex, FlexItem, TextControl, } from '@wordpress/components'; import { chevronRight } from '@wordpress/icons'; import { formatAddressLine } from '../utils/address'; import { formatShortDate, getCourierLogo } from '../../../shared/utils'; import { Quote } from 'types/quote'; import { PluginSettings } from '../../../types'; import { Order } from '../../../types/order'; import { Dropshop } from '../../../types/dropshop'; import { getQuotesValidCollectionDates, isQuoteDropoff, metersToMiles, } from '../utils/quoteHelpers'; import DropshopModal from '../dropshop-modal'; interface ServiceSectionProps { quote: Quote; store: PluginSettings['shipping']; order: Order; collectionDate: string | null; quoteDropshops: Dropshop[]; } const ServiceSection = ({ quote, store, order, collectionDate, quoteDropshops, }: ServiceSectionProps) => { const [openDropshop, setOpenDropshop] = useState(false); const { service } = quote ?? {}; const logo = getCourierLogo(service?.courierSlug || service?.courier); const isDropoff = isQuoteDropoff(quote); const nearestShop = quoteDropshops?.[0]; const hasDropshops = isDropoff && quoteDropshops?.length > 0; const dropoffLabel = isDropoff && nearestShop ? `${__('Drop off today', 'parcel2go-shipping')}. (${metersToMiles(Number(nearestShop.distance) || 0)})` : __('Collection', 'parcel2go-shipping'); const estimatedDeliveryDate = useMemo(() => { if (!quote) return undefined; const dates = getQuotesValidCollectionDates(quote); if (!collectionDate) return dates[0]?.delivery?.dateMin; const date = dates.find((d) => d.collection.date === collectionDate); if (!date) return dates[0]?.delivery?.dateMin; return date.delivery.dateMin; }, [collectionDate, quote]); return ( {service.name}
{service.courier} {service.name}
{hasDropshops ? ( <> {openDropshop && ( setOpenDropshop(false) } /> )} ) : (
{dropoffLabel}
)}
{__('Estimated Delivery', 'parcel2go-shipping')}:{' '} {formatShortDate(estimatedDeliveryDate)}

{__( 'Shipping Details', 'parcel2go-shipping' ).toUpperCase()}

{__('From:', 'parcel2go-shipping')}
{store.property} {store.street} {store.town}, {store.county},{' '} {store.postcode} {store.countryName}
{__('To:', 'parcel2go-shipping')}
{formatAddressLine(order?.shipping)}
); }; export default ServiceSection;