Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | 1x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 2x 26x 26x 26x 26x 1x 1x 26x 6x 1x 1x 5x 5x 5x 4x 4x 1x 1x 26x 26x 26x 1x | import { useState } from 'react'
import { useNavigate, useParams } from 'react-router-dom'
import { useForm } from 'react-hook-form'
import { zodResolver } from '@hookform/resolvers/zod'
import { useTranslation } from 'react-i18next'
import { Card, CardHeader, CardTitle, CardContent } from '@core/components/ui/card'
import type { DonationFormValues } from '../schemas/donation.schema'
import { donationSchema } from '../schemas/donation.schema'
import { useAppConfig } from '@core/providers/AppConfigProvider'
import { api } from '@core/lib/api'
import { useCurrencyFormatter } from '@core/hooks/useCurrencyFormatter'
import { PaymentFormFactory } from './payment/PaymentFormFactory'
import { motion, AnimatePresence } from 'framer-motion'
import { CreditCard } from 'lucide-react'
import { DonationAmountSelector } from './DonationAmountSelector'
import { DonationContactForm } from './DonationContactForm'
export const CheckoutForm = () => {
const { t } = useTranslation('common')
const navigate = useNavigate()
const { slug } = useParams<{ slug: string }>()
const { config } = useAppConfig()
const [step, setStep] = useState<'details' | 'payment'>('details')
// Define type or import Response type. For now locally.
const [sessionData, setSessionData] = useState<{
id: string
clientSecret: string
} | null>(null)
const [submitError, setSubmitError] = useState<string | null>(null)
const [selectedAmount, setSelectedAmount] = useState<number>(20)
const { currency } = useCurrencyFormatter()
// Validate slug against config to ensure consistency
if (slug && config.slug && slug !== config.slug) {
console.warn(
`Slug mismatch: URL param '${slug}' does not match config slug '${config.slug}'. Using config slug.`,
)
}
const activeSlug = slug || config.slug
const {
register,
handleSubmit,
setValue,
watch,
getValues,
formState: { errors },
} = useForm<DonationFormValues>({
resolver: zodResolver(donationSchema),
defaultValues: {
amount: 20,
isAnonymous: false,
name: '',
email: '',
message: '',
},
})
// eslint-disable-next-line react-hooks/incompatible-library
const currentAmount = watch('amount')
const handleAmountSelect = (amount: number) => {
setSelectedAmount(amount)
setValue('amount', amount)
}
const onSubmitDetails = async (data: DonationFormValues) => {
if (!activeSlug) {
setSubmitError(t('donation.error_invalid_event'))
return
}
try {
const amountInCents = Math.round(data.amount * 100)
const { data: intentData } = await api.post('/donations/intent', {
amount: amountInCents,
currency: currency,
eventId: config.id, // Use ID for backend, but slug for navigation
metadata: {
donorName: data.name,
donorEmail: data.email,
message: data.message,
isAnonymous: data.isAnonymous ? 'true' : 'false',
},
})
setSessionData(intentData)
setStep('payment')
} catch (err) {
console.error(err)
setSubmitError(
t('donation.error_init', 'Failed to initialize donation. Please try again.'),
)
}
}
const panelClass = 'backdrop-blur-md border-t overflow-hidden mt-6'
const panelStyle = {
backgroundColor: 'var(--glass-bg)',
borderColor: 'var(--glass-border)',
backdropFilter: 'blur(var(--glass-blur))',
borderRadius: 'var(--donation-card-radius)',
boxShadow: 'var(--donation-card-shadow)',
}
return (
<AnimatePresence mode="wait">
{step === 'details' ? (
<motion.div
key="details"
initial={{ opacity: 0, x: -20 }}
animate={{ opacity: 1, x: 0 }}
exit={{ opacity: 0, x: -20 }}
transition={{ duration: 0.3 }}
>
<form onSubmit={handleSubmit(onSubmitDetails)} className="space-y-6">
<DonationAmountSelector
selectedAmount={selectedAmount}
onAmountSelect={handleAmountSelect}
register={register}
errors={errors}
setValue={setValue}
/>
<DonationContactForm
register={register}
errors={errors}
currentAmount={currentAmount}
submitError={submitError}
/>
</form>
</motion.div>
) : (
<motion.div
key="payment"
initial={{ opacity: 0, x: 20 }}
animate={{ opacity: 1, x: 0 }}
exit={{ opacity: 0, x: 20 }}
transition={{ duration: 0.3 }}
>
<Card className={panelClass} style={panelStyle}>
<CardHeader>
<CardTitle className="flex items-center gap-2">
<CreditCard className="h-6 w-6 text-primary" />
{t('donation.payment')}
</CardTitle>
</CardHeader>
<CardContent>
{sessionData && (
<PaymentFormFactory
providerId={config.donation.payment.provider}
sessionData={sessionData}
amount={currentAmount || 0}
currency={config.donation?.payment?.currency || 'usd'}
config={config.donation.payment.config}
onSuccess={() => {
navigate(`/${slug}/thank-you`, {
state: {
amount: currentAmount,
donorName: getValues('name'),
transactionId: sessionData.id,
},
})
}}
onBack={() => setStep('details')}
onError={(msg: string) => console.error(msg)}
/>
)}
</CardContent>
</Card>
</motion.div>
)}
</AnimatePresence>
)
}
|