Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Added useTokens hook in package/core/react #780

Merged
merged 4 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { styles } from '../styles';
import Skeleton from 'react-loading-skeleton';
import tokenStyles from './token.module.css';
import { useFrontier } from '~/react/contexts/FrontierContext';
import { useEffect, useMemo, useState } from 'react';
import { useEffect, useState } from 'react';
import coin from '~/react/assets/coin.svg';
import { AuthTooltipMessage, getFormattedNumberString } from '~/react/utils';
import { toast } from 'sonner';
Expand All @@ -13,6 +13,7 @@ import { PlusIcon } from '@radix-ui/react-icons';
import qs from 'query-string';
import { DEFAULT_TOKEN_PRODUCT_NAME } from '~/react/utils/constants';
import { useBillingPermission } from '~/react/hooks/useBillingPermission';
import { useTokens } from '~/react/hooks/useTokens';

interface TokenHeaderProps {
billingSupportEmail?: string;
Expand Down Expand Up @@ -114,8 +115,6 @@ export default function Tokens() {
isActiveOrganizationLoading,
isBillingAccountLoading
} = useFrontier();
const [tokenBalance, setTokenBalance] = useState(0);
const [isTokensLoading, setIsTokensLoading] = useState(false);
const [transactionsList, setTransactionsList] = useState<
V1Beta1BillingTransaction[]
>([]);
Expand All @@ -124,24 +123,9 @@ export default function Tokens() {
const [isCheckoutLoading, setIsCheckoutLoading] = useState(false);
const { isAllowed, isFetching: isPermissionsFetching } =
useBillingPermission();
const { tokenBalance, isTokensLoading } = useTokens();

useEffect(() => {
async function getBalance(orgId: string, billingAccountId: string) {
try {
setIsTokensLoading(true);
const resp = await client?.frontierServiceGetBillingBalance(
orgId,
billingAccountId
);
const tokens = resp?.data?.balance?.amount || '0';
setTokenBalance(Number(tokens));
} catch (err: any) {
console.error(err);
toast.error('Unable to fetch balance');
} finally {
setIsTokensLoading(false);
}
}
async function getTransactions(orgId: string, billingAccountId: string) {
try {
setIsTransactionsListLoading(true);
Expand All @@ -163,7 +147,6 @@ export default function Tokens() {
}

if (activeOrganization?.id && billingAccount?.id) {
getBalance(activeOrganization?.id, billingAccount?.id);
getTransactions(activeOrganization?.id, billingAccount?.id);
}
}, [activeOrganization?.id, billingAccount?.id, client]);
Expand Down
35 changes: 35 additions & 0 deletions sdks/js/packages/core/react/hooks/useTokens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { useEffect, useState } from 'react';
import { useFrontier } from '../contexts/FrontierContext';
import { toast } from 'sonner';

export const useTokens = () => {
const { client, activeOrganization, billingAccount } = useFrontier();

const [tokenBalance, setTokenBalance] = useState(0);
const [isTokensLoading, setIsTokensLoading] = useState(true);

useEffect(() => {
async function getBalance(orgId: string, billingAccountId: string) {
try {
setIsTokensLoading(true);
const resp = await client?.frontierServiceGetBillingBalance(
orgId,
billingAccountId
);
const tokens = resp?.data?.balance?.amount || '0';
setTokenBalance(Number(tokens));
} catch (err: any) {
console.error(err);
toast.error('Unable to fetch balance');
} finally {
setIsTokensLoading(false);
}
}

if (client && activeOrganization?.id && billingAccount?.id) {
getBalance(activeOrganization.id, billingAccount.id);
}
}, [billingAccount?.id, client, activeOrganization?.id]);

return { tokenBalance, isTokensLoading };
};
1 change: 1 addition & 0 deletions sdks/js/packages/core/react/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export { useFrontier } from './contexts/FrontierContext';
export { FrontierProvider } from './contexts/FrontierProvider';

export { Amount };
export { useTokens } from './hooks/useTokens';

export type {
FrontierClientOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,3 @@ export default function BillingAccountDetails() {
</Flex>
);
}

Loading