From 7b55eeff881b4e93af1a46abe6c74b8756a8d330 Mon Sep 17 00:00:00 2001 From: Austin Chen Date: Tue, 14 Dec 2021 02:27:22 -0800 Subject: [PATCH] Move shared logic into contracts-list --- web/components/contract-overview.tsx | 2 +- web/components/contracts-list.tsx | 76 ++++++++++++++++++++++++- web/pages/markets.tsx | 85 +--------------------------- 3 files changed, 77 insertions(+), 86 deletions(-) diff --git a/web/components/contract-overview.tsx b/web/components/contract-overview.tsx index 3add4ce4..1bdf3946 100644 --- a/web/components/contract-overview.tsx +++ b/web/components/contract-overview.tsx @@ -3,7 +3,7 @@ import { compute, Contract } from '../lib/firebase/contracts' import { Col } from './layout/col' import { Spacer } from './layout/spacer' import { ContractProbGraph } from './contract-prob-graph' -import { ContractDetails } from '../pages/markets' +import { ContractDetails } from './contracts-list' export const ContractOverview = (props: { contract: Contract diff --git a/web/components/contracts-list.tsx b/web/components/contracts-list.tsx index 3e90d14f..e8ab2160 100644 --- a/web/components/contracts-list.tsx +++ b/web/components/contracts-list.tsx @@ -1,7 +1,79 @@ +import Link from 'next/link' +import { Col } from '../components/layout/col' +import { Row } from '../components/layout/row' import { useEffect, useState } from 'react' import { useUser } from '../hooks/use-user' -import { Contract, listContracts } from '../lib/firebase/contracts' -import { ContractsGrid } from '../pages/markets' +import { compute, Contract, listContracts } from '../lib/firebase/contracts' +import { formatWithCommas } from '../lib/util/format' + +export function ContractDetails(props: { contract: Contract }) { + const { contract } = props + const { volume, createdDate } = compute(contract) + + return ( + +
By {contract.creatorName}
+
+
{createdDate}
+
+
{formatWithCommas(volume)} vol
+
+ ) +} + +function ContractCard(props: { contract: Contract }) { + const { contract } = props + const { probPercent } = compute(contract) + + return ( + + +
  • +
    +
    +
    + {/* Left side of card */} +
    +

    + {contract.question} +

    + +
    + + {/* Right side of card */} + + + {contract.resolution || ( +
    + {probPercent} +
    chance
    +
    + )} + + +
    +
    +
    +
  • +
    + + ) +} + +export function ContractsGrid(props: { contracts: Contract[] }) { + const { contracts } = props + return ( + + ) +} export function ContractsList(props: {}) { const creator = useUser() diff --git a/web/pages/markets.tsx b/web/pages/markets.tsx index ff3315d2..047316f0 100644 --- a/web/pages/markets.tsx +++ b/web/pages/markets.tsx @@ -1,81 +1,9 @@ -import Link from 'next/link' import React, { useEffect, useState } from 'react' +import { ContractsGrid } from '../components/contracts-list' import { Header } from '../components/header' -import { Col } from '../components/layout/col' -import { Row } from '../components/layout/row' import { Title } from '../components/title' import { compute, listAllContracts } from '../lib/firebase/contracts' import { Contract } from '../lib/firebase/contracts' -import { formatWithCommas } from '../lib/util/format' - -export function ContractDetails(props: { contract: Contract }) { - const { contract } = props - const { volume, createdDate } = compute(contract) - - return ( - -
    By {contract.creatorName}
    -
    -
    {createdDate}
    -
    -
    {formatWithCommas(volume)} vol
    -
    - ) -} - -function ContractCard(props: { contract: Contract }) { - const { contract } = props - const { probPercent } = compute(contract) - - return ( - - -
  • -
    -
    -
    - {/* Left side of card */} -
    -

    - {contract.question} -

    - -
    - - {/* Right side of card */} - - - {contract.resolution || ( -
    - {probPercent} -
    chance
    -
    - )} - - -
    -
    -
    -
  • -
    - - ) -} - -export function ContractsGrid(props: { contracts: Contract[] }) { - const { contracts } = props - return ( - - ) -} export default function Markets() { const [contracts, setContracts] = useState([]) @@ -94,19 +22,10 @@ export default function Markets() { (c) => check(c.question) || check(c.description) || check(c.creatorName) ) - function volume(contract: Contract) { - return ( - contract.pot.YES + - contract.pot.NO - - contract.seedAmounts.YES - - contract.seedAmounts.NO - ) - } - if (sort === 'createdTime') { matches.sort((a, b) => b.createdTime - a.createdTime) } else if (sort === 'volume') { - matches.sort((a, b) => volume(b) - volume(a)) + matches.sort((a, b) => compute(b).volume - compute(a).volume) } return (