Support json fields and better error handling

This commit is contained in:
muzam
2022-01-12 14:51:02 +05:30
parent a9676288ea
commit 0c4330e329
3 changed files with 47 additions and 25 deletions

View File

@@ -172,7 +172,7 @@
"Fee": "12", "Fee": "12",
"SignerQuorum": 3, "SignerQuorum": 3,
"SignerEntries": { "SignerEntries": {
"type": "list", "type": "json",
"value": [ "value": [
{ {
"SignerEntry": { "SignerEntry": {

View File

@@ -83,6 +83,7 @@ const Transaction = () => {
setTxIsLoading(true); setTxIsLoading(true);
// setTxIsError(null) // setTxIsError(null)
try {
let options = { ...txFields }; let options = { ...txFields };
options.Destination = selectedDestAccount?.value; options.Destination = selectedDestAccount?.value;
@@ -96,6 +97,22 @@ const Transaction = () => {
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 // delete unneccesary fields
if (!options[field]) { if (!options[field]) {
delete options[field]; delete options[field];
@@ -105,9 +122,13 @@ const Transaction = () => {
TransactionType, TransactionType,
...options, ...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,

View File

@@ -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',
}); });
} }
}; };