Skip to content

Commit 647cdd2

Browse files
committed
feat: update BuyCredits loader to handle project slug and active address
1 parent 63a463c commit 647cdd2

File tree

1 file changed

+48
-21
lines changed

1 file changed

+48
-21
lines changed

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

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,47 +13,60 @@ import {
1313
PrivateAccount,
1414
} from 'lib/queries/react-query/registry-server/getAccounts/getAccountsQuery.types';
1515
import { getAccountByIdQuery } from 'lib/queries/react-query/registry-server/graphql/getAccountByIdQuery/getAccountByIdQuery';
16+
import { getProjectBySlugQuery } from 'lib/queries/react-query/registry-server/graphql/getProjectBySlugQuery/getProjectBySlugQuery';
1617
import { getFromCacheOrFetch } from 'lib/queries/react-query/utils/getFromCacheOrFetch';
1718

19+
import { getIsOnChainId } from 'components/templates/ProjectDetails/ProjectDetails.utils';
20+
1821
type LoaderType = {
1922
queryClient: QueryClient;
2023
apolloClientFactory: ApolloClientFactory;
24+
address: string | undefined;
2125
};
2226
/**
2327
* Loader function for the Buy Credits page that checks if there are available
2428
* sell orders in a given project for a given account address
2529
*/
2630
export const buyCreditsLoader =
27-
({ queryClient, apolloClientFactory }: LoaderType) =>
28-
async ({ params: { projectId } }: { params: { projectId?: string } }) => {
31+
({ queryClient, apolloClientFactory, address }: LoaderType) =>
32+
async ({
33+
params: { projectId: projectIdParam },
34+
}: {
35+
params: { projectId?: string };
36+
}) => {
37+
const isOnChainId = getIsOnChainId(projectIdParam);
2938
const apolloClient =
3039
apolloClientFactory.getClient() as ApolloClient<NormalizedCacheObject>;
3140
const marketplaceClient = await getMarketplaceQueryClient();
3241
const atomStore = getDefaultStore();
3342
const selectedLanguage = atomStore.get(selectedLanguageAtom);
43+
let currentActiveAddress = address;
3444

35-
const data = queryClient.getQueryData([GET_ACCOUNTS_QUERY_KEY]) as
36-
| Accounts
37-
| null
38-
| undefined;
45+
if (!currentActiveAddress) {
46+
const data = (await getFromCacheOrFetch({
47+
query: { queryKey: [GET_ACCOUNTS_QUERY_KEY] },
48+
reactQueryClient: queryClient,
49+
})) as Accounts | null | undefined;
3950

40-
const activeAccountId = data?.activeAccountId;
41-
const privAuthenticatedAccounts = data?.authenticatedAccounts;
51+
const activeAccountId = data?.activeAccountId;
52+
const privAuthenticatedAccounts = data?.authenticatedAccounts;
4253

43-
const authenticatedAccountsResult = await getAuthenticatedAccounts({
44-
privAuthenticatedAccounts,
45-
client: apolloClient,
46-
languageCode: selectedLanguage,
47-
queryClient,
48-
});
54+
const authenticatedAccountsResult = await getAuthenticatedAccounts({
55+
privAuthenticatedAccounts,
56+
client: apolloClient,
57+
languageCode: selectedLanguage,
58+
queryClient,
59+
});
4960

50-
const authenticatedAccounts = authenticatedAccountsResult.map(
51-
result => result?.accountById,
52-
);
61+
const authenticatedAccounts = authenticatedAccountsResult.map(
62+
result => result?.accountById,
63+
);
5364

54-
const activeAccount = authenticatedAccounts.find(
55-
account => account?.id === activeAccountId,
56-
);
65+
const activeAccount = authenticatedAccounts.find(
66+
account => account?.id === activeAccountId,
67+
);
68+
currentActiveAddress = activeAccount?.addr ?? undefined;
69+
}
5770

5871
const sellOrdersQuery = getSellOrdersExtendedQuery({
5972
client: marketplaceClient,
@@ -66,9 +79,23 @@ export const buyCreditsLoader =
6679
reactQueryClient: queryClient,
6780
});
6881

82+
const projectBySlugQuery = getProjectBySlugQuery({
83+
client: apolloClient,
84+
enabled: !!projectIdParam && !isOnChainId,
85+
slug: projectIdParam as string,
86+
languageCode: selectedLanguage,
87+
});
88+
89+
const projectBySlug = await getFromCacheOrFetch({
90+
query: projectBySlugQuery,
91+
reactQueryClient: queryClient,
92+
});
93+
const projectBySlugId = projectBySlug?.data?.projectBySlug?.onChainId;
94+
const projectId = isOnChainId ? projectIdParam : projectBySlugId;
95+
6996
const availableSellOrders = allSellOrders?.filter(
7097
(sellOrder: SellOrderInfo) =>
71-
sellOrder.seller !== activeAccount?.addr &&
98+
sellOrder.seller !== currentActiveAddress &&
7299
projectId &&
73100
sellOrder.batchDenom?.startsWith(projectId),
74101
);

0 commit comments

Comments
 (0)