Skip to content

Commit

Permalink
getsales data when token is on sale
Browse files Browse the repository at this point in the history
  • Loading branch information
sstraatemans committed Oct 25, 2024
1 parent 869b987 commit 712acb8
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/apps/marmalade-marketplace/src/pages/api/cron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ export default async function handler(
}

const blocksToProcess = Math.min(
10,
1000,
latestBlockNumber - latestProcessedBlockNumber,
);
await sync(
Expand Down
27 changes: 26 additions & 1 deletion packages/apps/marmalade-marketplace/src/pages/mytokens/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ListingHeader } from '@/components/ListingHeader';
import { Token } from '@/components/Token';
import { getTokens, NonFungibleTokenBalance } from '@/graphql/queries/client';
import { useAccount } from '@/hooks/account';
import { database } from '@/utils/firebase';
import { ChainId } from '@kadena/client';
import {
Badge,
Expand All @@ -11,13 +12,36 @@ import {
ProgressCircle,
Stack,
} from '@kadena/kode-ui';
import { collection, getDocs, query, where } from 'firebase/firestore';
import { useEffect, useState } from 'react';

const MyTokens = () => {
const [tokens, setTokens] = useState<Array<NonFungibleTokenBalance>>([]);
const [onSaleTokens, setOnSaleTokens] = useState<
Array<NonFungibleTokenBalance>
>([]);
const [isLoading, setIsLoading] = useState<boolean>(false);
const { account } = useAccount();

const fetchOnSaleTokens = async (accountName?: string) => {
if (accountName) {
//const querySnapshot = await getDocs(query(collection(database, 'sales')));
const salesRef = collection(database, 'sales');
const q = await query(
salesRef,
where('seller.account', '==', account?.accountName),
);
const querySnapshot = await getDocs(q);

console.log(querySnapshot);
const docs: NonFungibleTokenBalance[] = [];
querySnapshot.forEach((doc) => {
docs.push(doc.data() as NonFungibleTokenBalance);
});
setOnSaleTokens(docs);
}
};

const fetchTokens = async (accountName?: string) => {
if (accountName) {
const tokens = await getTokens(accountName);
Expand All @@ -29,6 +53,7 @@ const MyTokens = () => {
useEffect(() => {
setIsLoading(true);
fetchTokens(account?.accountName);
fetchOnSaleTokens(account?.accountName);
}, [account?.accountName]);

console.log({ tokens });
Expand Down Expand Up @@ -60,7 +85,7 @@ const MyTokens = () => {
{isLoading ? (
<ProgressCircle size="lg" isIndeterminate />
) : (
tokens.map((token) => (
[...tokens, ...onSaleTokens].map((token) => (
<GridItem key={token.tokenId}>
<Token
tokenId={token.tokenId}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,32 @@ import { useParams, useSearchParams } from 'next/navigation';
import { useEffect, useState } from 'react';

//styles
import { Sale } from '@/hooks/getSales';
import { layoutClass } from '@/styles/layout.css';
import { database } from '@/utils/firebase';
import {
CardContentBlock,
CardFixedContainer,
CardFooterGroup,
} from '@kadena/kode-ui/patterns';
import { collection, getDocs, query, where } from 'firebase/firestore';
import * as styles from '../../../styles/token.css';

interface ITokenInfo {
id: string;
policies: Policy[];
precision: { int: string };
supply: number;
uri: string;
}

const TokenComponent = () => {
const params = useParams();
const account = useAccount();
const [balance, setBalance] = useState<number>(0);
const [tokenId, setTokenId] = useState<string>('');
const [saleId, setSaleId] = useState<string | null>(null);
const [saleData, setSaleData] = useState<Sale | null>(null);
const [chainId, setChainId] = useState<string>('');
const [tokenInfo, setTokenInfo] = useState<ITokenInfo | null>();
const [tokenImageUrl, setTokenImageUrl] = useState<string>('/no-image.webp');
Expand All @@ -52,13 +64,30 @@ const TokenComponent = () => {

const [selectedKey, setSelectedKey] = useState(saleId ? 'bid' : 'info');

interface ITokenInfo {
id: string;
policies: Policy[];
precision: { int: string };
supply: number;
uri: string;
}
const fetchOnSaleTokens = async (tokenId?: string) => {
if (tokenId) {
//const querySnapshot = await getDocs(query(collection(database, 'sales')));
const salesRef = collection(database, 'sales');
const q = query(salesRef, where('tokenId', '==', tokenId));
const querySnapshot = await getDocs(q);

console.log(querySnapshot);
const docs: Sale[] = [];
querySnapshot.forEach((doc) => {
docs.push(doc.data() as Sale);
});

console.log(docs[0]);
if (docs.length) {
setSaleData(docs[0]);
}
}
};

useEffect(() => {
if (!tokenId) return;
fetchOnSaleTokens(tokenId);
}, [tokenId]);

useEffect(() => {
async function fetch() {
Expand Down Expand Up @@ -216,15 +245,15 @@ const TokenComponent = () => {
tokenPrecision={tokenPrecision}
account={account}
policyConfig={policyConfig}
saleId={saleId!}
saleId={saleData?.saleId!}
/>
</Stack>
</TabItem>
{/* only show bid tab if saleId is present*/}

<TabItem title="Buy" key="bid">
<Stack className={styles.flexContainer}>
<Bid saleId={saleId!} tokenImageUrl={tokenImageUrl} />
<Bid saleId={saleData?.saleId!} tokenImageUrl={tokenImageUrl} />
</Stack>
</TabItem>

Expand Down

0 comments on commit 712acb8

Please sign in to comment.