import { Input, InputError, VegaIcon, VegaIconNames, } from '@vegaprotocol/ui-toolkit'; import type { FieldValues } from 'react-hook-form'; import { useForm } from 'react-hook-form'; import classNames from 'classnames'; import { useSearchParams } from 'react-router-dom'; import { useEffect, useState } from 'react'; import { Button } from './buttons'; import { useVegaWallet } from '@vegaprotocol/wallet'; export const ApplyCodeForm = () => { const [finalized, setFinalized] = useState(false); const { isReadOnly, pubKey, sendTx } = useVegaWallet(); const { register, handleSubmit, formState: { errors }, setValue, setError, } = useForm(); const [params] = useSearchParams(); useEffect(() => { const code = params.get('code'); if (code) setValue('code', code); }, [params, setValue]); const onSubmit = ({ code }: FieldValues) => { if (isReadOnly || !pubKey || !code || code.length === 0) { return; } sendTx(pubKey, { applyReferralCode: { id: code as string, }, }) .then((res) => { setFinalized(true); }) .catch((err) => { setError('code', { type: 'required', message: 'Your code has been rejected', }); }); }; if (finalized) { return (

{' '} Code applied

); } return (

Apply a referral code

Enter a referral code

{errors.code && ( {errors.code.message?.toString()} )}
); };