import React, { useState } from 'react'; import Button from '../widgets/Button'; import { useAppStateContext } from '../../context/user.data.context'; import { redeemDiscountCode } from '../../service/discount/discount.service'; /** * Voucher code input for the pricing page. Redeems an EXTEND_TRIAL_DAYS code, * shows how many trial days were granted and refreshes the user so the trial * countdown updates. */ const VoucherRedeem = (): React.ReactElement => { const { fetchUser } = useAppStateContext(); const [code, setCode] = useState(''); const [loading, setLoading] = useState(false); const [success, setSuccess] = useState(null); const [error, setError] = useState(null); const handleRedeem = async (): Promise => { if (!code.trim()) return; setLoading(true); setError(null); setSuccess(null); try { const res: any = await redeemDiscountCode(code.trim()); if (res?.success && res?.trial_days_granted !== undefined) { const granted: number = res.trial_days_granted ?? 0; const remaining: number = res.remaining_trial_days ?? 0; setSuccess( `You've got +${granted} trial ${ granted === 1 ? 'day' : 'days' }! You now have ${remaining} ${ remaining === 1 ? 'day' : 'days' } of trial left.` ); setCode(''); await fetchUser(); } else { setError( res?.error || res?.errors || 'This code could not be redeemed.' ); } } catch { setError('This code could not be redeemed.'); } finally { setLoading(false); } }; return (

Voucher

Have a voucher? Enter it below.

setCode(e.target.value.toUpperCase())} onKeyDown={e => { if (e.key === 'Enter') handleRedeem(); }} placeholder="ENTER VOUCHER" className="flex-1 rounded-md border border-gray-300 bg-white px-3 py-2 font-mono text-sm uppercase text-gray-900 placeholder:text-gray-400 focus:border-gray-400 focus:outline-none focus:ring-1 focus:ring-gray-200" />
{success && (
{success}
)} {error && (
{error}
)}
); }; export default VoucherRedeem;