Add Select UI for account.

This commit is contained in:
muzam1l
2023-03-23 16:12:35 +05:30
parent 62d521b2cc
commit 025eff6cf2
3 changed files with 35 additions and 24 deletions

View File

@@ -38,28 +38,14 @@ export const TxUI: FC<UIProps> = ({
switchToJson switchToJson
}) => { }) => {
const { accounts } = useSnapshot(state) const { accounts } = useSnapshot(state)
const { const { selectedAccount, selectedTransaction, txFields, selectedFlags, hookParameters, memos } =
selectedAccount, txState
selectedDestAccount,
selectedTransaction,
txFields,
selectedFlags,
hookParameters,
memos
} = txState
const accountOptions: SelectOption[] = accounts.map(acc => ({ const accountOptions: SelectOption[] = accounts.map(acc => ({
label: acc.name, label: acc.name,
value: acc.address value: acc.address
})) }))
const destAccountOptions: SelectOption[] = accounts
.map(acc => ({
label: acc.name,
value: acc.address
}))
.filter(acc => acc.value !== selectedAccount?.value)
const flagsOptions: SelectOption[] = Object.entries( const flagsOptions: SelectOption[] = Object.entries(
getFlags(selectedTransaction?.value) || {} getFlags(selectedTransaction?.value) || {}
).map(([label, value]) => ({ ).map(([label, value]) => ({
@@ -215,6 +201,7 @@ export const TxUI: FC<UIProps> = ({
value = _value?.toString() value = _value?.toString()
} }
const isAccount = typeIs(_value, 'object') && _value.$type === 'account'
const isXrpAmount = typeIs(_value, 'object') && _value.$type === 'amount.xrp' const isXrpAmount = typeIs(_value, 'object') && _value.$type === 'amount.xrp'
const isTokenAmount = typeIs(_value, 'object') && _value.$type === 'amount.token' const isTokenAmount = typeIs(_value, 'object') && _value.$type === 'amount.token'
const isJson = typeof _value === 'object' && _value.$type === 'json' const isJson = typeof _value === 'object' && _value.$type === 'json'
@@ -303,6 +290,23 @@ export const TxUI: FC<UIProps> = ({
</TxField> </TxField>
) )
} }
if (isAccount) {
const label = accountOptions.find(a => a.value === value)?.label || value
return (
<TxField key={field} label={field}>
<Select
instanceId={field}
placeholder={`Select ${field} account`}
options={accountOptions}
value={{
value,
label
}}
onChange={(acc: any) => handleSetField(field, acc.value)}
/>
</TxField>
)
}
return ( return (
<TxField key={field} label={field}> <TxField key={field} label={field}>
{isJson ? ( {isJson ? (

View File

@@ -10,7 +10,6 @@ interface TransactionOptions {
TransactionType: string TransactionType: string
Account?: string Account?: string
Fee?: string Fee?: string
Destination?: string
[index: string]: any [index: string]: any
} }
interface OtherOptions { interface OtherOptions {

View File

@@ -130,7 +130,6 @@ export const modifyTxState = (
return tx.state return tx.state
} }
// state to tx options
export const prepareTransaction = (data: any) => { export const prepareTransaction = (data: any) => {
let options = { ...data } let options = { ...data }
@@ -145,7 +144,6 @@ export const prepareTransaction = (data: any) => {
options[field] = undefined options[field] = undefined
} }
} }
// amount.token // amount.token
if (_value.$type === 'amount.token') { if (_value.$type === 'amount.token') {
if (typeIs(_value.$value, 'string')) { if (typeIs(_value.$value, 'string')) {
@@ -156,7 +154,10 @@ export const prepareTransaction = (data: any) => {
options[field] = undefined options[field] = undefined
} }
} }
// account
if (_value.$type === 'account') {
options[field] = _value.$value?.toString();
}
// json // json
if (_value.$type === 'json') { if (_value.$type === 'json') {
const val = _value.$value; const val = _value.$value;
@@ -181,7 +182,6 @@ export const prepareTransaction = (data: any) => {
return options return options
} }
// editor value to state
export const prepareState = (value: string, transactionType?: string) => { export const prepareState = (value: string, transactionType?: string) => {
const options = parseJSON(value) const options = parseJSON(value)
if (!options) { if (!options) {
@@ -252,19 +252,27 @@ export const prepareState = (value: string, transactionType?: string) => {
const isAmount = schemaVal && const isAmount = schemaVal &&
typeIs(schemaVal, "object") && typeIs(schemaVal, "object") &&
schemaVal.$type.startsWith('amount.'); schemaVal.$type.startsWith('amount.');
const isAccount = schemaVal &&
typeIs(schemaVal, "object") &&
schemaVal.$type.startsWith("account");
if (isAmount && ["number", "string"].includes(typeof value)) { if (isAmount && ["number", "string"].includes(typeof value)) {
rest[field] = { rest[field] = {
$type: 'amount.xrp', // TODO narrow typed $type. $type: 'amount.xrp', // TODO narrow typed $type.
$value: +value / 1000000 // ! maybe use bigint? $value: +value / 1000000 // ! maybe use bigint?
} }
} } else if (isAmount && typeof value === 'object') {
else if (isAmount && typeof value === 'object') {
rest[field] = { rest[field] = {
$type: 'amount.token', $type: 'amount.token',
$value: value $value: value
} }
} else if (typeof value === 'object') { } else if (isAccount) {
rest[field] = {
$type: "account",
$value: value?.toString()
}
}
else if (typeof value === 'object') {
rest[field] = { rest[field] = {
$type: 'json', $type: 'json',
$value: value $value: value