From 386619619b6b762e31b02ee4646a3f41284ef7f0 Mon Sep 17 00:00:00 2001 From: muzam1l Date: Wed, 4 May 2022 16:57:32 +0530 Subject: [PATCH] support object `Amount` --- components/Transaction/index.tsx | 13 ++--------- components/Transaction/json.tsx | 8 +++---- components/Transaction/ui.tsx | 40 ++++---------------------------- content/transactions.json | 16 ++++++------- state/transactions.ts | 19 ++++++++------- 5 files changed, 29 insertions(+), 67 deletions(-) diff --git a/components/Transaction/index.tsx b/components/Transaction/index.tsx index a1d9b63..558bad3 100644 --- a/components/Transaction/index.tsx +++ b/components/Transaction/index.tsx @@ -82,7 +82,7 @@ const Transaction: FC = ({ let st: TransactionState | undefined; if (viewType === "json") { // save the editor state first - const pst = prepareState(editorValue); + const pst = prepareState(editorValue || '', txState); if (!pst) return; st = setState(pst); @@ -116,16 +116,7 @@ const Transaction: FC = ({ } } setState({ txIsLoading: false }); - }, [ - viewType, - editorValue, - accounts, - txIsDisabled, - setState, - selectedAccount?.value, - prepareOptions, - header, - ]); + }, [viewType, accounts, txIsDisabled, setState, header, editorValue, txState, selectedAccount?.value, prepareOptions]); const resetState = useCallback(() => { modifyTransaction(header, { viewType }, { replaceState: true }); diff --git a/components/Transaction/json.tsx b/components/Transaction/json.tsx index 5460613..acccff8 100644 --- a/components/Transaction/json.tsx +++ b/components/Transaction/json.tsx @@ -45,8 +45,8 @@ export const TxJson: FC = ({ else setHasUnsaved(true); }, [editorValue, value]); - const saveState = (value: string) => { - const tx = prepareState(value); + const saveState = (value: string, txState: TransactionState) => { + const tx = prepareState(value, txState); if (tx) setState(tx); }; @@ -61,7 +61,7 @@ export const TxJson: FC = ({ const onExit = (value: string) => { const options = parseJSON(value); if (options) { - saveState(value); + saveState(value, txState); return; } showAlert("Error!", { @@ -106,7 +106,7 @@ export const TxJson: FC = ({ {hasUnsaved && ( This file has unsaved changes.{" "} - saveState(editorValue)}>save{" "} + saveState(editorValue, txState)}>save{" "} discard )} diff --git a/components/Transaction/ui.tsx b/components/Transaction/ui.tsx index 7dd5241..b933a03 100644 --- a/components/Transaction/ui.tsx +++ b/components/Transaction/ui.tsx @@ -9,7 +9,6 @@ import { TransactionState, transactionsData, TxFields, - OtherFields, } from "../../state/transactions"; import { useSnapshot } from "valtio"; import state from "../../state"; @@ -74,11 +73,11 @@ export const TxUI: FC = ({ state: txState, setState }) => { resetOptions(tt.value); }; - const usualFields = ["TransactionType", "Amount", "Account", "Destination"]; + const specialFields = ["TransactionType", "Account", "Destination"]; const otherFields = Object.keys(txFields).filter( - k => !usualFields.includes(k) - ) as OtherFields; + k => !specialFields.includes(k) + ) as [keyof TxFields]; return ( = ({ state: txState, setState }) => { onChange={(acc: any) => handleSetAccount(acc)} // TODO make react-select have correct types for acc /> - {txFields.Amount !== undefined && ( - - - Amount (XRP):{" "} - - - setState({ - txFields: { - ...txFields, - Amount: { type: "currency", value: e.target.value }, - }, - }) - } - css={{ width: "70%", flex: "inherit" }} - /> - - )} {txFields.Destination !== undefined && ( = ({ state: txState, setState }) => { value = _value?.toString(); } - let isCurrency = - typeof _value === "object" && _value.type === "currency"; + let isXrp = typeof _value === "object" && _value.type === "xrp"; return ( = ({ state: txState, setState }) => { }} > - {field + (isCurrency ? " (XRP)" : "")}:{" "} + {field + (isXrp ? " (XRP)" : "")}:{" "} ; -export type OtherFields = (keyof Omit)[]; - export const defaultTransaction: TransactionState = { selectedTransaction: null, selectedAccount: null, @@ -107,8 +105,8 @@ export const prepareTransaction = (data: any) => { (Object.keys(options)).forEach(field => { let _value = options[field]; - // convert currency - if (_value && typeof _value === "object" && _value.type === "currency") { + // convert xrp + if (_value && typeof _value === "object" && _value.type === "xrp") { if (+_value.value) { options[field] = (+_value.value * 1000000 + "") as any; } else { @@ -141,7 +139,7 @@ export const prepareTransaction = (data: any) => { } // editor value to state -export const prepareState = (value?: string) => { +export const prepareState = (value: string, txState: TransactionState) => { const options = parseJSON(value); if (!options) { showAlert("Error!", { @@ -152,6 +150,7 @@ export const prepareState = (value?: string) => { const { Account, TransactionType, Destination, ...rest } = options; let tx: Partial = {}; + const { txFields } = txState if (Account) { const acc = state.accounts.find(acc => acc.address === Account); @@ -179,7 +178,7 @@ export const prepareState = (value?: string) => { tx.selectedTransaction = null; } - if (Destination !== undefined) { + if (txFields.Destination !== undefined) { const dest = state.accounts.find(acc => acc.address === Destination); rest.Destination = null if (dest) { @@ -201,10 +200,12 @@ export const prepareState = (value?: string) => { Object.keys(rest).forEach(field => { const value = rest[field]; - if (field === "Amount") { + const origValue = txFields[field as keyof TxFields] + const isXrp = typeof value !== 'object' && origValue && typeof origValue === 'object' && origValue.type === 'xrp' + if (isXrp) { rest[field] = { - type: "currency", - value: +value / 1000000, // TODO handle object currencies + type: "xrp", + value: +value / 1000000, // TODO maybe use bigint? }; } else if (typeof value === "object") { rest[field] = {