From 5d3f1b86e8bb2efb2173b6452060e37de603740d Mon Sep 17 00:00:00 2001 From: fmtabbara Date: Thu, 2 Sep 2021 17:29:03 +0100 Subject: [PATCH] delegate form validation --- .../src/routes/delegate/_DelegateForm.tsx | 111 ++++++++++++++++++ tauri-wallet/src/routes/delegate/index.tsx | 2 +- .../src/routes/delegate/validationSchema.ts | 17 +++ tauri-wallet/src/theme.tsx | 3 + 4 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 tauri-wallet/src/routes/delegate/_DelegateForm.tsx create mode 100644 tauri-wallet/src/routes/delegate/validationSchema.ts diff --git a/tauri-wallet/src/routes/delegate/_DelegateForm.tsx b/tauri-wallet/src/routes/delegate/_DelegateForm.tsx new file mode 100644 index 0000000000..2609489f68 --- /dev/null +++ b/tauri-wallet/src/routes/delegate/_DelegateForm.tsx @@ -0,0 +1,111 @@ +import React from 'react' +import { + Button, + FormControl, + Grid, + InputAdornment, + TextField, + Theme, + useTheme, +} from '@material-ui/core' +import { useForm } from 'react-hook-form' +import { NodeTypeSelector } from '../../components/NodeTypeSelector' +import { EnumNodeType } from '../../types/global' +import { yupResolver } from '@hookform/resolvers/yup' +import { validationSchema } from './validationSchema' + +type TDelegateForm = { + nodeType: EnumNodeType + identity: string + amount: string +} + +const defaultValues: TDelegateForm = { + nodeType: EnumNodeType.Mixnode, + identity: '', + amount: '', +} + +export const DelegateForm = () => { + const theme = useTheme() + const { + register, + setValue, + watch, + handleSubmit, + formState: { errors }, + } = useForm({ + defaultValues, + resolver: yupResolver(validationSchema), + }) + + const watchNodeType = watch('nodeType', defaultValues.nodeType) + + const onSubmit = (data: TDelegateForm) => console.log(data) + + return ( + +
+ + + setValue('nodeType', nodeType)} + /> + + + + + + + punks + ), + }} + /> + + +
+
+ +
+
+ ) +} diff --git a/tauri-wallet/src/routes/delegate/index.tsx b/tauri-wallet/src/routes/delegate/index.tsx index 9ca3a65d10..deee341100 100644 --- a/tauri-wallet/src/routes/delegate/index.tsx +++ b/tauri-wallet/src/routes/delegate/index.tsx @@ -1,5 +1,5 @@ import React from 'react' -import { DelegateForm } from './DelegateForm' +import { DelegateForm } from './_DelegateForm' import { Layout, NymCard, Page } from '../../components' export const Delegate = () => { diff --git a/tauri-wallet/src/routes/delegate/validationSchema.ts b/tauri-wallet/src/routes/delegate/validationSchema.ts new file mode 100644 index 0000000000..9085eb508a --- /dev/null +++ b/tauri-wallet/src/routes/delegate/validationSchema.ts @@ -0,0 +1,17 @@ +import * as Yup from 'yup' +import { validateAmount, validateKey } from '../../utils' + +export const validationSchema = Yup.object().shape({ + identity: Yup.string() + .required() + .test( + 'valid-id-key', + 'A valid identity key is required e.g. 824WyExLUWvLE2mpSHBatN4AoByuLzfnHFeHWiBYzg4z', + (value) => (!!value ? validateKey(value) : false) + ), + amount: Yup.string() + .required() + .test('valid-amount-key', 'A valid amount key is required', (value) => + !!value ? validateAmount(value, '1000000') : false + ), +}) diff --git a/tauri-wallet/src/theme.tsx b/tauri-wallet/src/theme.tsx index 1eabd5afb4..46ae236c30 100644 --- a/tauri-wallet/src/theme.tsx +++ b/tauri-wallet/src/theme.tsx @@ -32,6 +32,9 @@ export const theme = createTheme({ containedPrimary: { color: 'white', }, + text: { + padding: '12px 24px', + }, }, MuiStepIcon: {