Disable submissions without eating the comment after 1h

This commit is contained in:
Austin Chen 2022-03-16 19:55:03 -07:00
parent 01884bd3df
commit 94f6f08784
2 changed files with 5 additions and 13 deletions

View File

@ -25,11 +25,7 @@ import {
import { useUser } from '../../hooks/use-user' import { useUser } from '../../hooks/use-user'
import { Linkify } from '../linkify' import { Linkify } from '../linkify'
import { Row } from '../layout/row' import { Row } from '../layout/row'
import { import { createComment, MAX_COMMENT_LENGTH } from '../../lib/firebase/comments'
canAddComment,
createComment,
MAX_COMMENT_LENGTH,
} from '../../lib/firebase/comments'
import { formatMoney } from '../../../common/util/format' import { formatMoney } from '../../../common/util/format'
import { Comment } from '../../../common/comment' import { Comment } from '../../../common/comment'
import { ResolutionOrChance } from '../contract-card' import { ResolutionOrChance } from '../contract-card'
@ -180,11 +176,11 @@ function FeedBet(props: {
const isSelf = user?.id === userId const isSelf = user?.id === userId
// You can comment if your bet was posted in the last hour // You can comment if your bet was posted in the last hour
const canComment = canAddComment(createdTime, isSelf) const canComment = isSelf && Date.now() - createdTime < 60 * 60 * 1000
const [comment, setComment] = useState('') const [comment, setComment] = useState('')
async function submitComment() { async function submitComment() {
if (!user || !comment) return if (!user || !comment || !canComment) return
await createComment(contract.id, id, comment, user) await createComment(contract.id, id, comment, user)
} }
@ -219,8 +215,7 @@ function FeedBet(props: {
</> </>
)} )}
<RelativeTimestamp time={createdTime} /> <RelativeTimestamp time={createdTime} />
{canComment && ( {(canComment || comment) && (
// Allow user to comment in an textarea if they are the creator
<div className="mt-2"> <div className="mt-2">
<Textarea <Textarea
value={comment} value={comment}
@ -238,6 +233,7 @@ function FeedBet(props: {
<button <button
className="btn btn-outline btn-sm mt-1" className="btn btn-outline btn-sm mt-1"
onClick={submitComment} onClick={submitComment}
disabled={!canComment}
> >
Comment Comment
</button> </button>

View File

@ -40,10 +40,6 @@ export async function createComment(
return await setDoc(ref, comment) return await setDoc(ref, comment)
} }
export const canAddComment = (createdTime: number, isSelf: boolean) => {
return isSelf && Date.now() - createdTime < 60 * 60 * 1000
}
function getCommentsCollection(contractId: string) { function getCommentsCollection(contractId: string) {
return collection(db, 'contracts', contractId, 'comments') return collection(db, 'contracts', contractId, 'comments')
} }