From 8ec666d0486a654edac7e576746a11a729442b59 Mon Sep 17 00:00:00 2001 From: saml33 Date: Fri, 11 Feb 2022 13:29:07 +1100 Subject: [PATCH] add length based ID form validation --- pages/referral.tsx | 77 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 54 insertions(+), 23 deletions(-) diff --git a/pages/referral.tsx b/pages/referral.tsx index 97ca0a88..c34627bc 100644 --- a/pages/referral.tsx +++ b/pages/referral.tsx @@ -35,6 +35,8 @@ import { useViewport } from '../hooks/useViewport' import { breakpoints } from '../components/TradePageGrid' import { ExpandableRow } from '../components/TableElements' import MobileTableHeader from '../components/mobile/MobileTableHeader' +import Input from '../components/Input' +import InlineNotification from '../components/InlineNotification' export async function getStaticProps({ locale }) { return { @@ -83,6 +85,7 @@ export default function Referral() { const [showAccountsModal, setShowAccountsModal] = useState(false) const [hasReferrals] = useState(true) // Placeholder to show/hide users referral stats const [loading, setLoading] = useState(false) + const [inputError, setInputError] = useState('') const { width } = useViewport() const isMobile = width ? width < breakpoints.sm : false @@ -110,28 +113,48 @@ export default function Referral() { } }, [hasCopied]) - const submitRefLink = async () => { - try { - const txid = await client.registerReferrerId( - mangoGroup, - mangoAccount, - wallet, - customRefLinkInput - ) - notify({ - txid, - title: 'Custom referal link created', - }) - } catch (e) { - notify({ - type: 'error', - title: 'Unable to create referral link', - description: e.message, - txid: e.txid, - }) + const onChangeRefIdInput = (value) => { + setCustomRefLinkInput(value) + if (customRefLinkInput.length > 32) { + setInputError('Referral IDs must be less then 33 characters') + } else { + setInputError('') } } + const validateRefIdInput = () => { + if (customRefLinkInput.length >= 33) { + setInputError('Referral IDs must be less then 33 characters') + } + if (customRefLinkInput.length === 0) { + setInputError('Enter a referral ID') + } + } + + const submitRefLink = async () => { + if (!inputError) { + try { + const txid = await client.registerReferrerId( + mangoGroup, + mangoAccount, + wallet, + customRefLinkInput + ) + notify({ + txid, + title: 'Custom referal link created', + }) + } catch (e) { + notify({ + type: 'error', + title: 'Unable to create referral link', + description: e.message, + txid: e.txid, + }) + } + } else return + } + const handleCopyLink = (link, index) => { copyToClipboard(link) setHasCopied(index) @@ -339,15 +362,23 @@ export default function Referral() { - - setCustomRefLinkInput(e.target.value) - } + onBlur={validateRefIdInput} + onChange={(e) => onChangeRefIdInput(e.target.value)} value={customRefLinkInput} /> + {inputError ? ( +
+ +
+ ) : null}