Readonly tx json view

This commit is contained in:
muzam1l
2022-04-22 18:46:35 +05:30
parent 587f09ec00
commit b3f2d0fb6d
5 changed files with 509 additions and 379 deletions

View File

@@ -1,6 +1,28 @@
import { proxy } from 'valtio';
import { TransactionState } from '../components/Transaction';
import { deepEqual } from '../utils/object';
import transactionsData from "../content/transactions.json";
export type SelectOption = {
value: string;
label: string;
};
export interface TransactionState {
selectedTransaction: SelectOption | null;
selectedAccount: SelectOption | null;
selectedDestAccount: SelectOption | null;
txIsLoading: boolean;
txIsDisabled: boolean;
txFields: TxFields;
}
export type TxFields = Omit<
typeof transactionsData[0],
"Account" | "Sequence" | "TransactionType"
>;
export type OtherFields = (keyof Omit<TxFields, "Destination">)[];
export const defaultTransaction: TransactionState = {
selectedTransaction: null,
@@ -50,10 +72,55 @@ export const modifyTransaction = (
return;
}
if (deepEqual(partialTx, {})) {
tx.state = { ...defaultTransaction }
console.log({ tx: tx.state, is: tx.state === defaultTransaction })
}
Object.keys(partialTx).forEach(k => {
// Typescript mess here, but is definetly safe!
const s = tx.state as any;
const p = partialTx as any;
if (!deepEqual(s[k], p[k])) s[k] = p[k];
});
};
};
export const prepareTransaction = (data: any) => {
let options = { ...data };
// options.Destination = selectedDestAccount?.value;
(Object.keys(options)).forEach(field => {
let _value = options[field];
// convert currency
if (typeof _value === "object" && _value.type === "currency") {
if (+_value.value) {
options[field] = (+_value.value * 1000000 + "") as any;
} else {
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];
}
});
return options
}
export { transactionsData }