some refactoring

This commit is contained in:
muzam1l
2022-04-06 14:42:39 +05:30
parent 842b8a5226
commit 04e2274dbf

View File

@@ -38,6 +38,11 @@ type TxFields = Omit<
>; >;
type OtherFields = (keyof Omit<TxFields, "Destination">)[]; type OtherFields = (keyof Omit<TxFields, "Destination">)[];
interface AccountOption {
label: string;
value: string;
}
interface Props { interface Props {
header?: string; header?: string;
} }
@@ -45,7 +50,7 @@ interface Props {
const Transaction: FC<Props> = ({ header, ...props }) => { const Transaction: FC<Props> = ({ header, ...props }) => {
const snap = useSnapshot(state); const snap = useSnapshot(state);
const transactionsOptions = transactionsData.map((tx) => ({ const transactionsOptions = transactionsData.map(tx => ({
value: tx.TransactionType, value: tx.TransactionType,
label: tx.TransactionType, label: tx.TransactionType,
})); }));
@@ -53,23 +58,24 @@ const Transaction: FC<Props> = ({ header, ...props }) => {
typeof transactionsOptions[0] | null typeof transactionsOptions[0] | null
>(null); >(null);
const accountOptions = snap.accounts.map((acc) => ({ const accountOptions: AccountOption[] = snap.accounts.map(acc => ({
label: acc.name, label: acc.name,
value: acc.address, value: acc.address,
})); }));
const [selectedAccount, setSelectedAccount] = useState<
typeof accountOptions[0] | null
>(null);
const destAccountOptions = snap.accounts const [selectedAccount, setSelectedAccount] = useState<AccountOption | null>(
.map((acc) => ({ null
);
const destAccountOptions: AccountOption[] = snap.accounts
.map(acc => ({
label: acc.name, label: acc.name,
value: acc.address, value: acc.address,
})) }))
.filter((acc) => acc.value !== selectedAccount?.value); .filter(acc => acc.value !== selectedAccount?.value);
const [selectedDestAccount, setSelectedDestAccount] = useState<
typeof destAccountOptions[0] | null const [selectedDestAccount, setSelectedDestAccount] =
>(null); useState<AccountOption | null>(null);
const [txIsLoading, setTxIsLoading] = useState(false); const [txIsLoading, setTxIsLoading] = useState(false);
const [txIsDisabled, setTxIsDisabled] = useState(false); const [txIsDisabled, setTxIsDisabled] = useState(false);
@@ -78,7 +84,7 @@ const Transaction: FC<Props> = ({ header, ...props }) => {
useEffect(() => { useEffect(() => {
const transactionType = selectedTransaction?.value; const transactionType = selectedTransaction?.value;
const account = snap.accounts.find( const account = snap.accounts.find(
(acc) => acc.address === selectedAccount?.value acc => acc.address === selectedAccount?.value
); );
if (!account || !transactionType || txIsLoading) { if (!account || !transactionType || txIsLoading) {
setTxIsDisabled(true); setTxIsDisabled(true);
@@ -89,7 +95,7 @@ const Transaction: FC<Props> = ({ header, ...props }) => {
useEffect(() => { useEffect(() => {
let _txFields: TxFields | undefined = transactionsData.find( let _txFields: TxFields | undefined = transactionsData.find(
(tx) => tx.TransactionType === selectedTransaction?.value tx => tx.TransactionType === selectedTransaction?.value
); );
if (!_txFields) return setTxFields({}); if (!_txFields) return setTxFields({});
_txFields = { ..._txFields } as TxFields; _txFields = { ..._txFields } as TxFields;
@@ -106,7 +112,7 @@ const Transaction: FC<Props> = ({ header, ...props }) => {
const submitTest = useCallback(async () => { const submitTest = useCallback(async () => {
const account = snap.accounts.find( const account = snap.accounts.find(
(acc) => acc.address === selectedAccount?.value acc => acc.address === selectedAccount?.value
); );
const TransactionType = selectedTransaction?.value; const TransactionType = selectedTransaction?.value;
if (!account || !TransactionType || txIsDisabled) return; if (!account || !TransactionType || txIsDisabled) return;
@@ -117,7 +123,7 @@ const Transaction: FC<Props> = ({ header, ...props }) => {
let options = { ...txFields }; 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") {
@@ -183,14 +189,14 @@ const Transaction: FC<Props> = ({ header, ...props }) => {
setTxIsLoading(false); setTxIsLoading(false);
}, []); }, []);
const handleSetAccount = useCallback((acc: typeof accountOptions[0]) => { const handleSetAccount = (acc: AccountOption) => {
setSelectedAccount(acc) setSelectedAccount(acc);
streamState.selectedAccount = acc streamState.selectedAccount = acc;
}, []) };
const usualFields = ["TransactionType", "Amount", "Account", "Destination"]; const usualFields = ["TransactionType", "Amount", "Account", "Destination"];
const otherFields = Object.keys(txFields).filter( const otherFields = Object.keys(txFields).filter(
(k) => !usualFields.includes(k) k => !usualFields.includes(k)
) as OtherFields; ) as OtherFields;
return ( return (
<Box css={{ position: "relative", height: "calc(100% - 28px)" }} {...props}> <Box css={{ position: "relative", height: "calc(100% - 28px)" }} {...props}>
@@ -223,7 +229,7 @@ const Transaction: FC<Props> = ({ header, ...props }) => {
hideSelectedOptions hideSelectedOptions
css={{ width: "70%" }} css={{ width: "70%" }}
value={selectedTransaction} value={selectedTransaction}
onChange={(tt) => setSelectedTransaction(tt as any)} onChange={(tt: any) => setSelectedTransaction(tt)}
/> />
</Flex> </Flex>
<Flex <Flex
@@ -245,7 +251,7 @@ const Transaction: FC<Props> = ({ header, ...props }) => {
css={{ width: "70%" }} css={{ width: "70%" }}
options={accountOptions} options={accountOptions}
value={selectedAccount} value={selectedAccount}
onChange={acc => handleSetAccount(acc as any)} onChange={(acc: any) => handleSetAccount(acc)} // TODO make react-select have correct types for acc
/> />
</Flex> </Flex>
{txFields.Amount !== undefined && ( {txFields.Amount !== undefined && (
@@ -264,7 +270,7 @@ const Transaction: FC<Props> = ({ header, ...props }) => {
</Text> </Text>
<Input <Input
value={txFields.Amount.value} value={txFields.Amount.value}
onChange={(e) => onChange={e =>
setTxFields({ setTxFields({
...txFields, ...txFields,
Amount: { type: "currency", value: e.target.value }, Amount: { type: "currency", value: e.target.value },
@@ -295,11 +301,11 @@ const Transaction: FC<Props> = ({ header, ...props }) => {
options={destAccountOptions} options={destAccountOptions}
value={selectedDestAccount} value={selectedDestAccount}
isClearable isClearable
onChange={(acc) => setSelectedDestAccount(acc as any)} onChange={(acc: any) => setSelectedDestAccount(acc)}
/> />
</Flex> </Flex>
)} )}
{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 = value =
@@ -325,7 +331,7 @@ const Transaction: FC<Props> = ({ header, ...props }) => {
</Text> </Text>
<Input <Input
value={value} value={value}
onChange={(e) => onChange={e =>
setTxFields({ setTxFields({
...txFields, ...txFields,
[field]: [field]:
@@ -382,7 +388,7 @@ const Test = () => {
gutterSize={4} gutterSize={4}
gutterAlign="center" gutterAlign="center"
style={{ height: "calc(100vh - 60px)" }} style={{ height: "calc(100vh - 60px)" }}
onDragEnd={(e) => saveSplit("testVertical", e)} onDragEnd={e => saveSplit("testVertical", e)}
> >
<Flex <Flex
row row
@@ -404,21 +410,19 @@ const Test = () => {
width: "100%", width: "100%",
height: "100%", height: "100%",
}} }}
onDragEnd={(e) => saveSplit("testHorizontal", e)} onDragEnd={e => saveSplit("testHorizontal", e)}
> >
<Box css={{ width: "55%", px: "$2" }}> <Box css={{ width: "55%", px: "$2" }}>
<Tabs <Tabs
keepAllAlive keepAllAlive
forceDefaultExtension forceDefaultExtension
defaultExtension=".json" defaultExtension=".json"
onCreateNewTab={(name) => onCreateNewTab={name => setTabHeaders(tabHeaders.concat(name))}
setTabHeaders(tabHeaders.concat(name)) onCloseTab={index =>
}
onCloseTab={(index) =>
setTabHeaders(tabHeaders.filter((_, idx) => idx !== index)) setTabHeaders(tabHeaders.filter((_, idx) => idx !== index))
} }
> >
{tabHeaders.map((header) => ( {tabHeaders.map(header => (
<Tab key={header} header={header}> <Tab key={header} header={header}>
<Transaction header={header} /> <Transaction header={header} />
</Tab> </Tab>