|
| 1 | +import { useCallback, useEffect, useState } from 'react'; |
| 2 | +import { DateTime } from 'luxon'; |
| 3 | +import { stringify } from 'viem'; |
| 4 | +import { useConfig } from '../providers/ConfigProvider/ConfigProviderContext'; |
| 5 | +import { useNode } from '../providers/NodeProvider/NodeProviderContext'; |
| 6 | +import { DealRecord, GenericOfferOptions, GenericQuery } from '../../../../src/shared/types'; |
| 7 | +import { centerEllipsis, formatBalance } from '../../../react-libs/src/utils'; |
| 8 | +import { DealStatus } from '../../../../src/shared/contracts'; |
| 9 | + |
| 10 | +/** |
| 11 | + * Deals table |
| 12 | + */ |
| 13 | +export const Deals = () => { |
| 14 | + const { isAuth } = useConfig(); |
| 15 | + const { node } = useNode(); |
| 16 | + const [dealStates, setDealStates] = useState<Record<string, DealStatus>>({}); |
| 17 | + const [deals, setDeals] = useState<DealRecord<GenericQuery, GenericOfferOptions>[]>([]); |
| 18 | + const [message, setMessage] = useState<string | undefined>(); |
| 19 | + const [error, setError] = useState<string | undefined>(); |
| 20 | + |
| 21 | + const getDeals = useCallback(async () => { |
| 22 | + setError(undefined); |
| 23 | + setMessage(undefined); |
| 24 | + |
| 25 | + if (!node) { |
| 26 | + return; |
| 27 | + } |
| 28 | + const records = await node.deals.getAll.query({}); |
| 29 | + console.log('Deals:', records); |
| 30 | + setDeals(records); |
| 31 | + }, [node]); |
| 32 | + |
| 33 | + useEffect(() => { |
| 34 | + if (deals && deals.length > 0) { |
| 35 | + const newDealStates: Record<string, DealStatus> = {}; |
| 36 | + deals.forEach((d) => { |
| 37 | + newDealStates[d.offer.id] = d.status; |
| 38 | + }); |
| 39 | + setDealStates(newDealStates); |
| 40 | + } |
| 41 | + }, [deals]); |
| 42 | + |
| 43 | + if (!isAuth) { |
| 44 | + return null; |
| 45 | + } |
| 46 | + |
| 47 | + return ( |
| 48 | + <div> |
| 49 | + <div> |
| 50 | + <button onClick={getDeals}> |
| 51 | + Load deals |
| 52 | + </button> |
| 53 | + </div> |
| 54 | + <div style={{ marginTop: 20 }}> |
| 55 | + <table border={1} cellPadding={5}> |
| 56 | + <thead> |
| 57 | + <tr> |
| 58 | + <td>Offer Id</td> |
| 59 | + <td>Created</td> |
| 60 | + <td>Options</td> |
| 61 | + <td>Buyer</td> |
| 62 | + <td>Price</td> |
| 63 | + <td>Status</td> |
| 64 | + <td>Action</td> |
| 65 | + </tr> |
| 66 | + </thead> |
| 67 | + <tbody> |
| 68 | + {deals.map((d, index) => ( |
| 69 | + <tr key={index}> |
| 70 | + <td>{centerEllipsis(d.offer.payload.id)}</td> |
| 71 | + <td>{DateTime.fromSeconds(Number(d.created)).toISODate()}</td> |
| 72 | + <td>{stringify(d.offer.options)}</td> |
| 73 | + <td>{centerEllipsis(d.buyer)}</td> |
| 74 | + <td>{formatBalance(d.price, 4)}</td> |
| 75 | + <td |
| 76 | + style={{ |
| 77 | + color: dealStates[d.offer.id] === 1 ? 'green' : 'red', |
| 78 | + }} |
| 79 | + > |
| 80 | + {DealStatus[dealStates[d.offer.id]]} |
| 81 | + </td> |
| 82 | + <td></td> |
| 83 | + </tr> |
| 84 | + ))} |
| 85 | + </tbody> |
| 86 | + </table> |
| 87 | + </div> |
| 88 | + </div> |
| 89 | + ); |
| 90 | +}; |
0 commit comments