diff --git a/web/components/answers/create-answer-panel.tsx b/web/components/answers/create-answer-panel.tsx index 2a089f50..6eeadf97 100644 --- a/web/components/answers/create-answer-panel.tsx +++ b/web/components/answers/create-answer-panel.tsx @@ -1,6 +1,7 @@ import clsx from 'clsx' import { useState } from 'react' import Textarea from 'react-expanding-textarea' +import { findBestMatch } from 'string-similarity' import { FreeResponseContract } from 'common/contract' import { BuyAmountInput } from '../amount-input' @@ -23,6 +24,7 @@ import { firebaseLogin } from 'web/lib/firebase/users' import { Bet } from 'common/bet' import { MAX_ANSWER_LENGTH } from 'common/answer' import { withTracking } from 'web/lib/service/analytics' +import { lowerCase } from 'lodash' export function CreateAnswerPanel(props: { contract: FreeResponseContract }) { const { contract } = props @@ -30,9 +32,15 @@ export function CreateAnswerPanel(props: { contract: FreeResponseContract }) { const [text, setText] = useState('') const [betAmount, setBetAmount] = useState(10) const [amountError, setAmountError] = useState() + const [answerError, setAnswerError] = useState() + const [possibleDuplicateAnswer, setPossibleDuplicateAnswer] = useState< + string | undefined + >() const [isSubmitting, setIsSubmitting] = useState(false) + const { answers } = contract - const canSubmit = text && betAmount && !amountError && !isSubmitting + const canSubmit = + text && betAmount && !amountError && !isSubmitting && !answerError const submitAnswer = async () => { if (canSubmit) { @@ -54,6 +62,36 @@ export function CreateAnswerPanel(props: { contract: FreeResponseContract }) { } } + const changeAnswer = (text: string) => { + setText(text) + const existingAnswer = answers.find( + (a) => lowerCase(a.text) === lowerCase(text) + ) + + if (existingAnswer) { + setAnswerError( + existingAnswer + ? `"${existingAnswer.text}" already exists as an answer` + : '' + ) + return + } else { + setAnswerError('') + } + + if (answers.length && text) { + const matches = findBestMatch( + lowerCase(text), + answers.map((a) => lowerCase(a.text)) + ) + setPossibleDuplicateAnswer( + matches.bestMatch.rating > 0.8 + ? answers[matches.bestMatchIndex].text + : '' + ) + } + } + const resultProb = getDpmOutcomeProbabilityAfterBet( contract.totalShares, 'new', @@ -79,12 +117,21 @@ export function CreateAnswerPanel(props: { contract: FreeResponseContract }) {
Add your answer