Add comment inputs for free response answers

This commit is contained in:
Ian Philips 2022-04-29 16:37:02 -06:00
parent 44c7c3db19
commit 281695bdad
3 changed files with 87 additions and 6 deletions

View File

@ -38,7 +38,11 @@ export function ContractTabs(props: {
bets={bets} bets={bets}
comments={comments} comments={comments}
user={user} user={user}
mode="comments" mode={
contract.outcomeType === 'FREE_RESPONSE'
? 'free-response-comments'
: 'comments'
}
betRowClassName="!mt-0 xl:hidden" betRowClassName="!mt-0 xl:hidden"
/> />
) )

View File

@ -33,6 +33,7 @@ export type CommentInputItem = BaseActivityItem & {
type: 'commentInput' type: 'commentInput'
betsByCurrentUser: Bet[] betsByCurrentUser: Bet[]
comments: Comment[] comments: Comment[]
answerOutcome?: string
} }
export type DescriptionItem = BaseActivityItem & { export type DescriptionItem = BaseActivityItem & {
@ -263,6 +264,64 @@ function getAnswerGroups(
return answerGroups return answerGroups
} }
function getAnswerAndCommentInputGroups(
contract: FullContract<DPM, FreeResponse>,
bets: Bet[],
comments: Comment[],
user: User | undefined | null
) {
let outcomes = _.uniq(bets.map((bet) => bet.outcome)).filter(
(outcome) => getOutcomeProbability(contract, outcome) > 0.0001
)
outcomes = _.sortBy(outcomes, (outcome) =>
getOutcomeProbability(contract, outcome)
)
const answerGroups = outcomes
.map((outcome) => {
const answerBets = bets.filter((bet) => bet.outcome === outcome)
const answerComments = comments.filter((comment) =>
answerBets.some(
(bet) => bet.id === comment.betId || comment.answerOutcome === outcome
)
)
const answer = contract.answers?.find(
(answer) => answer.id === outcome
) as Answer
// Create a list of items for each answer group made up of bets with comments and a comment input
let items = []
items.push({
type: 'commentInput' as const,
id: 'commentInputFor' + outcome,
contract,
betsByCurrentUser: user
? bets.filter((bet) => bet.userId === user.id)
: [],
comments: comments,
answerOutcome: outcome,
})
items.push(
...getCommentsWithPositions(
answerBets,
answerComments,
contract
).reverse()
)
return {
id: outcome,
type: 'answergroup' as const,
contract,
answer,
items,
user,
}
})
.filter((group) => group.answer)
return answerGroups
}
function groupBetsAndComments( function groupBetsAndComments(
bets: Bet[], bets: Bet[],
comments: Comment[], comments: Comment[],
@ -382,7 +441,7 @@ export function getAllContractActivityItems(
) )
) )
items.push({ items.push({
type: 'commentInput', type: 'commentInput' as const,
id: 'commentInput', id: 'commentInput',
contract, contract,
betsByCurrentUser: [], betsByCurrentUser: [],
@ -408,7 +467,7 @@ export function getAllContractActivityItems(
if (outcomeType === 'BINARY') { if (outcomeType === 'BINARY') {
items.push({ items.push({
type: 'commentInput', type: 'commentInput' as const,
id: 'commentInput', id: 'commentInput',
contract, contract,
betsByCurrentUser: [], betsByCurrentUser: [],
@ -479,7 +538,7 @@ export function getSpecificContractActivityItems(
comments: Comment[], comments: Comment[],
user: User | null | undefined, user: User | null | undefined,
options: { options: {
mode: 'comments' | 'bets' mode: 'comments' | 'bets' | 'free-response-comments'
} }
) { ) {
const { mode } = options const { mode } = options
@ -513,6 +572,16 @@ export function getSpecificContractActivityItems(
comments: comments, comments: comments,
}) })
break break
case 'free-response-comments':
items.push(
...getAnswerAndCommentInputGroups(
contract as FullContract<DPM, FreeResponse>,
bets,
comments,
user
)
)
break
} }
return items.reverse() return items.reverse()

View File

@ -16,7 +16,13 @@ export function ContractActivity(props: {
bets: Bet[] bets: Bet[]
comments: Comment[] comments: Comment[]
user: User | null | undefined user: User | null | undefined
mode: 'only-recent' | 'abbreviated' | 'all' | 'comments' | 'bets' mode:
| 'only-recent'
| 'abbreviated'
| 'all'
| 'comments'
| 'bets'
| 'free-response-comments'
contractPath?: string contractPath?: string
className?: string className?: string
betRowClassName?: string betRowClassName?: string
@ -38,7 +44,9 @@ export function ContractActivity(props: {
? getRecentContractActivityItems(contract, bets, comments, user, { ? getRecentContractActivityItems(contract, bets, comments, user, {
contractPath, contractPath,
}) })
: mode === 'comments' || mode === 'bets' : mode === 'comments' ||
mode === 'bets' ||
mode === 'free-response-comments'
? getSpecificContractActivityItems(contract, bets, comments, user, { ? getSpecificContractActivityItems(contract, bets, comments, user, {
mode, mode,
}) })