Skip to content

Commit

Permalink
fix(fe): show warning when editing contest if submission exists (#2139)
Browse files Browse the repository at this point in the history
* fix(fe): show warning when editing contest if submission exists

* chore(fe): edit toast description
  • Loading branch information
B0XERCAT authored Oct 6, 2024
1 parent 6ec6c5b commit d8b2301
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 30 deletions.
87 changes: 58 additions & 29 deletions apps/frontend/app/admin/contest/[id]/edit/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
import { GET_CONTEST } from '@/graphql/contest/queries'
import { UPDATE_CONTEST_PROBLEMS_ORDER } from '@/graphql/problem/mutations'
import { GET_CONTEST_PROBLEMS } from '@/graphql/problem/queries'
import { GET_CONTEST_SUBMISSIONS_COUNT } from '@/graphql/submission/queries'
import { useMutation, useQuery } from '@apollo/client'
import type { UpdateContestInput } from '@generated/graphql'
import { zodResolver } from '@hookform/resolvers/zod'
Expand Down Expand Up @@ -62,6 +63,7 @@ export default function Page({ params }: { params: { id: string } }) {
const { id } = params

const shouldSkipWarning = useRef(false)
const hasSubmission = useRef(false)
const router = useRouter()

useConfirmNavigation(shouldSkipWarning)
Expand All @@ -76,6 +78,20 @@ export default function Page({ params }: { params: { id: string } }) {

const { handleSubmit, getValues, setValue } = methods

useQuery(GET_CONTEST_SUBMISSIONS_COUNT, {
variables: {
input: {
contestId: Number(id)
},
take: 2
},
onCompleted: (data) => {
if (data.getContestSubmissions.length !== 0) {
hasSubmission.current = true
}
}
})

useQuery(GET_CONTEST, {
variables: { contestId: Number(id) },
onCompleted: (contestData) => {
Expand Down Expand Up @@ -152,36 +168,49 @@ export default function Page({ params }: { params: { id: string } }) {
return
}

await removeProblemsFromContest({
variables: {
groupId: 1,
contestId: Number(id),
problemIds: prevProblemIds
}
})
await importProblemsToContest({
variables: {
groupId: 1,
contestId: Number(id),
problemIdsWithScore: problems.map((problem) => {
return {
problemId: problem.id,
score: problem.score
if (hasSubmission.current) {
await new Promise<void>((resolve) => {
toast.warning(
'Submissions exist. Only contest changes, excluding changes to the contest problems, will be saved.',
{
onAutoClose: () => {
resolve()
},
duration: 4000
}
})
}
})

const orderArray = problems
.sort((a, b) => a.order - b.order)
.map((problem) => problem.id)
await updateContestProblemsOrder({
variables: {
groupId: 1,
contestId: Number(id),
orders: orderArray
}
})
)
})
} else {
await removeProblemsFromContest({
variables: {
groupId: 1,
contestId: Number(id),
problemIds: prevProblemIds
}
})
await importProblemsToContest({
variables: {
groupId: 1,
contestId: Number(id),
problemIdsWithScore: problems.map((problem) => {
return {
problemId: problem.id,
score: problem.score
}
})
}
})
const orderArray = problems
.sort((a, b) => a.order - b.order)
.map((problem) => problem.id)
await updateContestProblemsOrder({
variables: {
groupId: 1,
contestId: Number(id),
orders: orderArray
}
})
}

shouldSkipWarning.current = true
toast.success('Contest updated successfully')
Expand Down
22 changes: 21 additions & 1 deletion apps/frontend/graphql/submission/queries.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import { gql } from '@generated'

const GET_CONTEST_SUBMISSIONS_COUNT = gql(`
query GetContestSubmissionsCount(
$input: GetContestSubmissionsInput!,
$cursor: Int,
$take: Int
) {
getContestSubmissions(
input: $input,
cursor: $cursor,
take: $take
) {
id
}
}
`)

const GET_CONTEST_SUBMISSIONS = gql(`
query GetContestSubmissions(
$input: GetContestSubmissionsInput!,
Expand Down Expand Up @@ -74,4 +90,8 @@ const GET_SUBMISSION = gql(`query GetSubmission(
}
}`)

export { GET_CONTEST_SUBMISSIONS, GET_SUBMISSION }
export {
GET_CONTEST_SUBMISSIONS_COUNT,
GET_CONTEST_SUBMISSIONS,
GET_SUBMISSION
}

0 comments on commit d8b2301

Please sign in to comment.