Support json fields and better error handling
This commit is contained in:
@@ -172,7 +172,7 @@
|
|||||||
"Fee": "12",
|
"Fee": "12",
|
||||||
"SignerQuorum": 3,
|
"SignerQuorum": 3,
|
||||||
"SignerEntries": {
|
"SignerEntries": {
|
||||||
"type": "list",
|
"type": "json",
|
||||||
"value": [
|
"value": [
|
||||||
{
|
{
|
||||||
"SignerEntry": {
|
"SignerEntry": {
|
||||||
|
|||||||
@@ -83,31 +83,52 @@ const Transaction = () => {
|
|||||||
|
|
||||||
setTxIsLoading(true);
|
setTxIsLoading(true);
|
||||||
// setTxIsError(null)
|
// setTxIsError(null)
|
||||||
let options = { ...txFields };
|
try {
|
||||||
|
let options = { ...txFields };
|
||||||
|
|
||||||
options.Destination = selectedDestAccount?.value;
|
options.Destination = selectedDestAccount?.value;
|
||||||
(Object.keys(options) as (keyof TxFields)[]).forEach(field => {
|
(Object.keys(options) as (keyof TxFields)[]).forEach(field => {
|
||||||
let _value = options[field];
|
let _value = options[field];
|
||||||
// convert currency
|
// convert currency
|
||||||
if (typeof _value === "object" && _value.type === "currency") {
|
if (typeof _value === "object" && _value.type === "currency") {
|
||||||
if (+_value.value) {
|
if (+_value.value) {
|
||||||
options[field] = (+_value.value * 1000000 + "") as any;
|
options[field] = (+_value.value * 1000000 + "") as any;
|
||||||
} else {
|
} else {
|
||||||
options[field] = undefined; // 👇 💀
|
options[field] = undefined; // 👇 💀
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// handle type: `json`
|
||||||
|
if (typeof _value === "object" && _value.type === "json") {
|
||||||
|
if (typeof _value.value === "object") {
|
||||||
|
options[field] = _value.value as any;
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
options[field] = JSON.parse(_value.value);
|
||||||
|
} catch (error) {
|
||||||
|
const message = `Input error for json field '${field}': ${
|
||||||
|
error instanceof Error ? error.message : ""
|
||||||
|
}`;
|
||||||
|
throw Error(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
// delete unneccesary fields
|
|
||||||
if (!options[field]) {
|
|
||||||
delete options[field];
|
|
||||||
}
|
|
||||||
});
|
|
||||||
await sendTransaction(account, {
|
|
||||||
TransactionType,
|
|
||||||
...options,
|
|
||||||
});
|
|
||||||
|
|
||||||
|
// delete unneccesary fields
|
||||||
|
if (!options[field]) {
|
||||||
|
delete options[field];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
await sendTransaction(account, {
|
||||||
|
TransactionType,
|
||||||
|
...options,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
if (error instanceof Error) {
|
||||||
|
state.transactionLogs.push({ type: "error", message: error.message });
|
||||||
|
}
|
||||||
|
}
|
||||||
setTxIsLoading(false);
|
setTxIsLoading(false);
|
||||||
// TODO catch error for UI to show
|
|
||||||
}, [
|
}, [
|
||||||
selectedAccount,
|
selectedAccount,
|
||||||
selectedDestAccount,
|
selectedDestAccount,
|
||||||
@@ -196,6 +217,7 @@ const Transaction = () => {
|
|||||||
{otherFields.map(field => {
|
{otherFields.map(field => {
|
||||||
let _value = txFields[field];
|
let _value = txFields[field];
|
||||||
let value = typeof _value === "object" ? _value.value : _value;
|
let value = typeof _value === "object" ? _value.value : _value;
|
||||||
|
value = typeof value === "object" ? JSON.stringify(value) : value?.toLocaleString();
|
||||||
let isCurrency = typeof _value === "object" && _value.type === "currency";
|
let isCurrency = typeof _value === "object" && _value.type === "currency";
|
||||||
return (
|
return (
|
||||||
<Flex
|
<Flex
|
||||||
@@ -208,7 +230,7 @@ const Transaction = () => {
|
|||||||
{field + (isCurrency ? " (XRP)" : "")}:{" "}
|
{field + (isCurrency ? " (XRP)" : "")}:{" "}
|
||||||
</Text>
|
</Text>
|
||||||
<Input
|
<Input
|
||||||
value={value?.toString()} // TODO handle list maybe
|
value={value}
|
||||||
onChange={e =>
|
onChange={e =>
|
||||||
setTxFields({
|
setTxFields({
|
||||||
...txFields,
|
...txFields,
|
||||||
|
|||||||
@@ -41,10 +41,10 @@ export const sendTransaction = async (account: IAccount, txOptions: TransactionO
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log(err);
|
console.error(err);
|
||||||
state.transactionLogs.push({
|
state.transactionLogs.push({
|
||||||
type: "error",
|
type: "error",
|
||||||
message: "Something went wrong, try again later.",
|
message: err instanceof Error ? `Error: ${err.message}` : 'Something went wrong, try again later',
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user