-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #428 from Chia-Network/fix-tokenization-modal-vali…
…dations fix: tokenization modal wallet address validations
- Loading branch information
Showing
6 changed files
with
116 additions
and
89 deletions.
There are no files selected for viewing
112 changes: 65 additions & 47 deletions
112
src/renderer/components/blocks/forms/CreateTokenForm.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,84 @@ | ||
import React, { forwardRef, useImperativeHandle } from 'react'; | ||
import { Field, HelperText, Spacer } from '@/components'; | ||
import { Field, HelperText, Spacer, validateWalletAddress } from '@/components'; | ||
import { FormattedMessage, IntlShape, useIntl } from 'react-intl'; | ||
import { Form, Formik, FormikProps } from 'formik'; | ||
import * as yup from 'yup'; | ||
import { Address } from '@/schemas/Address.schema'; | ||
|
||
interface FormProps extends React.RefAttributes<CreateTokenFormRef> { | ||
data?: Address[]; | ||
addressBookRecords: Address[]; | ||
onValidityChange: (isValid: boolean) => void; | ||
} | ||
|
||
export interface CreateTokenFormRef { | ||
submitForm: () => Promise<string | undefined>; | ||
submitForm: () => Promise<any>; | ||
} | ||
|
||
const CreateTokenForm: React.FC<FormProps> = forwardRef<CreateTokenFormRef, FormProps>(({ data }, ref) => { | ||
const intl: IntlShape = useIntl(); | ||
const formikRef = React.useRef<FormikProps<any>>(null); | ||
const CreateTokenForm: React.FC<FormProps> = forwardRef<CreateTokenFormRef, FormProps>( | ||
({ addressBookRecords, onValidityChange }, ref) => { | ||
const intl: IntlShape = useIntl(); | ||
const formikRef = React.useRef<FormikProps<Address>>(null); | ||
|
||
const validationSchema = yup.object({ | ||
walletAddress: yup.string().required(intl.formatMessage({ id: 'wallet-address-is-required' })), | ||
}); | ||
const validationSchema = yup.object({ | ||
walletAddress: yup | ||
.string() | ||
.required(intl.formatMessage({ id: 'wallet-address-is-required' })) | ||
.test('validate-wallet-address', function (walletAddress) { | ||
return validateWalletAddress(walletAddress, this, intl); | ||
}), | ||
name: yup.string().optional(), | ||
}); | ||
|
||
useImperativeHandle(ref, () => ({ | ||
submitForm: async () => { | ||
if (formikRef.current) { | ||
const formik = formikRef.current; | ||
if (formik) { | ||
const errors = await formik.validateForm(formik.values); | ||
formik.setTouched(Object.keys(errors).reduce((acc, key) => ({ ...acc, [key]: true }), {})); | ||
if (formik.values?.walletAddress) { | ||
return formik.values.walletAddress; | ||
useImperativeHandle(ref, () => ({ | ||
submitForm: async () => { | ||
if (formikRef.current) { | ||
const formik = formikRef.current; | ||
if (formik) { | ||
const errors = await formik.validateForm(formik.values); | ||
formik.setTouched(Object.keys(errors).reduce((acc, key) => ({ ...acc, [key]: true }), {})); | ||
if (formik.values?.walletAddress) { | ||
return formik.values.walletAddress; | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
})); | ||
}, | ||
})); | ||
|
||
return ( | ||
<Formik innerRef={formikRef} initialValues={data} validationSchema={validationSchema} onSubmit={() => {}}> | ||
{() => ( | ||
<Form> | ||
<div className="mb-4"> | ||
<HelperText className="text-gray-400 sentence-case"> | ||
<FormattedMessage id="carbon-token-recipient" /> | ||
</HelperText> | ||
<Spacer size={5} /> | ||
<Field | ||
name="walletAddress" | ||
type="picklist" | ||
options={ | ||
data?.map((addressData) => ({ | ||
label: addressData.name || '', | ||
value: addressData.walletAddress || '', | ||
})) || [] | ||
} | ||
freeform={true} | ||
/> | ||
</div> | ||
</Form> | ||
)} | ||
</Formik> | ||
); | ||
}); | ||
return ( | ||
<Formik<Address> | ||
innerRef={formikRef} | ||
initialValues={{ walletAddress: '', name: '' }} | ||
validationSchema={validationSchema} | ||
onSubmit={() => {}} | ||
> | ||
{({ isValid }) => { | ||
onValidityChange(isValid); | ||
|
||
return ( | ||
<Form> | ||
<div className="mb-4"> | ||
<HelperText className="text-gray-400 sentence-case"> | ||
<FormattedMessage id="carbon-token-recipient" /> | ||
</HelperText> | ||
<Spacer size={5} /> | ||
<Field | ||
required | ||
name="walletAddress" | ||
type="picklist" | ||
initialValue={''} | ||
options={addressBookRecords.map((addressRecord) => ({ | ||
label: addressRecord.name || '', | ||
value: addressRecord.walletAddress || '', | ||
}))} | ||
freeform={true} | ||
/> | ||
</div> | ||
</Form> | ||
); | ||
}} | ||
</Formik> | ||
); | ||
}, | ||
); | ||
|
||
export { CreateTokenForm }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './Field'; | ||
export * from './TagInput'; | ||
export * from './Repeater'; | ||
export * from './validations'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { TestContext, ValidationError } from 'yup'; | ||
import { IntlShape } from 'react-intl'; | ||
|
||
export const validateWalletAddress = (value: string, context: TestContext, intl: IntlShape): ValidationError | true => { | ||
console.log('!!!!!!!!', value); | ||
|
||
if (!value) return true; // If empty, required will handle it | ||
|
||
if (value.startsWith('xch')) { | ||
if (/^xch[a-zA-Z0-9]{59}$/.test(value)) { | ||
return true; | ||
} else { | ||
return context.createError({ | ||
message: intl.formatMessage({ | ||
id: 'wallet-addresses-start-with-xch-and-are-62-characters-long', | ||
}), | ||
}); | ||
} | ||
} else if (value.startsWith('txch')) { | ||
if (/^txch[a-zA-Z0-9]{59}$/.test(value)) { | ||
return true; | ||
} else { | ||
return context.createError({ | ||
message: intl.formatMessage({ | ||
id: 'testnet-wallet-addresses-start-with-txch-and-are-63-characters-long', | ||
}), | ||
}); | ||
} | ||
} else { | ||
return context.createError({ | ||
message: intl.formatMessage({ | ||
id: 'wallet-address-must-start-with-xch-or-txch', | ||
}), | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters