Skip to content

Commit f966436

Browse files
authored
fix: APP-491 empty choose credits form appears for ms (#2591)
1 parent 56601df commit f966436

File tree

4 files changed

+103
-7
lines changed

4 files changed

+103
-7
lines changed

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ 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

19+
import { buyCreditsLoader } from 'pages/BuyCredits/BuyCredits.loader';
1820
import { CertificatePage } from 'pages/Certificate/Certificate';
1921
import MyBridge from 'pages/Dashboard/MyBridge';
2022
import { MyBridgableEcocreditsTable } from 'pages/Dashboard/MyBridge/MyBridge.BridgableEcocreditsTable';
@@ -100,13 +102,13 @@ export const RegenRoutes = ({
100102
reactQueryClient,
101103
apolloClientFactory,
102104
}: RouterProps) => {
103-
const { wallet } = useWallet();
105+
const { activeWalletAddr } = useWallet();
104106
return (
105107
<RouterProvider
106108
router={getRegenRouter({
107109
reactQueryClient,
108110
apolloClientFactory,
109-
address: wallet?.address,
111+
address: activeWalletAddr,
110112
})}
111113
fallbackElement={<PageLoader />}
112114
/>
@@ -116,7 +118,7 @@ export const RegenRoutes = ({
116118
type RouterParams = {
117119
reactQueryClient: QueryClient;
118120
apolloClientFactory: ApolloClientFactory;
119-
address?: string;
121+
address?: Maybe<string>;
120122
};
121123

122124
export const getRegenRoutes = ({
@@ -171,7 +173,15 @@ export const getRegenRoutes = ({
171173
})}
172174
></Route>
173175
</Route>
174-
<Route path="project/:projectId/buy" element={<BuyCredits />} />
176+
<Route
177+
path="project/:projectId/buy"
178+
element={<BuyCredits />}
179+
loader={buyCreditsLoader({
180+
queryClient: reactQueryClient,
181+
apolloClientFactory,
182+
address,
183+
})}
184+
/>
175185
<Route
176186
path="post/:iri"
177187
element={<Post />}
@@ -352,11 +362,12 @@ export const getRegenRoutes = ({
352362
export const getRegenRouter = ({
353363
reactQueryClient,
354364
apolloClientFactory,
365+
address,
355366
}: RouterParams): Router => {
356367
const sentryCreateBrowserRouter =
357368
Sentry.wrapCreateBrowserRouter(createBrowserRouter);
358369
return sentryCreateBrowserRouter(
359-
getRegenRoutes({ reactQueryClient, apolloClientFactory }),
370+
getRegenRoutes({ reactQueryClient, apolloClientFactory, address }),
360371
{
361372
basename: import.meta.env.PUBLIC_URL,
362373
},

web-marketplace/src/lib/queries/react-query/registry-server/getAccounts/getAccountsQuery.types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export type PrivateAccount = {
99
google_email: string | null;
1010
};
1111

12-
type Accounts = {
12+
export type Accounts = {
1313
activeAccountId: string;
1414
authenticatedAccounts?: Array<PrivateAccount>;
1515
};
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { ApolloClient, NormalizedCacheObject } from '@apollo/client';
2+
import { SellOrderInfo } from '@regen-network/api/lib/generated/regen/ecocredit/marketplace/v1/query';
3+
import { QueryClient } from '@tanstack/react-query';
4+
import { getDefaultStore } from 'jotai';
5+
6+
import { Maybe } from 'generated/graphql';
7+
import { selectedLanguageAtom } from 'lib/atoms/languageSwitcher.atoms';
8+
import { ApolloClientFactory } from 'lib/clients/apolloClientFactory';
9+
import { getMarketplaceQueryClient } from 'lib/clients/regen/ecocredit/marketplace/marketplaceQueryClient';
10+
import { getSellOrdersExtendedQuery } from 'lib/queries/react-query/ecocredit/marketplace/getSellOrdersExtendedQuery/getSellOrdersExtendedQuery';
11+
import { getProjectBySlugQuery } from 'lib/queries/react-query/registry-server/graphql/getProjectBySlugQuery/getProjectBySlugQuery';
12+
import { getFromCacheOrFetch } from 'lib/queries/react-query/utils/getFromCacheOrFetch';
13+
14+
import { getIsOnChainId } from 'components/templates/ProjectDetails/ProjectDetails.utils';
15+
16+
type LoaderType = {
17+
queryClient: QueryClient;
18+
apolloClientFactory: ApolloClientFactory;
19+
address?: Maybe<string>;
20+
};
21+
/**
22+
* Loader function for the Buy Credits page that checks if there are available
23+
* sell orders in a given project where the current account address is not the seller.
24+
* Returns true if at least one sell order exists from a different seller.
25+
*/
26+
export const buyCreditsLoader =
27+
({ queryClient, apolloClientFactory, address }: LoaderType) =>
28+
async ({
29+
params: { projectId: projectIdParam },
30+
}: {
31+
params: { projectId?: string };
32+
}) => {
33+
const isOnChainId = getIsOnChainId(projectIdParam);
34+
const apolloClient =
35+
apolloClientFactory.getClient() as ApolloClient<NormalizedCacheObject>;
36+
const marketplaceClient = await getMarketplaceQueryClient();
37+
const atomStore = getDefaultStore();
38+
const selectedLanguage = atomStore.get(selectedLanguageAtom);
39+
40+
const sellOrdersQuery = getSellOrdersExtendedQuery({
41+
client: marketplaceClient,
42+
reactQueryClient: queryClient,
43+
request: {},
44+
});
45+
46+
const allSellOrders = await getFromCacheOrFetch({
47+
query: sellOrdersQuery,
48+
reactQueryClient: queryClient,
49+
});
50+
51+
const projectBySlugQuery = getProjectBySlugQuery({
52+
client: apolloClient,
53+
enabled: !!projectIdParam && !isOnChainId,
54+
slug: projectIdParam as string,
55+
languageCode: selectedLanguage,
56+
});
57+
58+
const projectBySlug = await getFromCacheOrFetch({
59+
query: projectBySlugQuery,
60+
reactQueryClient: queryClient,
61+
});
62+
const projectBySlugId = projectBySlug?.data?.projectBySlug?.onChainId;
63+
const projectId = isOnChainId ? projectIdParam : projectBySlugId;
64+
65+
const availableSellOrders = allSellOrders?.filter(
66+
(sellOrder: SellOrderInfo) =>
67+
sellOrder.seller !== address &&
68+
projectId &&
69+
sellOrder.batchDenom?.startsWith(projectId),
70+
);
71+
72+
return !!availableSellOrders?.length;
73+
};

web-marketplace/src/pages/BuyCredits/BuyCredits.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { useEffect, useState } from 'react';
2-
import { useParams } from 'react-router-dom';
2+
import { useLoaderData, useNavigate, useParams } from 'react-router-dom';
33
import { useAtom, useAtomValue } from 'jotai';
44

5+
import { Loading } from 'web-components/src/components/loading';
6+
57
import NotFoundPage from 'pages/NotFound';
68
import WithLoader from 'components/atoms/WithLoader';
79
import { MultiStepTemplate } from 'components/templates/MultiStepTemplate';
@@ -17,6 +19,14 @@ import { useSummarizePayment } from './hooks/useSummarizePayment';
1719

1820
export const BuyCredits = () => {
1921
const { projectId } = useParams();
22+
const navigate = useNavigate();
23+
const accountCanBuy = useLoaderData();
24+
25+
useEffect(() => {
26+
if (!accountCanBuy) {
27+
navigate(`/project/${projectId}`, { replace: true });
28+
}
29+
}, [navigate, projectId, accountCanBuy]);
2030

2131
const {
2232
loadingSanityProject,
@@ -78,6 +88,8 @@ export const BuyCredits = () => {
7888
}
7989
}, [paymentOption, retiring, setRetiring]);
8090

91+
if (!accountCanBuy) return <Loading />;
92+
8193
if (noProjectFound) return <NotFoundPage />;
8294

8395
return (

0 commit comments

Comments
 (0)