import { useMemo, useState } from 'react' import { __ } from '@wordpress/i18n' import classNames from 'classnames' import { createFormFields } from '../../components/Form/utils' import { useForm } from '../../components/Form/FormProvider' import { IFormContext } from '../../components/Form/types' import { wbkFormatPrice } from '../../providers/BookingFormProvider/utils' interface GiftCardAmountStepProps { presetAmounts: number[] allowCustom: boolean priceFormat: string } export const GiftCardAmountStep = ({ presetAmounts, allowCustom, priceFormat, }: GiftCardAmountStepProps) => { const { fields, setFields, anyTouched } = useForm() as IFormContext const amountField = fields.find((field) => field.slug === 'amount') const amountValue = String(amountField?.value ?? '') const amountNumber = Number(amountValue.replace(',', '.')) || 0 const matchesPreset = presetAmounts.some( (presetAmount) => presetAmount === amountNumber ) const [isCustom, setIsCustom] = useState( allowCustom && amountValue !== '' && !matchesPreset ) const setAmount = (nextAmount: string, custom: boolean) => { setIsCustom(custom) setFields( fields.map((field) => field.slug === 'amount' ? { ...field, value: nextAmount } : field ) ) } const amountError = useMemo(() => { if (!anyTouched) { return '' } const amount = Number(amountValue.replace(',', '.')) if (!Number.isFinite(amount) || amount <= 0) { return __('Please enter a valid amount', 'webba-booking-lite') } return '' }, [anyTouched, amountValue]) return (

{__('Choose an amount', 'webba-booking-lite')}

{presetAmounts.map((presetAmount) => { const selected = !isCustom && amountNumber === presetAmount return ( ) })} {allowCustom && ( )}
{isCustom && (
{createFormFields( fields.filter((field) => field.slug === 'amount'), anyTouched )}
)} {!isCustom && amountError && (
{amountError}
)}
) }