Skip to content

Commit beb0b4e

Browse files
committed
refactor: update BuyCredits loader and RegenRoutes to use activeWalletAddr
1 parent 6a6c779 commit beb0b4e

File tree

2 files changed

+7
-69
lines changed

2 files changed

+7
-69
lines changed

web-marketplace/src/clients/regen/Regen.Routes.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { Router } from '@remix-run/router';
1212
import * as Sentry from '@sentry/react';
1313
import { QueryClient } from '@tanstack/react-query';
1414

15+
import { Maybe } from 'generated/graphql';
1516
import { ApolloClientFactory } from 'lib/clients/apolloClientFactory';
1617
import { useWallet } from 'lib/wallet/wallet';
1718

@@ -101,13 +102,13 @@ export const RegenRoutes = ({
101102
reactQueryClient,
102103
apolloClientFactory,
103104
}: RouterProps) => {
104-
const { wallet } = useWallet();
105+
const { activeWalletAddr } = useWallet();
105106
return (
106107
<RouterProvider
107108
router={getRegenRouter({
108109
reactQueryClient,
109110
apolloClientFactory,
110-
address: wallet?.address,
111+
address: activeWalletAddr,
111112
})}
112113
fallbackElement={<PageLoader />}
113114
/>
@@ -117,7 +118,7 @@ export const RegenRoutes = ({
117118
type RouterParams = {
118119
reactQueryClient: QueryClient;
119120
apolloClientFactory: ApolloClientFactory;
120-
address?: string;
121+
address?: Maybe<string>;
121122
};
122123

123124
export const getRegenRoutes = ({

web-marketplace/src/pages/BuyCredits/BuyCredits.loader.ts

Lines changed: 3 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,11 @@ import { SellOrderInfo } from '@regen-network/api/lib/generated/regen/ecocredit/
33
import { QueryClient } from '@tanstack/react-query';
44
import { getDefaultStore } from 'jotai';
55

6+
import { Maybe } from 'generated/graphql';
67
import { selectedLanguageAtom } from 'lib/atoms/languageSwitcher.atoms';
78
import { ApolloClientFactory } from 'lib/clients/apolloClientFactory';
89
import { getMarketplaceQueryClient } from 'lib/clients/regen/ecocredit/marketplace/marketplaceQueryClient';
910
import { getSellOrdersExtendedQuery } from 'lib/queries/react-query/ecocredit/marketplace/getSellOrdersExtendedQuery/getSellOrdersExtendedQuery';
10-
import { GET_ACCOUNTS_QUERY_KEY } from 'lib/queries/react-query/registry-server/getAccounts/getAccountsQuery.constants';
11-
import {
12-
Accounts,
13-
PrivateAccount,
14-
} from 'lib/queries/react-query/registry-server/getAccounts/getAccountsQuery.types';
15-
import { getAccountByIdQuery } from 'lib/queries/react-query/registry-server/graphql/getAccountByIdQuery/getAccountByIdQuery';
1611
import { getProjectBySlugQuery } from 'lib/queries/react-query/registry-server/graphql/getProjectBySlugQuery/getProjectBySlugQuery';
1712
import { getFromCacheOrFetch } from 'lib/queries/react-query/utils/getFromCacheOrFetch';
1813

@@ -21,7 +16,7 @@ import { getIsOnChainId } from 'components/templates/ProjectDetails/ProjectDetai
2116
type LoaderType = {
2217
queryClient: QueryClient;
2318
apolloClientFactory: ApolloClientFactory;
24-
address: string | undefined;
19+
address?: Maybe<string>;
2520
};
2621
/**
2722
* Loader function for the Buy Credits page that checks if there are available
@@ -40,33 +35,6 @@ export const buyCreditsLoader =
4035
const marketplaceClient = await getMarketplaceQueryClient();
4136
const atomStore = getDefaultStore();
4237
const selectedLanguage = atomStore.get(selectedLanguageAtom);
43-
let currentActiveAddress = address;
44-
45-
if (!currentActiveAddress) {
46-
const data = (await getFromCacheOrFetch({
47-
query: { queryKey: [GET_ACCOUNTS_QUERY_KEY] },
48-
reactQueryClient: queryClient,
49-
})) as Accounts | null | undefined;
50-
51-
const activeAccountId = data?.activeAccountId;
52-
const privAuthenticatedAccounts = data?.authenticatedAccounts;
53-
54-
const authenticatedAccountsResult = await getAuthenticatedAccounts({
55-
privAuthenticatedAccounts,
56-
client: apolloClient,
57-
languageCode: selectedLanguage,
58-
queryClient,
59-
});
60-
61-
const authenticatedAccounts = authenticatedAccountsResult.map(
62-
result => result?.accountById,
63-
);
64-
65-
const activeAccount = authenticatedAccounts.find(
66-
account => account?.id === activeAccountId,
67-
);
68-
currentActiveAddress = activeAccount?.addr ?? undefined;
69-
}
7038

7139
const sellOrdersQuery = getSellOrdersExtendedQuery({
7240
client: marketplaceClient,
@@ -95,41 +63,10 @@ export const buyCreditsLoader =
9563

9664
const availableSellOrders = allSellOrders?.filter(
9765
(sellOrder: SellOrderInfo) =>
98-
sellOrder.seller !== currentActiveAddress &&
66+
sellOrder.seller !== address &&
9967
projectId &&
10068
sellOrder.batchDenom?.startsWith(projectId),
10169
);
10270

10371
return !!availableSellOrders?.length;
10472
};
105-
106-
async function getAuthenticatedAccounts({
107-
privAuthenticatedAccounts,
108-
client,
109-
languageCode,
110-
queryClient,
111-
}: {
112-
privAuthenticatedAccounts: PrivateAccount[] | undefined;
113-
client: ApolloClient<NormalizedCacheObject>;
114-
languageCode: string;
115-
queryClient: QueryClient;
116-
}) {
117-
if (!privAuthenticatedAccounts?.length) return [];
118-
119-
const accounts = await Promise.all(
120-
privAuthenticatedAccounts.map(({ id }) => {
121-
const query = getAccountByIdQuery({
122-
client,
123-
id,
124-
languageCode,
125-
});
126-
127-
return queryClient.fetchQuery({
128-
queryKey: query.queryKey,
129-
queryFn: query.queryFn,
130-
});
131-
}),
132-
);
133-
134-
return accounts;
135-
}

0 commit comments

Comments
 (0)