Skip to content

Commit

Permalink
feat: merge states of multiple hooks for erc1155 tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
aramalipoor committed Oct 9, 2022
1 parent 491aecc commit d7fa20e
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 65 deletions.
2 changes: 1 addition & 1 deletion packages/react/src/common/hooks/useAxiosGet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const useAxiosGet = <T>({
enabled = true,
}: Config) => {
const [response, setResponse] = useState<T | null>(null);
const [error, setError] = useState<Error>();
const [error, setError] = useState<Error | undefined | null>();
const [isLoading, setLoading] = useState(enabled);

const cancelQuery = useCancel();
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export * from './diamonds';
export * from './ipfs';
export * from './meta-transactions';
export * from './ui';
export * from './utils';
export * from './wallet';
1 change: 1 addition & 0 deletions packages/react/src/core/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './useMergeQueryStates';
31 changes: 31 additions & 0 deletions packages/react/src/core/utils/useMergeQueryStates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { useQuery } from '@tanstack/react-query';

export const useMergeQueryStates = (
results: Partial<ReturnType<typeof useQuery<any, any, any>> | any>[],
) => {
const isLoading = results.some((result) => result.isLoading);
const isFetching = results.some((result) => result.isFetching);
const isFetched = results.every((result) => result.isFetched);
const isError = results.some((result) => result.isError);
const isSuccess = results.every((result) => result.isSuccess);
const isInitialLoading = results.some((result) => result.isInitialLoading);
const isRefetching = results.some((result) => result.isRefetching);

const error = results.find((result) => result.isError && result.error)?.error;
const fetchStatus =
results
.map((result) => result.fetchStatus)
.find((status) => status !== 'idle') || 'idle';

return {
isLoading,
isFetching,
isFetched,
isError,
isSuccess,
isInitialLoading,
isRefetching,
fetchStatus,
error,
} as const;
};
9 changes: 1 addition & 8 deletions packages/react/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import '@tanstack/query-core';
import '@wagmi/core';

export * from '@flair-sdk/common';
export * from '@flair-sdk/registry';
export * from '@flair-sdk/ipfs';
export * from '@flair-sdk/metatx';

export * from './common';
export * from './core';
export * from './modules';
export * from './providers';
export * from './setupTests';
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Environment } from '@flair-sdk/common';
import { BigNumber } from 'ethers';
import { useMemo } from 'react';

import { useMergeQueryStates } from '../../../../../core';
import { useNftTokensByCollection } from '../../../../../core/data-query/hooks/useNftTokensByCollection';
import {
useTokenMetadataBaseUri,
Expand All @@ -28,17 +29,14 @@ export const useERC1155TokensByCollectionContractOnly = ({
contractAddress,
enabled = true,
}: Props) => {
const {
data: collectionTokens,
error: collectionTokensError,
isLoading: collectionTokensLoading,
} = useNftTokensByCollection({
env,
clientId,
chainId,
contractAddress,
enabled,
});
const { data: collectionTokens, ...collectionTokensState } =
useNftTokensByCollection({
env,
clientId,
chainId,
contractAddress,
enabled,
});

const tokenIds = useMemo(() => {
return Array.from(Array(100).keys());
Expand All @@ -51,45 +49,41 @@ export const useERC1155TokensByCollectionContractOnly = ({
// : Array.from(Array(100).keys());
}, []);

const {
data: collectionBaseUri,
error: collectionBaseUriError,
isLoading: collectionBaseUriLoading,
} = useTokenMetadataBaseUri({
chainId,
contractAddress,
});
const { data: collectionBaseUri, ...collectionBaseUriState } =
useTokenMetadataBaseUri({
chainId,
contractAddress,
});

const {
data: collectionFallbackUri,
error: collectionFallbackUriError,
isLoading: collectionFallbackUriLoading,
} = useTokenMetadataFallbackUri({
chainId,
contractAddress,
});
const { data: collectionFallbackUri, ...collectionFallbackUriState } =
useTokenMetadataFallbackUri({
chainId,
contractAddress,
});

const {
data: tokenIdToMaxSupplyMapping,
error: tokenIdToMaxSupplyMappingError,
isLoading: tokenIdToMaxSupplyMappingLoading,
} = useERC1155MaxSupplyBatch({
chainId,
contractAddress,
tokenIds,
enabled,
});
const { data: tokenIdToMaxSupplyMapping, ...tokenIdToMaxSupplyMappingState } =
useERC1155MaxSupplyBatch({
chainId,
contractAddress,
tokenIds,
enabled,
});

const {
data: tokenIdToUriMapping,
error: tokenIdToUriMappingError,
isLoading: tokenIdToUriMappingLoading,
} = useTokenMetadataUriBatch({
chainId,
contractAddress,
tokenIds,
enabled,
});
const { data: tokenIdToUriMapping, ...tokenIdToUriMappingState } =
useTokenMetadataUriBatch({
chainId,
contractAddress,
tokenIds,
enabled,
});

const mergedStates = useMergeQueryStates([
collectionTokensState,
collectionBaseUriState,
collectionFallbackUriState,
tokenIdToMaxSupplyMappingState,
tokenIdToUriMappingState,
]);

const combinedTokenIds = useMemo(() => {
const combinedTokenIds: string[] =
Expand Down Expand Up @@ -138,14 +132,7 @@ export const useERC1155TokensByCollectionContractOnly = ({
]);

return {
...mergedStates,
data: tokens,
error:
collectionTokensError ||
tokenIdToMaxSupplyMappingError ||
tokenIdToUriMappingError,
isLoading:
collectionTokensLoading ||
tokenIdToMaxSupplyMappingLoading ||
tokenIdToUriMappingLoading,
} as const;
};
1 change: 0 additions & 1 deletion packages/react/src/react-app-env.d.ts

This file was deleted.

0 comments on commit d7fa20e

Please sign in to comment.