Skip to content

Commit

Permalink
Make get__Account not have undefined in return type
Browse files Browse the repository at this point in the history
  • Loading branch information
serjonya-trili committed Jul 26, 2023
1 parent aca651a commit 885bbac
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/components/CSVFileUploader/CSVFileUploadForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ const CSVFileUploadForm = ({ onClose }: { onClose: () => void }) => {
};

const onSubmit = async ({ file, sender }: FormFields) => {
const account = getAccount(sender) as ImplicitAccount;
const account = getAccount(sender);
Papa.parse<string[]>(file[0], {
skipEmptyLines: true,
complete: (rows: ParseResult<string[]>) => onCSVFileUploadComplete(account, rows),
Expand Down
3 changes: 1 addition & 2 deletions src/components/sendForm/SendForm.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useToast } from "@chakra-ui/react";
import { TezosToolkit, TransferParams } from "@taquito/taquito";
import { useEffect, useRef, useState } from "react";
import { ImplicitAccount } from "../../types/Account";
import { RawPkh } from "../../types/Address";
import { Operation } from "../../types/Operation";
import { useGetImplicitAccount } from "../../utils/hooks/accountHooks";
Expand Down Expand Up @@ -73,7 +72,7 @@ export const SendForm = ({

const addToBatch = async (operation: Operation, senderPkh: RawPkh) => {
// TODO: add support for Multisig
const sender = getAccount(senderPkh) as ImplicitAccount;
const sender = getAccount(senderPkh);

try {
// TODO: add support for Multisig
Expand Down
10 changes: 7 additions & 3 deletions src/components/sendForm/components/SignButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,17 @@ const SignButton: React.FC<{
signerAccount: ImplicitAccount;
network: TezosNetwork;
}> = ({ signerAccount, network, onSubmit }) => {
const { register, handleSubmit, formState } = useForm<{ password: string }>({ mode: "onBlur" });
const { errors } = formState;
const {
register,
handleSubmit,
formState: { errors },
} = useForm<{ password: string }>({ mode: "onBlur" });

const getSecretKey = useGetSecretKey();
const toast = useToast();
const [isLoading, setIsLoading] = useState(false);

// wrapper function that handles changing the isLoading state & error handling
const handleSign = async (getToolkit: () => Promise<TezosToolkit>) => {
if (isLoading) {
return;
Expand Down Expand Up @@ -98,7 +102,7 @@ const SignButton: React.FC<{
)}
{signerAccount.type === AccountType.SOCIAL && <GoogleAuth onSuccessfulAuth={onSocialSign} />}
{signerAccount.type === AccountType.LEDGER && (
<Button onClick={() => onLedgerSign()} bg="umami.blue" width="100%" isLoading={isLoading}>
<Button onClick={onLedgerSign} bg="umami.blue" width="100%" isLoading={isLoading}>
Sign with Ledger
</Button>
)}
Expand Down
24 changes: 12 additions & 12 deletions src/components/sendForm/steps/FillStep.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,8 @@ export const SendTezOrNFTForm = ({

const buildTezFromFormValues = (
formValues: FormValues,
getImplicitAccount: (pkh: RawPkh) => ImplicitAccount | undefined,
getMultisigAccount: (pkh: RawPkh) => MultisigAccount | undefined,
getImplicitAccount: (pkh: RawPkh) => ImplicitAccount,
getMultisigAccount: (pkh: RawPkh) => MultisigAccount,
parameter?: TransferParams["parameter"]
): FormOperations => {
const value: Operation[] = [
Expand All @@ -361,23 +361,23 @@ const buildTezFromFormValues = (
if (formValues.proposalSigner !== undefined) {
return {
type: "proposal",
signer: getImplicitAccount(formValues.proposalSigner) as ImplicitAccount,
signer: getImplicitAccount(formValues.proposalSigner),
content: value,
sender: getMultisigAccount(formValues.sender) as MultisigAccount,
sender: getMultisigAccount(formValues.sender),
};
}
return {
type: "implicit",
content: value,
signer: getImplicitAccount(formValues.sender) as ImplicitAccount,
signer: getImplicitAccount(formValues.sender),
};
};

const buildTokenFromFormValues = (
formValues: FormValues,
asset: Token,
getImplicitAccount: (pkh: RawPkh) => ImplicitAccount | undefined,
getMultisigAccount: (pkh: RawPkh) => MultisigAccount | undefined
getImplicitAccount: (pkh: RawPkh) => ImplicitAccount,
getMultisigAccount: (pkh: RawPkh) => MultisigAccount
): FormOperations => {
const token = [
toOperation(asset, {
Expand All @@ -390,16 +390,16 @@ const buildTokenFromFormValues = (
if (formValues.proposalSigner !== undefined) {
return {
type: "proposal",
signer: getImplicitAccount(formValues.proposalSigner) as ImplicitAccount,
signer: getImplicitAccount(formValues.proposalSigner),
content: token,
sender: getMultisigAccount(formValues.sender) as MultisigAccount,
sender: getMultisigAccount(formValues.sender),
};
}

return {
type: "implicit",
content: token,
signer: getImplicitAccount(formValues.sender) as ImplicitAccount,
signer: getImplicitAccount(formValues.sender),
};
};

Expand Down Expand Up @@ -434,7 +434,7 @@ export const FillStep: React.FC<{
onSubmit({
type: "implicit",
content: [delegation],
signer: getImplicitAccount(formValues.sender) as ImplicitAccount,
signer: getImplicitAccount(formValues.sender),
});
}}
/>
Expand Down Expand Up @@ -508,7 +508,7 @@ export const FillStep: React.FC<{
onSubmit({
type: "implicit",
content: mode.data.batch,
signer: getImplicitAccount(mode.data.signer) as ImplicitAccount,
signer: getImplicitAccount(mode.data.signer),
});
}}
/>
Expand Down
10 changes: 7 additions & 3 deletions src/utils/hooks/accountHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
LedgerAccount,
MultisigAccount,
SocialAccount,
ImplicitAccount,
} from "../../types/Account";
import { RawPkh } from "../../types/Address";
import { decrypt } from "../aes";
Expand All @@ -21,9 +22,11 @@ export const useImplicitAccounts = () => {
return useAppSelector(s => s.accounts.items);
};

// For cleaner code and ease of use this hook returns an ImplicitAccount
// Please make sure not to pass in non existing Pkh
export const useGetImplicitAccount = () => {
const accounts = useImplicitAccounts();
return (pkh: RawPkh) => accounts.find(account => account.address.pkh === pkh);
return (pkh: RawPkh) => accounts.find(account => account.address.pkh === pkh) as ImplicitAccount;
};

export const useReset = () => {
Expand Down Expand Up @@ -141,10 +144,11 @@ export const useMultisigAccounts = (): MultisigAccount[] => {
return multisigs.map((m, i) => multisigToAccount(m, `Multisig Account ${i}`));
};

// For cleaner code and ease of use this hook returns a MultisigAccount
// Please make sure not to pass in non existing Pkh
export const useGetMultisigAccount = () => {
const accounts = useMultisigAccounts();
return (pkh: RawPkh): MultisigAccount | undefined =>
accounts.find(account => account.address.pkh === pkh);
return (pkh: RawPkh) => accounts.find(account => account.address.pkh === pkh) as MultisigAccount;
};

export const useAllAccounts = (): Account[] => {
Expand Down

0 comments on commit 885bbac

Please sign in to comment.