Skip to content

Commit 1ced8a1

Browse files
authored
Merge pull request #1372 from topcoder-platform/pm-2662_3
fix(PM-2662): Show only current users winning submission in winners tab if challenge is private
2 parents 5163510 + 8fac84b commit 1ced8a1

File tree

1 file changed

+44
-5
lines changed

1 file changed

+44
-5
lines changed

src/apps/review/src/lib/components/TableWinners/TableWinners.tsx

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Table Winners.
33
*/
4-
import { FC, useContext, useMemo } from 'react'
4+
import { FC, useCallback, useContext, useMemo } from 'react'
55
import { Link, useLocation } from 'react-router-dom'
66
import _ from 'lodash'
77
import classNames from 'classnames'
@@ -15,13 +15,14 @@ import { IsRemovingType } from '~/apps/admin/src/lib/models'
1515
import {
1616
ChallengeDetailContextModel,
1717
ProjectResult,
18+
ReviewAppContextModel,
1819
} from '../../models'
1920
import { TableWrapper } from '../TableWrapper'
2021
import { ORDINAL_SUFFIX } from '../../../config/index.config'
21-
import { ChallengeDetailContext } from '../../contexts'
22+
import { ChallengeDetailContext, ReviewAppContext } from '../../contexts'
2223
import { buildPhaseTabs, getHandleUrl } from '../../utils'
2324
import type { PhaseOrderingOptions } from '../../utils'
24-
import { useSubmissionDownloadAccess } from '../../hooks'
25+
import { useRolePermissions, UseRolePermissionsResult, useSubmissionDownloadAccess } from '../../hooks'
2526
import type { UseSubmissionDownloadAccessResult } from '../../hooks/useSubmissionDownloadAccess'
2627
import { CollapsibleAiReviewsRow } from '../CollapsibleAiReviewsRow'
2728

@@ -44,6 +45,8 @@ export const TableWinners: FC<Props> = (props: Props) => {
4445
const isTablet = useMemo(() => screenWidth <= 744, [screenWidth])
4546
const location = useLocation()
4647
const { challengeInfo }: ChallengeDetailContextModel = useContext(ChallengeDetailContext)
48+
const { canViewAllSubmissions }: UseRolePermissionsResult = useRolePermissions()
49+
const { loginUserInfo }: ReviewAppContextModel = useContext(ReviewAppContext)
4750

4851
const phaseOrderingOptions = useMemo<PhaseOrderingOptions>(() => {
4952
const typeName = challengeInfo?.type?.name?.toLowerCase?.() || ''
@@ -56,6 +59,42 @@ export const TableWinners: FC<Props> = (props: Props) => {
5659
}
5760
}, [challengeInfo?.type?.abbreviation, challengeInfo?.type?.name])
5861

62+
const isCompletedDesignChallenge = useMemo(() => {
63+
if (!challengeInfo) return false
64+
const type = challengeInfo.track.name ? String(challengeInfo.track.name)
65+
.toLowerCase() : ''
66+
const status = challengeInfo.status ? String(challengeInfo.status)
67+
.toLowerCase() : ''
68+
return type === 'design' && (
69+
status === 'completed'
70+
)
71+
}, [challengeInfo])
72+
73+
const isSubmissionsViewable = useMemo(() => {
74+
if (!challengeInfo?.metadata?.length) return false
75+
return challengeInfo.metadata.some(m => m.name === 'submissionsViewable' && String(m.value)
76+
.toLowerCase() === 'true')
77+
}, [challengeInfo])
78+
79+
const canViewSubmissions = useMemo(() => {
80+
if (isCompletedDesignChallenge) {
81+
return canViewAllSubmissions || isSubmissionsViewable
82+
}
83+
84+
return true
85+
}, [isCompletedDesignChallenge, isSubmissionsViewable, canViewAllSubmissions])
86+
87+
const filterFunc = useCallback((submissions: ProjectResult[]): ProjectResult[] => submissions
88+
.filter(submission => {
89+
if (!canViewSubmissions) {
90+
return String(submission.userId) === String(loginUserInfo?.userId)
91+
}
92+
93+
return true
94+
}), [canViewSubmissions, loginUserInfo?.userId])
95+
96+
const winnerData = filterFunc(datas)
97+
5998
const reviewTabUrl = useMemo(() => {
6099
const searchParams = new URLSearchParams(location.search)
61100
const challengePhases = challengeInfo?.phases ?? []
@@ -271,11 +310,11 @@ export const TableWinners: FC<Props> = (props: Props) => {
271310
)}
272311
>
273312
{isTablet ? (
274-
<TableMobile columns={columnsMobile} data={datas} />
313+
<TableMobile columns={columnsMobile} data={winnerData} />
275314
) : (
276315
<Table
277316
columns={columns}
278-
data={datas}
317+
data={winnerData}
279318
disableSorting
280319
onToggleSort={_.noop}
281320
removeDefaultSort

0 commit comments

Comments
 (0)