From 561a7e2c7509e2bb81cc600974697bc0bf945d8a Mon Sep 17 00:00:00 2001 From: James Grugett Date: Sun, 20 Feb 2022 22:04:00 -0600 Subject: [PATCH] Allow editing the close time --- firestore.rules | 2 +- web/components/contract-card.tsx | 96 ++++++++++++++++++++++++---- web/components/contract-overview.tsx | 2 +- web/components/datetime-tooltip.tsx | 2 +- 4 files changed, 86 insertions(+), 16 deletions(-) diff --git a/firestore.rules b/firestore.rules index 385e2f44..8c419b01 100644 --- a/firestore.rules +++ b/firestore.rules @@ -25,7 +25,7 @@ service cloud.firestore { match /contracts/{contractId} { allow read; allow update: if request.resource.data.diff(resource.data).affectedKeys() - .hasOnly(['description', 'tags', 'lowercaseTags']); + .hasOnly(['description', 'closeTime', 'tags', 'lowercaseTags']); allow update: if isAdmin(); allow delete: if resource.data.creatorId == request.auth.uid; } diff --git a/web/components/contract-card.tsx b/web/components/contract-card.tsx index 501987c8..7491efbe 100644 --- a/web/components/contract-card.tsx +++ b/web/components/contract-card.tsx @@ -1,5 +1,7 @@ import clsx from 'clsx' import Link from 'next/link' +import { ClockIcon, DatabaseIcon, PencilIcon } from '@heroicons/react/outline' +import { TrendingUpIcon } from '@heroicons/react/solid' import { Row } from '../components/layout/row' import { formatMoney } from '../../common/util/format' import { UserLink } from './user-page' @@ -8,15 +10,15 @@ import { contractMetrics, contractPath, getBinaryProbPercent, + updateContract, } from '../lib/firebase/contracts' import { Col } from './layout/col' import dayjs from 'dayjs' -import { TrendingUpIcon } from '@heroicons/react/solid' import { DateTimeTooltip } from './datetime-tooltip' -import { ClockIcon, DatabaseIcon } from '@heroicons/react/outline' import { fromNow } from '../lib/util/time' import { Avatar } from './avatar' import { Spacer } from './layout/spacer' +import { useState } from 'react' export function ContractCard(props: { contract: Contract @@ -160,8 +162,11 @@ function AbbrContractDetails(props: { ) } -export function ContractDetails(props: { contract: Contract }) { - const { contract } = props +export function ContractDetails(props: { + contract: Contract + isCreator?: boolean +}) { + const { contract, isCreator } = props const { closeTime, creatorName, creatorUsername } = contract const { truePool, createdDate, resolvedDate } = contractMetrics(contract) @@ -202,15 +207,12 @@ export function ContractDetails(props: { contract: Contract }) { {!resolvedDate && closeTime && ( <> - {' - '} - Date.now() ? 'Trading ends:' : 'Trading ended:' - } - time={closeTime} - > - {dayjs(closeTime).format('MMM D')} ({fromNow(closeTime)}) - + {' - '}{' '} + )} @@ -243,3 +245,71 @@ export function contractTextDetails(contract: Contract) { (hashtags.length > 0 ? ` • ${hashtags.join(' ')}` : '') ) } + +function EditableCloseDate(props: { + closeTime: number + contract: Contract + isCreator: boolean +}) { + const { closeTime, contract, isCreator } = props + + const [isEditingCloseTime, setIsEditingCloseTime] = useState(false) + const [closeDate, setCloseDate] = useState( + closeTime && dayjs(closeTime).format('YYYY-MM-DDT23:59') + ) + + const onSave = () => { + const newCloseTime = dayjs(closeDate).valueOf() + if (newCloseTime === closeTime) setIsEditingCloseTime(false) + else if (newCloseTime > Date.now()) { + const { description } = contract + const formattedCloseDate = dayjs(newCloseTime).format('YYYY-MM-DD h:mm a') + const newDescription = `${description}\n\nClose date updated to ${formattedCloseDate}` + + updateContract(contract.id, { + closeTime: newCloseTime, + description: newDescription, + }) + + setIsEditingCloseTime(false) + } + } + + return ( + <> + {isEditingCloseTime ? ( +
+ e.stopPropagation()} + onChange={(e) => setCloseDate(e.target.value || '')} + min={Date.now()} + value={closeDate} + /> +
+ ) : ( + Date.now() ? 'Trading ends:' : 'Trading ended:'} + time={closeTime} + > + {dayjs(closeTime).format('MMM D')} ({fromNow(closeTime)}) + + )} + + {isCreator && + (isEditingCloseTime ? ( + + ) : ( + + ))} + + ) +} diff --git a/web/components/contract-overview.tsx b/web/components/contract-overview.tsx index c7059b22..d8a81c35 100644 --- a/web/components/contract-overview.tsx +++ b/web/components/contract-overview.tsx @@ -66,7 +66,7 @@ export const ContractOverview = (props: { )} - + {(isBinary || resolution) && ( diff --git a/web/components/datetime-tooltip.tsx b/web/components/datetime-tooltip.tsx index 69751eb6..6b8e5216 100644 --- a/web/components/datetime-tooltip.tsx +++ b/web/components/datetime-tooltip.tsx @@ -25,7 +25,7 @@ export function DateTimeTooltip(props: { > {props.children} - {props.children} + {props.children} ) }