Skip to content

Commit

Permalink
fix(fe): don't calculate contest score when contest is upcoming (#2082)
Browse files Browse the repository at this point in the history
* fix(fe): don't calculate contest score when contest is upcoming

* refactor(fe): use safeFetcher on calculateContestScore

* fix(fe): use default value when input,output description doesn't exist

* fix(fe): show default value when input description or output description is null
  • Loading branch information
jimin9038 authored Sep 12, 2024
1 parent 5dca292 commit 869c65f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 20 deletions.
11 changes: 8 additions & 3 deletions apps/frontend/app/(main)/contest/[contestId]/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ContestStatusTimeDiff from '@/components/ContestStatusTimeDiff'
import { auth } from '@/lib/auth'
import { fetcher, fetcherWithAuth } from '@/lib/utils'
import { fetcher, fetcherWithAuth, getStatusWithStartEnd } from '@/lib/utils'
import { dateFormatter } from '@/lib/utils'
import Calendar from '@/public/20_calendar.svg'
import CheckIcon from '@/public/check_blue.svg'
Expand Down Expand Up @@ -40,9 +40,14 @@ export default async function Layout({
)
const isJudgeResultVisible = contest.isJudgeResultVisible
const isRegistered = contest.isRegistered
const contestStatus = getStatusWithStartEnd(
formattedStartTime,
formattedEndTime
)

let totalScore = 0
let totalMaxScore = 0
if (isRegistered && isJudgeResultVisible) {
if (isRegistered && isJudgeResultVisible && contestStatus !== 'upcoming') {
const [score, maxScore] = await calculateContestScore({ contestId })
totalScore = score
totalMaxScore = maxScore
Expand All @@ -56,7 +61,7 @@ export default async function Layout({
{contest?.title}
</h2>
<div className="flex items-center gap-2">
{isRegistered && (
{isRegistered && contestStatus !== 'upcoming' && (
<>
<Image src={CheckIcon} alt="check" width={24} height={24} />
<p className="text-primary-light text-sm font-bold">
Expand Down
34 changes: 19 additions & 15 deletions apps/frontend/app/(main)/contest/utils.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
import { fetcherWithAuth } from '@/lib/utils'
import { safeFetcherWithAuth } from '@/lib/utils'
import type { ContestProblem } from '@/types/type'

interface ContestProblemsApiRes {
data: ContestProblem[]
}

const calculateContestScore = async ({ contestId }: { contestId: string }) => {
const contestProblems: ContestProblemsApiRes = await fetcherWithAuth
.get(`contest/${contestId}/problem`)
.json()
try {
const contestProblems: ContestProblemsApiRes = await safeFetcherWithAuth
.get(`contest/${contestId}/problem`)
.json()

const { totalScore, totalMaxScore } = contestProblems.data.reduce(
(acc, curr) => {
const score = curr.score ? parseInt(curr.score, 10) : 0
const maxScore = curr.maxScore || 0
const { totalScore, totalMaxScore } = contestProblems.data.reduce(
(acc, curr) => {
const score = curr.score ? parseInt(curr.score, 10) : 0
const maxScore = curr.maxScore || 0

acc.totalScore += score
acc.totalMaxScore += maxScore
acc.totalScore += score
acc.totalMaxScore += maxScore

return acc
},
{ totalScore: 0, totalMaxScore: 0 }
)
return [totalScore, totalMaxScore]
return acc
},
{ totalScore: 0, totalMaxScore: 0 }
)
return [totalScore, totalMaxScore]
} catch (error) {
return [0, 0]
}
}

export { calculateContestScore }
10 changes: 8 additions & 2 deletions apps/frontend/app/admin/problem/[id]/edit/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,14 @@ export default function Page({ params }: { params: { id: string } }) {
data.tag.map(({ tag }) => Number(tag.id))
)
setValue('description', data.description)
setValue('inputDescription', data.inputDescription)
setValue('outputDescription', data.outputDescription)
setValue(
'inputDescription',
data.inputDescription || '<p>Change this</p>'
)
setValue(
'outputDescription',
data.outputDescription || '<p>Change this</p>'
)
setValue('testcases', data.testcase)
setValue('timeLimit', data.timeLimit)
setValue('memoryLimit', data.memoryLimit)
Expand Down

0 comments on commit 869c65f

Please sign in to comment.