import { useCallback, useState } from 'react' import classNames from 'classnames' import { __ } from '@wordpress/i18n' import { ReactComponent as CheckIcon } from '../../../../public/images/icon-checkmark.svg' import arrowDownIcon from '../../../../public/images/arrow-down.svg' import { useBookingContext } from '../../providers/BookingFormProvider/BookingFormProvider' import { wbkFormatPrice } from '../../providers/BookingFormProvider/utils' import { Button } from '../Button/Button' import { useWording } from '../../hooks/useWording' import { getPlainTextFromHtml } from '../../lib/utils' import { IExtraProps } from './types' import cartReduceIcon from '../../../../public/images/icon-cart-reduce.svg' import cartIncreaseIcon from '../../../../public/images/icon-cart-increase.svg' import '../Services/Services.scss' import './Extras.scss' const DESCRIPTION_TRUNCATE_LENGTH = 190 export const ExtraItem = ({ id, label, description, image, price, min_quantity, max_quantity, selected, quantity, onUpdate, }: IExtraProps) => { const wording = useWording() const { priceFormat } = useBookingContext() const [descriptionExpanded, setDescriptionExpanded] = useState(false) const descriptionPlainText = description ? getPlainTextFromHtml(description) : '' const shouldTruncate = descriptionPlainText.length > DESCRIPTION_TRUNCATE_LENGTH const minQ = Math.max(1, Number(min_quantity) || 1) const maxQ = Math.max(minQ, Number(max_quantity) || 1) const hasQuantityRange = maxQ > minQ const currentQuantity = Math.min( maxQ, Math.max(minQ, Number(quantity) || minQ) ) const setQuantity = useCallback( (updatedQuantity: number) => { const num = Math.min(maxQ, Math.max(minQ, updatedQuantity)) onUpdate({ quantity: num }) }, [minQ, maxQ, onUpdate] ) const toggleSelectExtra = useCallback(() => { onUpdate({ selected: !selected, }) }, [selected, onUpdate]) return (
event.stopPropagation()} > onUpdate({ selected: !selected, }) } aria-label={ selected ? wording.selected || __('Selected', 'webba-booking-lite') : wording.select || __('+ Select', 'webba-booking-lite') } />
{image && ( {label} )}

{label}

{description && (

)} {description && shouldTruncate && (

{ e.preventDefault() e.stopPropagation() setDescriptionExpanded(!descriptionExpanded) }} className={classNames( 'wbk_service_item__description-toggle', { ['wbk_service_item__description-toggle--expanded']: descriptionExpanded, } )} > {(descriptionExpanded && (wording.show_less || __('Show less', 'webba-booking-lite'))) || wording.show_more || __('Show more', 'webba-booking-lite')} {
)}
{(price && Number(price) > 0 && ( {wbkFormatPrice(Number(price), priceFormat)} )) || wording.free || __('Free', 'webba-booking-lite')}
{selected && hasQuantityRange && (
{ event.preventDefault() event.stopPropagation() }} >
setQuantity(currentQuantity - 1) } onKeyDown={(event) => { if ( event.key === 'Enter' || event.key === ' ' ) { event.preventDefault() setQuantity(currentQuantity - 1) } }} > {
setQuantity(currentQuantity + 1) } onKeyDown={(event) => { if ( event.key === 'Enter' || event.key === ' ' ) { event.preventDefault() setQuantity(currentQuantity + 1) } }} > {
)}
) }