Commit 5d61a842 authored by isstuev's avatar isstuev

add custom abi

parent 158081b7
...@@ -8,12 +8,15 @@ import { ...@@ -8,12 +8,15 @@ import {
useColorModeValue, useColorModeValue,
} from '@chakra-ui/react'; } from '@chakra-ui/react';
import { useMutation, useQueryClient } from '@tanstack/react-query'; import { useMutation, useQueryClient } from '@tanstack/react-query';
import React, { useCallback, useEffect } from 'react'; import React, { useCallback } from 'react';
import type { ControllerRenderProps, SubmitHandler } from 'react-hook-form'; import type { ControllerRenderProps, SubmitHandler } from 'react-hook-form';
import { useForm, Controller } from 'react-hook-form'; import { useForm, Controller } from 'react-hook-form';
import type { CustomAbi, CustomAbis } from 'types/api/account'; import type { CustomAbi, CustomAbis } from 'types/api/account';
import { ADDRESS_REGEXP } from 'lib/addressValidations';
import AddressInput from 'ui/shared/AddressInput';
type Props = { type Props = {
data?: CustomAbi; data?: CustomAbi;
onClose: () => void; onClose: () => void;
...@@ -25,18 +28,19 @@ type Inputs = { ...@@ -25,18 +28,19 @@ type Inputs = {
abi: string; abi: string;
} }
// idk, maybe there is no limit const NAME_MAX_LENGTH = 255;
const NAME_MAX_LENGTH = 100;
const CustomAbiForm: React.FC<Props> = ({ data, onClose }) => { const CustomAbiForm: React.FC<Props> = ({ data, onClose }) => {
const { control, formState: { errors }, setValue, handleSubmit } = useForm<Inputs>(); const { control, formState: { errors }, handleSubmit } = useForm<Inputs>({
const queryClient = useQueryClient(); defaultValues: {
contract_address_hash: data?.contract_address_hash || '',
name: data?.name || '',
abi: JSON.stringify(data?.abi) || '',
},
mode: 'all',
});
useEffect(() => { const queryClient = useQueryClient();
setValue('contract_address_hash', data?.contract_address_hash || '');
setValue('name', data?.name || '');
setValue('abi', JSON.stringify(data?.abi) || '');
}, [ setValue, data ]);
const customAbiKey = (data: Inputs & { id?: number }) => { const customAbiKey = (data: Inputs & { id?: number }) => {
const body = JSON.stringify({ name: data.name, contract_address_hash: data.contract_address_hash, abi: data.abi }); const body = JSON.stringify({ name: data.name, contract_address_hash: data.contract_address_hash, abi: data.abi });
...@@ -82,13 +86,12 @@ const CustomAbiForm: React.FC<Props> = ({ data, onClose }) => { ...@@ -82,13 +86,12 @@ const CustomAbiForm: React.FC<Props> = ({ data, onClose }) => {
const renderContractAddressInput = useCallback(({ field }: {field: ControllerRenderProps<Inputs, 'contract_address_hash'>}) => { const renderContractAddressInput = useCallback(({ field }: {field: ControllerRenderProps<Inputs, 'contract_address_hash'>}) => {
return ( return (
<FormControl variant="floating" id="contract_address_hash" isRequired backgroundColor={ formBackgroundColor }> <AddressInput<Inputs, 'contract_address_hash'>
<Input field={ field }
{ ...field } isInvalid={ Boolean(errors.contract_address_hash) }
isInvalid={ Boolean(errors.contract_address_hash) } backgroundColor={ formBackgroundColor }
/> placeholder="Smart contract address (0x...)"
<FormLabel>Smart contract address (0x...)</FormLabel> />
</FormControl>
); );
}, [ errors, formBackgroundColor ]); }, [ errors, formBackgroundColor ]);
...@@ -125,15 +128,13 @@ const CustomAbiForm: React.FC<Props> = ({ data, onClose }) => { ...@@ -125,15 +128,13 @@ const CustomAbiForm: React.FC<Props> = ({ data, onClose }) => {
name="contract_address_hash" name="contract_address_hash"
control={ control } control={ control }
render={ renderContractAddressInput } render={ renderContractAddressInput }
rules={{ pattern: ADDRESS_REGEXP }}
/> />
</Box> </Box>
<Box marginTop={ 5 }> <Box marginTop={ 5 }>
<Controller <Controller
name="name" name="name"
control={ control } control={ control }
rules={{
maxLength: NAME_MAX_LENGTH,
}}
render={ renderNameInput } render={ renderNameInput }
/> />
</Box> </Box>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment