Skip to content

Commit

Permalink
refactor(send-form): remove custom dropdown type
Browse files Browse the repository at this point in the history
  • Loading branch information
kyranjamie committed Feb 27, 2024
1 parent a1fdbd2 commit 5b6bbfc
Show file tree
Hide file tree
Showing 16 changed files with 153 additions and 242 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { InscriptionPreviewCard } from '@app/components/inscription-preview-card
import { OrdinalIcon } from '@app/ui/components/avatar-icon/ordinal-icon';
import { Button } from '@app/ui/components/button/button';

import { RecipientField } from '../send-crypto-asset-form/components/recipient-field';
import { RecipientAddressTypeField } from '../send-crypto-asset-form/components/recipient-address-type-field';
import { CollectibleAsset } from './components/collectible-asset';
import { useSendInscriptionState } from './components/send-inscription-container';
import { useSendInscriptionForm } from './hooks/use-send-inscription-form';
Expand Down Expand Up @@ -48,7 +48,7 @@ export function SendInscriptionForm() {
<Box mt={['space.04', 'space.06', '100px']}>
<Flex flexDirection="column" mt="space.05" width="100%">
<CollectibleAsset icon={<OrdinalIcon />} name="Ordinal inscription" />
<RecipientField
<RecipientAddressTypeField
name={recipeintFieldName}
label="To"
placeholder="Enter recipient address"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,20 @@ import { BitcoinSendFormValues, StacksSendFormValues } from '@shared/models/form

import { Input } from '@app/ui/components/input/input';

interface RecipientFieldProps {
isDisabled?: boolean;
interface RecipientAddressTypeFieldProps {
label?: string;
name: string;
onBlur?(): void;
placeholder: string;
topInputOverlay?: React.JSX.Element;
rightLabel?: React.JSX.Element;
}
export function RecipientField({
export function RecipientAddressTypeField({
name,
topInputOverlay,
rightLabel,
isDisabled,
onBlur,
}: RecipientFieldProps) {
}: RecipientAddressTypeFieldProps) {
const [field] = useField(name);
const { setFieldValue } = useFormikContext<BitcoinSendFormValues | StacksSendFormValues>();

Expand All @@ -41,7 +39,6 @@ export function RecipientField({
<Input.Field
data-testid={SendCryptoAssetSelectors.RecipientFieldInput}
placeholder="Recipient"
disabled={isDisabled}
{...field}
onBlur={e => {
field.onBlur(e);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@ import { useNavigate } from 'react-router-dom';

import { useFormikContext } from 'formik';

import { BitcoinSendFormValues, StacksSendFormValues } from '@shared/models/form.model';
import { RouteUrls } from '@shared/route-urls';

import { RecipientFieldType } from '@app/pages/send/send-crypto-asset-form/components/recipient-type-dropdown/recipient-type-dropdown';

import { useRecipientBnsName } from './use-recipient-bns-name';

export const recipientIdentifierTypesMap = { bnsName: 'BNS Name', address: 'Address' } as const;

export type RecipientIdentifierType = keyof typeof recipientIdentifierTypesMap;

export function useRecipientSelectFields() {
const { setFieldError, setFieldTouched, setFieldValue } = useFormikContext<
BitcoinSendFormValues | StacksSendFormValues
>();
const [selectedRecipientField, setSelectedRecipientField] = useState(RecipientFieldType.Address);
const { setFieldError, setFieldTouched, setFieldValue } = useFormikContext();

const [selectedRecipientField, setSelectedRecipientField] =
useState<RecipientIdentifierType>('address');

const [isSelectVisible, setIsSelectVisible] = useState(false);
const { setBnsAddress } = useRecipientBnsName();
const navigate = useNavigate();

// Formik does not provide a field reset
const resetAllRecipientFields = useCallback(async () => {
void setFieldValue('recipient', '');
setFieldError('recipient', undefined);
Expand All @@ -32,17 +33,20 @@ export function useRecipientSelectFields() {
return useMemo(
() => ({
selectedRecipientField,

selectedRecipientFieldName: recipientIdentifierTypesMap[selectedRecipientField],

isSelectVisible,

showRecipientAccountsModal() {
setSelectedRecipientField(RecipientFieldType.Address);
setSelectedRecipientField('address');
navigate(RouteUrls.SendCryptoAssetFormRecipientAccounts, { replace: true });
},

async onSelectRecipientFieldType(index: number) {
async onSelectRecipientFieldType(recipientType: RecipientIdentifierType) {
await resetAllRecipientFields();
setBnsAddress('');
setSelectedRecipientField(index);
setSelectedRecipientField(recipientType);
setIsSelectVisible(false);
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,22 @@ import { useFormikContext } from 'formik';

import { BitcoinSendFormValues, StacksSendFormValues } from '@shared/models/form.model';

import { RecipientField } from '@app/pages/send/send-crypto-asset-form/components/recipient-field';
import { RecipientAddressTypeField } from '@app/pages/send/send-crypto-asset-form/components/recipient-address-type-field';
import { StacksClient } from '@app/query/stacks/stacks-client';

import { RecipientAddressDisplayer } from './components/recipient-address-displayer';
import { useRecipientBnsName } from './hooks/use-recipient-bns-name';

interface RecipientFieldBnsNameProps {
interface RecipientBnsNameTypeFieldProps {
fetchFn(client: StacksClient, name: string, isTestnet?: boolean): Promise<string | null>;
isSelectVisible: boolean;
selectedRecipientField: number;
topInputOverlay: React.JSX.Element;
rightLabel: React.JSX.Element;
}
export function RecipientFieldBnsName({
export function RecipientBnsNameTypeField({
fetchFn,
isSelectVisible,
topInputOverlay,
rightLabel,
}: RecipientFieldBnsNameProps) {
}: RecipientBnsNameTypeFieldProps) {
const [showBnsAddress, setShowBnsAddress] = useState(false);
const { errors, setFieldError, values } = useFormikContext<
BitcoinSendFormValues | StacksSendFormValues
Expand All @@ -47,8 +44,7 @@ export function RecipientFieldBnsName({

return (
<>
<RecipientField
isDisabled={isSelectVisible}
<RecipientAddressTypeField
name="recipientBnsName"
onBlur={() => getBnsAddressAndValidate(fetchFn)}
placeholder="Enter recipient BNS name"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { TextInputFieldError } from '@app/components/field-error';

import { SelectAccountButton } from '../recipient-accounts-drawer/select-account-button';
import { RecipientAddressTypeField } from '../recipient-address-type-field';
import { RecipientIdentifierTypeDropdown } from '../recipient-type-dropdown/recipient-type-dropdown';
import { useRecipientSelectFields } from './hooks/use-recipient-select-fields';
import { RecipientBnsNameTypeField } from './recipient-bns-name-type-field';

interface RecipientFieldProps {
bnsLookupFn(client: any, name: string, isTestnet?: boolean): Promise<string | null>;
}
export function RecipientField({ bnsLookupFn }: RecipientFieldProps) {
const {
showRecipientAccountsModal,
onSelectRecipientFieldType,
selectedRecipientFieldName,
selectedRecipientField,
} = useRecipientSelectFields();

const recipientDropdown = (
<RecipientIdentifierTypeDropdown
activeRecipientIdentifierType={selectedRecipientFieldName}
onSelectRecipientIdentifierType={recipientType => onSelectRecipientFieldType(recipientType)}
/>
);

const selectAccountButton = <SelectAccountButton onClick={showRecipientAccountsModal} />;

switch (selectedRecipientField) {
case 'bnsName':
return (
<>
<RecipientBnsNameTypeField
fetchFn={bnsLookupFn}
rightLabel={selectAccountButton}
topInputOverlay={recipientDropdown}
/>
<TextInputFieldError name="recipientBnsName" />
</>
);
case 'address':
default:
return (
<>
<RecipientAddressTypeField
name="recipient"
rightLabel={selectAccountButton}
placeholder="Enter recipient address"
topInputOverlay={recipientDropdown}
/>
<TextInputFieldError name="recipient" />
</>
);
}
}

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 5b6bbfc

Please sign in to comment.