Skip to content

Commit 19ecbcf

Browse files
authored
Merge pull request #1388 from topcoder-platform/PM-3255
Pm 3255
2 parents 2a17ea4 + 4fcb67f commit 19ecbcf

File tree

4 files changed

+145
-8
lines changed

4 files changed

+145
-8
lines changed

src/apps/review/src/lib/contexts/ChallengeDetailContextProvider.tsx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import { FC, PropsWithChildren, useContext, useMemo } from 'react'
55
import { useParams } from 'react-router-dom'
66

77
import { convertBackendSubmissionToSubmissionInfo } from '../models'
8-
import type { ChallengeDetailContextModel, SubmissionInfo } from '../models'
8+
import type {
9+
ChallengeDetailContextModel,
10+
ReviewAppContextModel,
11+
SubmissionInfo,
12+
} from '../models'
913
import {
1014
useFetchChallengeInfo,
1115
useFetchChallengeInfoProps,
@@ -16,11 +20,13 @@ import {
1620
} from '../hooks'
1721

1822
import { ChallengeDetailContext } from './ChallengeDetailContext'
23+
import { ReviewAppContext } from './ReviewAppContext'
1924

2025
export const ChallengeDetailContextProvider: FC<PropsWithChildren> = props => {
2126
const { challengeId = '' }: { challengeId?: string } = useParams<{
2227
challengeId: string
2328
}>()
29+
const { loginUserInfo }: ReviewAppContextModel = useContext(ReviewAppContext)
2430
// fetch challenge info
2531
const {
2632
challengeInfo,
@@ -37,10 +43,21 @@ export const ChallengeDetailContextProvider: FC<PropsWithChildren> = props => {
3743
isLoading: isLoadingChallengeResources,
3844
resourceMemberIdMapping,
3945
}: useFetchChallengeResourcesProps = useFetchChallengeResources(challengeId)
46+
const submissionViewer = useMemo(
47+
() => ({
48+
roles: myRoles,
49+
tokenRoles: loginUserInfo?.roles,
50+
userId: loginUserInfo?.userId,
51+
}),
52+
[loginUserInfo?.roles, loginUserInfo?.userId, myRoles],
53+
)
4054
const {
4155
challengeSubmissions,
4256
isLoading: isLoadingChallengeSubmissions,
43-
}: useFetchChallengeSubmissionsProps = useFetchChallengeSubmissions(challengeId)
57+
}: useFetchChallengeSubmissionsProps = useFetchChallengeSubmissions(
58+
challengeId,
59+
submissionViewer,
60+
)
4461

4562
const submissionInfos = useMemo<SubmissionInfo[]>(
4663
() => challengeSubmissions.map(convertBackendSubmissionToSubmissionInfo),

src/apps/review/src/lib/hooks/useFetchChallengeSubmissions.ts

Lines changed: 111 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
import useSWR, { SWRResponse } from 'swr'
66

77
import { handleError } from '~/libs/shared'
8+
import { UserRole } from '~/libs/core'
89

910
import { BackendSubmission, BackendSubmissionStatus } from '../models'
1011
import { fetchSubmissions } from '../services'
@@ -22,13 +23,20 @@ interface ChallengeSubmissionsMemoResult {
2223
filteredSubmissions: BackendSubmission[]
2324
}
2425

26+
export interface ChallengeSubmissionsViewer {
27+
roles?: Array<string | undefined | null>
28+
tokenRoles?: Array<string | undefined | null>
29+
userId?: string | number | null
30+
}
31+
2532
/**
2633
* Fetch challenge submissions
2734
* @param challengeId challenge id
2835
* @returns challenge submissions
2936
*/
3037
export function useFetchChallengeSubmissions(
3138
challengeId?: string,
39+
viewer?: ChallengeSubmissionsViewer,
3240
): useFetchChallengeSubmissionsProps {
3341
// Use swr hooks for submissions fetching
3442
const {
@@ -46,6 +54,92 @@ export function useFetchChallengeSubmissions(
4654
isPaused: () => !challengeId,
4755
})
4856

57+
const normalizedRoles = useMemo<string[]>(
58+
() => (viewer?.roles ?? [])
59+
.map(role => (role ? `${role}`.toLowerCase()
60+
.trim() : ''))
61+
.filter(Boolean),
62+
[viewer?.roles],
63+
)
64+
const normalizedTokenRoles = useMemo<string[]>(
65+
() => (viewer?.tokenRoles ?? [])
66+
.map(role => (typeof role === 'string' ? role.toLowerCase()
67+
.trim() : ''))
68+
.filter(Boolean),
69+
[viewer?.tokenRoles],
70+
)
71+
const hasSubmitterRole = useMemo<boolean>(
72+
() => normalizedRoles.some(role => role.includes('submitter')),
73+
[normalizedRoles],
74+
)
75+
const hasCopilotRole = useMemo<boolean>(
76+
() => normalizedRoles.some(role => role.includes('copilot')),
77+
[normalizedRoles],
78+
)
79+
const hasReviewerRole = useMemo<boolean>(
80+
() => normalizedRoles.some(role => role.includes('reviewer')),
81+
[normalizedRoles],
82+
)
83+
const hasManagerRole = useMemo<boolean>(
84+
() => normalizedRoles.some(role => role.includes('manager')),
85+
[normalizedRoles],
86+
)
87+
const hasScreenerRole = useMemo<boolean>(
88+
() => normalizedRoles.some(role => role.includes('screener')),
89+
[normalizedRoles],
90+
)
91+
const hasApproverRole = useMemo<boolean>(
92+
() => normalizedRoles.some(role => role.includes('approver')),
93+
[normalizedRoles],
94+
)
95+
const isProjectManager = useMemo<boolean>(
96+
() => normalizedTokenRoles.some(
97+
role => role === UserRole.projectManager.toLowerCase(),
98+
)
99+
|| normalizedRoles.some(role => role.includes('project manager')),
100+
[normalizedRoles, normalizedTokenRoles],
101+
)
102+
const isAdmin = useMemo<boolean>(
103+
() => normalizedTokenRoles.some(
104+
role => role === UserRole.administrator.toLowerCase(),
105+
)
106+
|| normalizedRoles.some(role => role.includes('admin')),
107+
[normalizedRoles, normalizedTokenRoles],
108+
)
109+
const canViewAllSubmissions = useMemo<boolean>(
110+
() => (viewer ? (
111+
isAdmin
112+
|| hasCopilotRole
113+
|| hasReviewerRole
114+
|| hasManagerRole
115+
|| hasScreenerRole
116+
|| hasApproverRole
117+
|| isProjectManager
118+
) : true),
119+
[
120+
viewer,
121+
isAdmin,
122+
hasCopilotRole,
123+
hasReviewerRole,
124+
hasManagerRole,
125+
hasScreenerRole,
126+
hasApproverRole,
127+
isProjectManager,
128+
],
129+
)
130+
const viewerMemberId = useMemo<string | undefined>(
131+
() => {
132+
const raw = viewer?.userId
133+
if (raw === undefined || raw === null) {
134+
return undefined
135+
}
136+
137+
const normalized = `${raw}`.trim()
138+
return normalized.length ? normalized : undefined
139+
},
140+
[viewer?.userId],
141+
)
142+
49143
// Show backend error when fetching data fail
50144
useEffect(() => {
51145
if (error) {
@@ -69,6 +163,10 @@ export function useFetchChallengeSubmissions(
69163
const normalizedDeletedIds = new Set<string>()
70164
const normalizedDeletedLegacyIds = new Set<string>()
71165
const activeSubmissions: BackendSubmission[] = []
166+
const shouldRestrictToCurrentMember = Boolean(
167+
hasSubmitterRole
168+
&& !canViewAllSubmissions,
169+
)
72170

73171
const normalizeStatus = (status: unknown): string => {
74172
if (typeof status === 'string') {
@@ -105,12 +203,23 @@ export function useFetchChallengeSubmissions(
105203
activeSubmissions.push(submission)
106204
})
107205

206+
const visibleSubmissions = shouldRestrictToCurrentMember
207+
? activeSubmissions.filter(submission => (viewerMemberId
208+
? `${submission?.memberId ?? ''}` === viewerMemberId
209+
: false))
210+
: activeSubmissions
211+
108212
return {
109213
deletedLegacySubmissionIds: normalizedDeletedLegacyIds,
110214
deletedSubmissionIds: normalizedDeletedIds,
111-
filteredSubmissions: activeSubmissions,
215+
filteredSubmissions: visibleSubmissions,
112216
}
113-
}, [challengeSubmissions])
217+
}, [
218+
challengeSubmissions,
219+
canViewAllSubmissions,
220+
hasSubmitterRole,
221+
viewerMemberId,
222+
])
114223

115224
return {
116225
challengeSubmissions: filteredSubmissions,

src/apps/review/src/lib/hooks/useFetchScreeningReview.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,16 @@ export function useFetchScreeningReview(): useFetchScreeningReviewProps {
436436
reviewers: challengeReviewers,
437437
resources,
438438
myResources,
439+
myRoles,
439440
}: ChallengeDetailContextModel = useContext(ChallengeDetailContext)
441+
const submissionViewer = useMemo(
442+
() => ({
443+
roles: myRoles,
444+
tokenRoles: loginUserInfo?.roles,
445+
userId: loginUserInfo?.userId,
446+
}),
447+
[loginUserInfo?.roles, loginUserInfo?.userId, myRoles],
448+
)
440449

441450
const challengeLegacy = (challengeInfo as unknown as {
442451
legacy?: {
@@ -451,7 +460,10 @@ export function useFetchScreeningReview(): useFetchScreeningReviewProps {
451460
deletedLegacySubmissionIds,
452461
deletedSubmissionIds,
453462
isLoading,
454-
}: useFetchChallengeSubmissionsProps = useFetchChallengeSubmissions(challengeId)
463+
}: useFetchChallengeSubmissionsProps = useFetchChallengeSubmissions(
464+
challengeId,
465+
submissionViewer,
466+
)
455467

456468
const visibleChallengeSubmissions = useMemo<BackendSubmission[]>(
457469
() => challengeSubmissions,

start.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44

55
export ESLINT_NO_DEV_ERRORS=true
66
export HTTPS=true
7-
export HOST=local.topcoder-dev.com
8-
export REACT_APP_HOST_ENV=${REACT_APP_HOST_ENV:-dev}
97
export PORT=443
10-
8+
export REACT_APP_HOST_ENV=${REACT_APP_HOST_ENV:-dev}
9+
export HOST=local.topcoder-dev.com
1110
# if [[ ! -e ./.environments/.env.local ]]; then
1211
# filename=.env.${REACT_APP_HOST_ENV:-dev}
1312
# cp ./.environments/$filename ./.environments/.env.local

0 commit comments

Comments
 (0)