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

fix: refactor batches fetching #2152

Merged
merged 9 commits into from
Oct 9, 2023
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 @@ -16,7 +16,7 @@ import { getBridgeTxStatusQuery } from 'lib/queries/react-query/bridge/getBridge
import { getGetTxsEventQuery } from 'lib/queries/react-query/cosmos/bank/getTxsEventQuery/getTxsEventQuery';
import { getAllSanityCreditClassesQuery } from 'lib/queries/react-query/sanity/getAllCreditClassesQuery/getAllCreditClassesQuery';

import { useBatchesWithMetadata } from 'hooks/batches/useBatchesWithMetadata';
import { useFetchBatchesWithMetadata } from 'hooks/batches/useFetchBatchesWithMetadata';

import {
BRIDGED_STATUSES,
Expand Down Expand Up @@ -148,7 +148,7 @@ export const useFetchBridgedEcocredits = ({ address }: Props): Output => {
isProjectsMetadataLoading,
classesMetadata,
isClassesMetadataLoading,
} = useBatchesWithMetadata(credits);
} = useFetchBatchesWithMetadata(credits);

// Normalization
// isLoading -> undefined: return empty strings in normalizer to trigger skeleton
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import {
creditBatchesHeadCells,
} from './CreditBatches.config';
import { useCreditBatchesStyles } from './CreditBatches.styles';
import { useFetchCreditBatches } from './hooks/useFetchCreditBatches';

interface CreditBatchProps {
creditClassId?: string | null;
Expand Down Expand Up @@ -58,10 +57,6 @@ const CreditBatches: React.FC<React.PropsWithChildren<CreditBatchProps>> = ({
sx,
}) => {
const { classes } = useCreditBatchesStyles();
const { batchesWithSupply } = useFetchCreditBatches({
creditBatches,
creditClassId,
});

let columnsToShow = [...creditBatchesHeadCells];

Expand All @@ -78,13 +73,13 @@ const CreditBatches: React.FC<React.PropsWithChildren<CreditBatchProps>> = ({
);
}

const someTx = batchesWithSupply?.some(batch => batch.txhash);
const someTx = creditBatches?.some(batch => batch.txhash);

if (!someTx) {
columnsToShow = columnsToShow.filter(column => column.id !== 'txhash');
}

if (!batchesWithSupply?.length) {
if (!creditBatches?.length) {
return (
<NoCredits
title="No credits issued"
Expand Down Expand Up @@ -126,7 +121,7 @@ const CreditBatches: React.FC<React.PropsWithChildren<CreditBatchProps>> = ({
initialPaginationParams={initialPaginationParams}
isIgnoreOffset={isIgnoreOffset}
sx={sx}
rows={batchesWithSupply.map(batch => {
rows={creditBatches.map(batch => {
/* eslint-disable react/jsx-key */
let result = [];
if (someTx) {
Expand Down Expand Up @@ -215,7 +210,7 @@ const CreditBatches: React.FC<React.PropsWithChildren<CreditBatchProps>> = ({
/>
);

return batchesWithSupply.length > 0 ? (
return creditBatches.length > 0 ? (
withSection ? (
<Section
classes={{ root: classes.section, title: classes.title }}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React, { useMemo } from 'react';
import Grid from '@mui/material/Grid';

import { useFetchCreditBatches } from '../../../hooks/batches/useFetchCreditBatches';
import { Statistic } from '../../molecules';
import { useFetchCreditBatches } from '../CreditBatches/hooks/useFetchCreditBatches';
import { useCreditTotalsStyles } from './CreditTotals.styles';
import { sumBatchTotals } from './CreditTotals.utils';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,33 @@ export const parseNumber = (value: any): number => {
export const sumBatchTotals = (
batches: BatchInfoWithSupply[],
): CreditTotalData => {
let tradeable = 0;
let retired = 0;
let created = 0;
let tradeable: number | undefined = undefined;
let retired: number | undefined = undefined;
let created: number | undefined = undefined;

batches.forEach(batch => {
const batchTradable = parseNumber(batch.tradableAmount);
const batchRetired = parseNumber(batch.retiredAmount);
const batchCancelled = parseNumber(batch.cancelledAmount);
tradeable += batchTradable;
retired += batchRetired;
created += batchTradable + batchRetired + batchCancelled;
});
if (batches.length > 0) {
batches.forEach(batch => {
const batchTradable = parseNumber(batch.tradableAmount);
const batchRetired = parseNumber(batch.retiredAmount);
const batchCancelled = parseNumber(batch.cancelledAmount);
tradeable = (tradeable ?? 0) + batchTradable;
retired = (retired ?? 0) + batchRetired;
created = (created ?? 0) + batchTradable + batchRetired + batchCancelled;
});
}

return {
tradeable: formatNumber({
num: tradeable,
...quantityFormatNumberOptions,
}),
retired: formatNumber({ num: retired, ...quantityFormatNumberOptions }),
created: formatNumber({ num: created, ...quantityFormatNumberOptions }),
tradeable: tradeable
? formatNumber({
num: tradeable,
...quantityFormatNumberOptions,
})
: undefined,
retired: retired
? formatNumber({ num: retired, ...quantityFormatNumberOptions })
: undefined,
created: created
? formatNumber({ num: created, ...quantityFormatNumberOptions })
: undefined,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import { useAllSoldOutProjectsIds } from 'components/organisms/ProjectCardsSecti
import { ProjectStorySection } from 'components/organisms/ProjectStorySection/ProjectStorySection';
import { SellOrdersActionsBar } from 'components/organisms/SellOrdersActionsBar/SellOrdersActionsBar';
import { AVG_PRICE_TOOLTIP_PROJECT } from 'components/organisms/SellOrdersActionsBar/SellOrdersActionsBar.constants';
import { usePaginatedBatchesByProject } from 'hooks/batches/usePaginatedBatchesByProject';
import { useFetchPaginatedBatches } from 'hooks/batches/useFetchPaginatedBatches';

import { useLedger } from '../../../ledger';
import { client as sanityClient } from '../../../lib/clients/sanity';
Expand Down Expand Up @@ -224,7 +224,7 @@ function ProjectDetails(): JSX.Element {
const anchoredMetadata = data as AnchoredProjectMetadataLD | undefined;

const { batchesWithSupply, setPaginationParams, paginationParams } =
usePaginatedBatchesByProject({ projectId: String(onChainProjectId) });
useFetchPaginatedBatches({ projectId: String(onChainProjectId) });
const { totals: batchesTotal } = getBatchesTotal(batchesWithSupply ?? []);

const {
Expand Down

This file was deleted.

Loading