Skip to content

Commit

Permalink
refactor: use current price query
Browse files Browse the repository at this point in the history
  • Loading branch information
secondl1ght committed Sep 13, 2024
1 parent 6593f68 commit f9ce555
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 18 deletions.
97 changes: 97 additions & 0 deletions src/graphql/queries/__generated__/prices.generated.tsx

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions src/graphql/queries/prices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,17 @@ export const getPricesHistorical = gql`
}
}
`;

export const getPriceCurrent = gql`
query getPriceCurrent {
prices {
current {
currency
date
id
value
}
id
}
}
`;
1 change: 1 addition & 0 deletions src/graphql/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ export type PricePoint = {

export type PriceQueries = {
__typename?: 'PriceQueries';
current: PricePoint;
historical: PriceHistorical;
id: Scalars['String']['output'];
};
Expand Down
21 changes: 3 additions & 18 deletions src/views/wallet/Receive.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { sub } from 'date-fns';
import { ArrowLeft, ArrowUpDown, ChevronsUpDown } from 'lucide-react';
import Link from 'next/link';
import { useTranslations } from 'next-intl';
Expand All @@ -17,7 +16,7 @@ import { Skeleton } from '@/components/ui/skeleton';
import { useToast } from '@/components/ui/use-toast';
import { useCreateLightningInvoiceMutation } from '@/graphql/mutations/__generated__/createInvoice.generated';
import { useCreateOnchainAddressMutation } from '@/graphql/mutations/__generated__/createOnchainAddress.generated';
import { useGetPricesHistoricalQuery } from '@/graphql/queries/__generated__/prices.generated';
import { useGetPriceCurrentQuery } from '@/graphql/queries/__generated__/prices.generated';
import {
useGetWalletDetailsQuery,
useGetWalletQuery,
Expand Down Expand Up @@ -186,19 +185,11 @@ export const Receive = () => {
}
}, [receive, bancoCode, receiveString, liquidAddress, t]);

const from_date = useMemo(
() => sub(new Date(), { days: 1 }).toISOString(),
[]
);

const {
data: priceData,
loading: priceLoading,
error: priceError,
} = useGetPricesHistoricalQuery({
variables: {
input: { from_date },
},
} = useGetPriceCurrentQuery({
onError: err => {
const messages = handleApolloError(err);

Expand All @@ -210,13 +201,7 @@ export const Receive = () => {
},
});

const latestPrice = useMemo(
() =>
priceData?.prices.historical.points
.filter(p => p.value !== null)
.toSorted((a, b) => Date.parse(b.date) - Date.parse(a.date))[0].value,
[priceData]
);
const latestPrice = priceData?.prices.current.value;

const loading =
detailsLoading ||
Expand Down

0 comments on commit f9ce555

Please sign in to comment.