Skip to content

Commit c9ea539

Browse files
committed
Phase handling cleanups
1 parent 399862d commit c9ea539

File tree

11 files changed

+304
-108
lines changed

11 files changed

+304
-108
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
} from '../../hooks/useFetchChallengeResults'
1818
import { ITERATIVE_REVIEW, SUBMITTER } from '../../../config/index.config'
1919
import { TableNoRecord } from '../TableNoRecord'
20+
import { hasIsLatestFlag } from '../../utils'
2021

2122
import TabContentApproval from './TabContentApproval'
2223
import TabContentCheckpoint from './TabContentCheckpoint'
@@ -106,9 +107,8 @@ export const ChallengeDetailsContent: FC<Props> = (props: Props) => {
106107
}
107108

108109
if (['submission / screening', 'submission', 'screening'].includes(selectedTabLower)) {
109-
const restrictScreeningToLatest = ['screening', 'submission / screening', 'submission']
110-
.includes(selectedTabLower)
111-
const screeningRows = restrictScreeningToLatest
110+
const hasLatestFlag = hasIsLatestFlag(props.screening)
111+
const screeningRows = hasLatestFlag
112112
? props.screening.filter(submission => submission.isLatest === true)
113113
: props.screening
114114

src/apps/review/src/lib/components/ChallengeDetailsContent/TabContentReview.tsx

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { TableLoading } from '~/apps/admin/src/lib'
88
import { IsRemovingType } from '~/apps/admin/src/lib/models'
99

1010
import { MappingReviewAppeal, SubmissionInfo } from '../../models'
11+
import { hasIsLatestFlag } from '../../utils'
1112
import { TableNoRecord } from '../TableNoRecord'
1213
import { TableReviewAppeals } from '../TableReviewAppeals'
1314
import { useRole, useRoleProps } from '../../hooks'
@@ -33,19 +34,23 @@ export const TabContentReview: FC<Props> = (props: Props) => {
3334
const selectedTab = props.selectedTab
3435
const reviews = props.reviews
3536
const submitterReviews = props.submitterReviews
36-
const restrictToLatest = ['review', 'screening', 'appeals', 'appeals response']
37-
.includes((selectedTab || '').toLowerCase())
3837
const filteredReviews = useMemo(
39-
() => (restrictToLatest
40-
? reviews.filter(submission => submission.isLatest === true)
41-
: reviews),
42-
[restrictToLatest, reviews],
38+
() => {
39+
const hasLatestFlag = hasIsLatestFlag(reviews)
40+
return hasLatestFlag
41+
? reviews.filter(submission => submission.isLatest === true)
42+
: reviews
43+
},
44+
[reviews],
4345
)
4446
const filteredSubmitterReviews = useMemo(
45-
() => (restrictToLatest
46-
? submitterReviews.filter(submission => submission.isLatest === true)
47-
: submitterReviews),
48-
[restrictToLatest, submitterReviews],
47+
() => {
48+
const hasLatestFlag = hasIsLatestFlag(submitterReviews)
49+
return hasLatestFlag
50+
? submitterReviews.filter(submission => submission.isLatest === true)
51+
: submitterReviews
52+
},
53+
[submitterReviews],
4954
)
5055
const firstSubmissions = useMemo(
5156
() => maxBy(filteredReviews, 'review.initialScore'),

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

Lines changed: 61 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ export const ScorecardDetails: FC<Props> = (props: Props) => {
118118
const { mutate }: { mutate: (key: any, data?: any, opts?: any) => Promise<any> } = useSWRConfig()
119119
const [isExpand, setIsExpand] = useState<{ [key: string]: boolean }>({})
120120
const [isShowSaveAsDraftModal, setIsShowSaveAsDraftModal] = useState(false)
121+
const [shouldRedirectAfterDraft, setShouldRedirectAfterDraft] = useState(false)
121122
const mappingReviewInfo = useMemo<{
122123
[key: string]: {
123124
item: ReviewItemInfo
@@ -167,6 +168,54 @@ export const ScorecardDetails: FC<Props> = (props: Props) => {
167168
changeHandle(isDirty)
168169
}, [isDirty, changeHandle])
169170

171+
const redirectToCurrentPhaseTab = useCallback(async () => {
172+
if (!challengeId) {
173+
return
174+
}
175+
176+
const challengeDetailsRoute
177+
= `${rootRoute}/${activeReviewAssigmentsRouteId}/${challengeId}/challenge-details`
178+
179+
try {
180+
await mutate(
181+
(key: unknown) => (
182+
typeof key === 'string'
183+
&& key.startsWith(`reviewBaseUrl/reviews/${challengeId}/`)
184+
),
185+
)
186+
await mutate(`reviewBaseUrl/submissions/${challengeId}`)
187+
} catch {
188+
// no-op: navigation should still occur even if revalidation fails
189+
}
190+
191+
const tabFromPhase = computeTabFromPhase(
192+
(challengeInfo?.phases || []) as Array<{
193+
id?: string
194+
name?: string
195+
scheduledStartDate?: string
196+
actualStartDate?: string
197+
}>,
198+
reviewInfo?.phaseId,
199+
challengeInfo?.type?.name,
200+
challengeInfo?.type?.abbreviation,
201+
)
202+
const hasIterativePhase = (challengeInfo?.phases || [])
203+
.some(p => (p?.name || '').toString()
204+
.toLowerCase()
205+
.startsWith('iterative review'))
206+
const tabSlug = tabFromPhase || (hasIterativePhase ? 'iterative-review' : 'review')
207+
208+
navigate(`${challengeDetailsRoute}?tab=${tabSlug}`)
209+
}, [
210+
challengeId,
211+
challengeInfo?.phases,
212+
challengeInfo?.type?.abbreviation,
213+
challengeInfo?.type?.name,
214+
mutate,
215+
navigate,
216+
reviewInfo?.phaseId,
217+
])
218+
170219
const errorMessageTop
171220
= isEmpty(errors) || isEmpty(isTouched)
172221
? ''
@@ -217,58 +266,18 @@ export const ScorecardDetails: FC<Props> = (props: Props) => {
217266
getValues(),
218267
true,
219268
totalScore,
220-
async () => {
269+
() => {
221270
reset(data)
222-
if (challengeId) {
223-
const challengeDetailsRoute
224-
= `${rootRoute}/${activeReviewAssigmentsRouteId}/${challengeId}/challenge-details`
225-
226-
// Proactively revalidate any cached review lists for this challenge
227-
// so the score/status reflect immediately on return.
228-
try {
229-
await mutate(
230-
(key: unknown) => (
231-
typeof key === 'string'
232-
&& key.startsWith(`reviewBaseUrl/reviews/${challengeId}/`)
233-
),
234-
)
235-
// Also refresh the submissions list cache so any
236-
// reviewResourceMapping/states used as fallbacks are up-to-date.
237-
await mutate(`reviewBaseUrl/submissions/${challengeId}`)
238-
} catch {}
239-
240-
const tabFromPhase = computeTabFromPhase(
241-
(challengeInfo?.phases || []) as Array<{
242-
id?: string
243-
name?: string
244-
scheduledStartDate?: string
245-
actualStartDate?: string
246-
}>,
247-
reviewInfo?.phaseId,
248-
challengeInfo?.type?.name,
249-
challengeInfo?.type?.abbreviation,
250-
)
251-
const hasIterativePhase = (challengeInfo?.phases || [])
252-
.some(p => (p?.name || '').toString()
253-
.toLowerCase()
254-
.startsWith('iterative review'))
255-
const tabSlug = tabFromPhase || (hasIterativePhase ? 'iterative-review' : 'review')
256-
navigate(`${challengeDetailsRoute}?tab=${tabSlug}`)
257-
}
271+
redirectToCurrentPhaseTab()
272+
.catch(() => undefined)
258273
},
259274
)
260275
}, [
261-
challengeId,
262-
challengeInfo?.phases,
263-
challengeInfo?.type?.abbreviation,
264-
challengeInfo?.type?.name,
265276
getValues,
266277
isDirty,
267-
navigate,
268-
mutate,
278+
redirectToCurrentPhaseTab,
269279
reset,
270280
saveReviewInfo,
271-
reviewInfo?.phaseId,
272281
totalScore,
273282
])
274283

@@ -416,7 +425,12 @@ export const ScorecardDetails: FC<Props> = (props: Props) => {
416425

417426
const closeHandel = useCallback(() => {
418427
setIsShowSaveAsDraftModal(false)
419-
}, [])
428+
if (shouldRedirectAfterDraft) {
429+
setShouldRedirectAfterDraft(false)
430+
redirectToCurrentPhaseTab()
431+
.catch(() => undefined)
432+
}
433+
}, [redirectToCurrentPhaseTab, shouldRedirectAfterDraft])
420434

421435
return isLoading ? (<TableLoading />) : (
422436
<div className={classNames(styles.container, className)}>
@@ -645,6 +659,7 @@ export const ScorecardDetails: FC<Props> = (props: Props) => {
645659
totalScore,
646660
() => {
647661
setIsShowSaveAsDraftModal(true)
662+
setShouldRedirectAfterDraft(true)
648663
reset(getValues())
649664
},
650665
)
@@ -814,13 +829,7 @@ function computeTabFromPhase(
814829
counts.set(raw, n + 1)
815830
const label = n === 0 ? raw : `${raw} ${n + 1}`
816831
if (p.id === targetPhaseId) {
817-
// Only return a tab for Iterative Review phases to avoid
818-
// changing non-iterative redirects.
819-
if (isIterativeReview(raw)) {
820-
return kebabCase(label)
821-
}
822-
823-
return undefined
832+
return kebabCase(label)
824833
}
825834
}
826835
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@ import {
3838
AggregatedReviewDetail,
3939
AggregatedSubmissionReviews,
4040
aggregateSubmissionReviews,
41+
challengeHasSubmissionLimit,
4142
getHandleUrl,
4243
getSubmissionHistoryKey,
44+
hasIsLatestFlag,
4345
isReviewPhase,
4446
partitionSubmissionHistory,
45-
shouldRestrictToLatestSubmissions,
4647
SubmissionHistoryPartition,
4748
} from '../../utils'
4849
import { ConfirmModal } from '../ConfirmModal'
@@ -211,9 +212,8 @@ export const TableReviewAppeals: FC<Props> = (props: Props) => {
211212
historyByMember,
212213
}: SubmissionHistoryPartition = submissionHistory
213214
const hasHistoryEntries = useMemo(
214-
() => isSubmissionTab && Array.from(historyByMember.values())
215-
.some(list => list.length > 0),
216-
[historyByMember, isSubmissionTab],
215+
() => isSubmissionTab && hasIsLatestFlag(datas),
216+
[datas, isSubmissionTab],
217217
)
218218

219219
const submissionMetaById = useMemo(() => {
@@ -237,7 +237,7 @@ export const TableReviewAppeals: FC<Props> = (props: Props) => {
237237
)
238238

239239
const restrictToLatest = useMemo(
240-
() => shouldRestrictToLatestSubmissions(challengeInfo),
240+
() => challengeHasSubmissionLimit(challengeInfo),
241241
[challengeInfo],
242242
)
243243

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

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ import {
2828
AggregatedReviewDetail,
2929
AggregatedSubmissionReviews,
3030
aggregateSubmissionReviews,
31+
challengeHasSubmissionLimit,
3132
getSubmissionHistoryKey,
33+
hasIsLatestFlag,
3234
isAppealsPhase,
3335
isAppealsResponsePhase,
3436
partitionSubmissionHistory,
@@ -99,6 +101,7 @@ export const TableReviewAppealsForSubmitter: FC<Props> = (props: Props) => {
99101
)
100102
const {
101103
latestSubmissions,
104+
latestSubmissionIds,
102105
historyByMember,
103106
}: SubmissionHistoryPartition = submissionHistory
104107

@@ -124,6 +127,11 @@ export const TableReviewAppealsForSubmitter: FC<Props> = (props: Props) => {
124127
[submissionMetaById],
125128
)
126129

130+
const restrictToLatest = useMemo(
131+
() => challengeHasSubmissionLimit(challengeInfo),
132+
[challengeInfo],
133+
)
134+
127135
const [historyKey, setHistoryKey] = useState<string | undefined>(undefined)
128136

129137
const historyEntriesForModal = useMemo<SubmissionInfo[]>(
@@ -149,9 +157,8 @@ export const TableReviewAppealsForSubmitter: FC<Props> = (props: Props) => {
149157
)
150158

151159
const hasHistoryEntries = useMemo(
152-
() => isSubmissionTab && Array.from(historyByMember.values())
153-
.some(list => list.length > 0),
154-
[historyByMember, isSubmissionTab],
160+
() => isSubmissionTab && hasIsLatestFlag(datas),
161+
[datas, isSubmissionTab],
155162
)
156163

157164
const getHistoryRestriction = useCallback(
@@ -226,13 +233,18 @@ export const TableReviewAppealsForSubmitter: FC<Props> = (props: Props) => {
226233
[allowsAppeals, isAppealsTab],
227234
)
228235

236+
const submissionsForAggregation = useMemo(
237+
() => (restrictToLatest ? latestSubmissions : datas),
238+
[datas, latestSubmissions, restrictToLatest],
239+
)
240+
229241
const aggregatedRows = useMemo(
230242
() => aggregateSubmissionReviews({
231243
mappingReviewAppeal,
232244
reviewers,
233-
submissions: latestSubmissions,
245+
submissions: submissionsForAggregation,
234246
}),
235-
[latestSubmissions, mappingReviewAppeal, reviewers],
247+
[mappingReviewAppeal, reviewers, submissionsForAggregation],
236248
)
237249

238250
const aggregatedSubmissionRows = useMemo<SubmissionRow[]>(
@@ -629,8 +641,14 @@ export const TableReviewAppealsForSubmitter: FC<Props> = (props: Props) => {
629641
const actionEntries: Array<{ key: string; element: JSX.Element }> = []
630642

631643
const historyKeyForRow = getSubmissionHistoryKey(data.memberId, data.id)
632-
const rowHistory = historyByMember.get(historyKeyForRow) ?? []
633-
if (isSubmissionTab && rowHistory.length > 0) {
644+
const rowHistoryEntries = historyByMember.get(historyKeyForRow) ?? []
645+
const isLatestSubmissionRow = latestSubmissionIds.has(data.id)
646+
const relevantHistory = restrictToLatest || isLatestSubmissionRow
647+
? rowHistoryEntries
648+
: []
649+
const filteredHistory = relevantHistory.filter(entry => entry.id !== data.id)
650+
651+
if (isSubmissionTab && filteredHistory.length > 0) {
634652
actionEntries.push({
635653
element: (
636654
<button
@@ -686,6 +704,8 @@ export const TableReviewAppealsForSubmitter: FC<Props> = (props: Props) => {
686704
openHistoryModal,
687705
shouldShowAppealsColumn,
688706
restrictionMessage,
707+
latestSubmissionIds,
708+
restrictToLatest,
689709
challengeInfo,
690710
isSubmissionTab,
691711
isAppealsTab,

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { SubmissionHistoryModal } from '../SubmissionHistoryModal'
2525
import {
2626
getHandleUrl,
2727
getSubmissionHistoryKey,
28+
hasIsLatestFlag,
2829
partitionSubmissionHistory,
2930
SubmissionHistoryPartition,
3031
} from '../../utils'
@@ -121,9 +122,8 @@ export const TableSubmissionScreening: FC<Props> = (props: Props) => {
121122
const { historyByMember }: SubmissionHistoryPartition = submissionHistory
122123

123124
const hasHistoryEntries = useMemo(
124-
() => Array.from(historyByMember.values())
125-
.some(list => list.length > 0),
126-
[historyByMember],
125+
() => hasIsLatestFlag(primarySubmissionInfos),
126+
[primarySubmissionInfos],
127127
)
128128

129129
const [historyKey, setHistoryKey] = useState<string | undefined>(undefined)

0 commit comments

Comments
 (0)