2022-09-13 23:16:07 +00:00
|
|
|
import { Contract } from 'common/contract'
|
|
|
|
import { useState } from 'react'
|
|
|
|
import { Button } from './button'
|
|
|
|
import { ContractSearch } from './contract-search'
|
|
|
|
import { Col } from './layout/col'
|
|
|
|
import { Modal } from './layout/modal'
|
|
|
|
import { Row } from './layout/row'
|
|
|
|
import { LoadingIndicator } from './loading-indicator'
|
|
|
|
|
|
|
|
export function SelectMarketsModal(props: {
|
|
|
|
title: string
|
|
|
|
description?: React.ReactNode
|
|
|
|
open: boolean
|
|
|
|
setOpen: (open: boolean) => void
|
|
|
|
submitLabel: (length: number) => string
|
|
|
|
onSubmit: (contracts: Contract[]) => void | Promise<void>
|
|
|
|
contractSearchOptions?: Partial<Parameters<typeof ContractSearch>[0]>
|
|
|
|
}) {
|
|
|
|
const {
|
|
|
|
title,
|
|
|
|
description,
|
|
|
|
open,
|
|
|
|
setOpen,
|
|
|
|
submitLabel,
|
|
|
|
onSubmit,
|
|
|
|
contractSearchOptions,
|
|
|
|
} = props
|
|
|
|
|
|
|
|
const [contracts, setContracts] = useState<Contract[]>([])
|
|
|
|
const [loading, setLoading] = useState(false)
|
|
|
|
|
|
|
|
async function addContract(contract: Contract) {
|
|
|
|
if (contracts.map((c) => c.id).includes(contract.id)) {
|
|
|
|
setContracts(contracts.filter((c) => c.id !== contract.id))
|
|
|
|
} else setContracts([...contracts, contract])
|
|
|
|
}
|
|
|
|
|
|
|
|
async function onFinish() {
|
|
|
|
setLoading(true)
|
|
|
|
await onSubmit(contracts)
|
|
|
|
setLoading(false)
|
|
|
|
setOpen(false)
|
|
|
|
setContracts([])
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Modal open={open} setOpen={setOpen} className={'sm:p-0'} size={'lg'}>
|
|
|
|
<Col className="h-[85vh] w-full gap-4 rounded-md bg-white">
|
|
|
|
<div className="p-8 pb-0">
|
|
|
|
<Row>
|
|
|
|
<div className={'text-xl text-indigo-700'}>{title}</div>
|
|
|
|
|
|
|
|
{!loading && (
|
|
|
|
<Row className="grow justify-end gap-4">
|
|
|
|
{contracts.length > 0 && (
|
|
|
|
<Button onClick={onFinish} color="indigo">
|
|
|
|
{submitLabel(contracts.length)}
|
|
|
|
</Button>
|
|
|
|
)}
|
|
|
|
<Button
|
|
|
|
onClick={() => {
|
|
|
|
if (contracts.length > 0) {
|
|
|
|
setContracts([])
|
|
|
|
} else {
|
|
|
|
setOpen(false)
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
color="gray"
|
|
|
|
>
|
|
|
|
{contracts.length > 0 ? 'Reset' : 'Cancel'}
|
|
|
|
</Button>
|
|
|
|
</Row>
|
|
|
|
)}
|
|
|
|
</Row>
|
|
|
|
{description}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
{loading && (
|
|
|
|
<div className="w-full justify-center">
|
|
|
|
<LoadingIndicator />
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
|
2022-09-16 13:32:15 +00:00
|
|
|
<div className="overflow-y-auto px-2 sm:px-8">
|
2022-09-13 23:16:07 +00:00
|
|
|
<ContractSearch
|
|
|
|
hideOrderSelector
|
|
|
|
onContractClick={addContract}
|
2022-09-15 15:51:52 +00:00
|
|
|
cardUIOptions={{
|
|
|
|
hideGroupLink: true,
|
|
|
|
hideQuickBet: true,
|
|
|
|
noLinkAvatar: true,
|
|
|
|
}}
|
2022-09-13 23:16:07 +00:00
|
|
|
highlightOptions={{
|
2022-10-02 23:02:31 +00:00
|
|
|
itemIds: contracts.map((c) => c.id),
|
2022-09-13 23:16:07 +00:00
|
|
|
highlightClassName:
|
|
|
|
'!bg-indigo-100 outline outline-2 outline-indigo-300',
|
|
|
|
}}
|
|
|
|
additionalFilter={{}} /* hide pills */
|
2022-09-16 13:32:15 +00:00
|
|
|
headerClassName="bg-white sticky"
|
2022-09-13 23:16:07 +00:00
|
|
|
{...contractSearchOptions}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</Col>
|
|
|
|
</Modal>
|
|
|
|
)
|
|
|
|
}
|