Skip to content

Commit

Permalink
refactor(fe): refactor graphql query files (#1713)
Browse files Browse the repository at this point in the history
* refactor(fe): move graphql queries to graphql folder

* refactor(fe): edit query name and order
  • Loading branch information
B0XERCAT authored May 27, 2024
1 parent 67632e1 commit b994a02
Show file tree
Hide file tree
Showing 18 changed files with 396 additions and 395 deletions.
106 changes: 12 additions & 94 deletions apps/frontend/app/admin/contest/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
'use client'

import { gql } from '@generated'
import { DataTableAdmin } from '@/components/DataTableAdmin'
import TextEditor from '@/components/TextEditor'
import { DateTimePickerDemo } from '@/components/date-time-picker-demo'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { ScrollArea } from '@/components/ui/scroll-area'
import {
IMPORT_PROBLEMS_TO_CONTEST,
UPDATE_CONTEST,
REMOVE_PROBLEMS_FROM_CONTEST
} from '@/graphql/contest/mutations'
import { GET_CONTEST } from '@/graphql/contest/queries'
import {
UPDATE_PROBLEM_VISIBLE,
UPDATE_CONTEST_PROBLEMS_ORDER
} from '@/graphql/problem/mutations'
import { GET_CONTEST_PROBLEMS } from '@/graphql/problem/queries'
import { cn } from '@/lib/utils'
import { useMutation, useQuery } from '@apollo/client'
import type { UpdateContestInput } from '@generated/graphql'
Expand All @@ -24,98 +34,6 @@ import { z } from 'zod'
import Label from '../_components/Label'
import { columns } from './_components/Columns'

const GET_CONTEST = gql(`
query GetContest($contestId: Int!) {
getContest(contestId: $contestId) {
id
description
endTime
startTime
title
}
}
`)

const UPDATE_CONTEST = gql(`
mutation UpdateContest($groupId: Int!, $input: UpdateContestInput!) {
updateContest(groupId: $groupId, input: $input) {
id
isRankVisible
isVisible
description
endTime
startTime
title
}
}
`)

const GET_CONTEST_PROBLEMS = gql(`
query GetContestProblems($groupId: Int!, $contestId: Int!) {
getContestProblems(groupId: $groupId, contestId: $contestId) {
order
problemId
problem {
title
difficulty
}
}
}
`)

const IMPORT_PROBLEMS_TO_CONTEST = gql(`
mutation ImportProblemsToContest(
$groupId: Int!,
$contestId: Int!,
$problemIds: [Int!]!
) {
importProblemsToContest(
groupId: $groupId,
contestId: $contestId,
problemIds: $problemIds
) {
contestId
problemId
}
}
`)

const REMOVE_PROBLEMS_FROM_CONTEST = gql(`
mutation RemoveProblemsFromContest(
$groupId: Int!,
$contestId: Int!,
$problemIds: [Int!]!
) {
removeProblemsFromContest(
groupId: $groupId,
contestId: $contestId,
problemIds: $problemIds
) {
contestId
problemId
}
}
`)

const UPDATE_CONTEST_PROBLEMS_ORDER = gql(`
mutation UpdateContestProblemsOrder($groupId: Int!, $contestId: Int!, $orders: [Int!]!) {
updateContestProblemsOrder(groupId: $groupId, contestId: $contestId, orders: $orders) {
order
contestId
problemId
}
}
`)

const EDIT_VISIBLE = gql(`
mutation UpdateVisible($groupId: Int!, $input: UpdateProblemInput!) {
updateProblem(groupId: $groupId, input: $input) {
id
isVisible
}
}
`)

const inputStyle =
'border-gray-200 focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-gray-950'

Expand Down Expand Up @@ -230,7 +148,7 @@ export default function Page({ params }: { params: { id: string } }) {
const [updateContest, { error }] = useMutation(UPDATE_CONTEST)
const [importProblemsToContest] = useMutation(IMPORT_PROBLEMS_TO_CONTEST)
const [removeProblemsFromContest] = useMutation(REMOVE_PROBLEMS_FROM_CONTEST)
const [updateVisible] = useMutation(EDIT_VISIBLE)
const [updateVisible] = useMutation(UPDATE_PROBLEM_VISIBLE)
const [updateContestProblemsOrder] = useMutation(
UPDATE_CONTEST_PROBLEMS_ORDER
)
Expand Down
14 changes: 2 additions & 12 deletions apps/frontend/app/admin/contest/_components/Columns.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use client'

import { gql } from '@generated'
import { DataTableColumnHeader } from '@/components/DataTableColumnHeader'
import { Checkbox } from '@/components/ui/checkbox'
import { Switch } from '@/components/ui/switch'
Expand All @@ -10,6 +9,7 @@ import {
TooltipProvider,
TooltipTrigger
} from '@/components/ui/tooltip'
import { UPDATE_CONTEST_VISIBLE } from '@/graphql/contest/mutations'
import { cn, dateFormatter } from '@/lib/utils'
import { useMutation } from '@apollo/client'
import * as TooltipPrimitive from '@radix-ui/react-tooltip'
Expand All @@ -28,18 +28,8 @@ interface DataTableContest {
isRankVisible: boolean
}

const EDIT_VISIBLE = gql(`
mutation UpdateContestVisible($groupId: Int!, $input: UpdateContestInput!) {
updateContest(groupId: $groupId, input: $input) {
id
isVisible
isRankVisible
}
}
`)

function VisibleCell({ row }: { row: Row<DataTableContest> }) {
const [updateVisible] = useMutation(EDIT_VISIBLE)
const [updateVisible] = useMutation(UPDATE_CONTEST_VISIBLE)

return (
<div className="flex space-x-2">
Expand Down
61 changes: 9 additions & 52 deletions apps/frontend/app/admin/contest/create/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use client'

import { gql } from '@generated'
import { DataTableAdmin } from '@/components/DataTableAdmin'
import TextEditor from '@/components/TextEditor'
import { DateTimePickerDemo } from '@/components/date-time-picker-demo'
Expand All @@ -12,6 +11,14 @@ import {
PopoverTrigger
} from '@/components/ui/popover'
import { ScrollArea } from '@/components/ui/scroll-area'
import {
CREATE_CONTEST,
IMPORT_PROBLEMS_TO_CONTEST
} from '@/graphql/contest/mutations'
import {
UPDATE_PROBLEM_VISIBLE,
UPDATE_CONTEST_PROBLEMS_ORDER
} from '@/graphql/problem/mutations'
import { cn } from '@/lib/utils'
import { useMutation } from '@apollo/client'
import type { CreateContestInput } from '@generated/graphql'
Expand All @@ -30,56 +37,6 @@ import { z } from 'zod'
import Label from '../_components/Label'
import { columns } from './_components/Columns'

const CREATE_CONTEST = gql(`
mutation CreateContest($groupId: Int!, $input: CreateContestInput!) {
createContest(groupId: $groupId, input: $input) {
id
isVisible
isRankVisible
description
endTime
startTime
title
}
}
`)

const IMPORT_PROBLEMS_TO_CONTEST = gql(`
mutation ImportProblemsToContest(
$groupId: Int!,
$contestId: Int!,
$problemIds: [Int!]!
) {
importProblemsToContest(
groupId: $groupId,
contestId: $contestId,
problemIds: $problemIds
) {
contestId
problemId
}
}
`)

const UPDATE_CONTEST_PROBLEMS_ORDER = gql(`
mutation UpdateContestProblemsOrder($groupId: Int!, $contestId: Int!, $orders: [Int!]!) {
updateContestProblemsOrder(groupId: $groupId, contestId: $contestId, orders: $orders) {
order
contestId
problemId
}
}
`)

const EDIT_VISIBLE = gql(`
mutation UpdateVisible($groupId: Int!, $input: UpdateProblemInput!) {
updateProblem(groupId: $groupId, input: $input) {
id
isVisible
}
}
`)

const inputStyle =
'border-gray-200 focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-gray-950'

Expand Down Expand Up @@ -123,7 +80,7 @@ export default function Page() {

const [createContest, { error }] = useMutation(CREATE_CONTEST)
const [importProblemsToContest] = useMutation(IMPORT_PROBLEMS_TO_CONTEST)
const [updateVisible] = useMutation(EDIT_VISIBLE)
const [updateVisible] = useMutation(UPDATE_PROBLEM_VISIBLE)
const [updateContestProblemsOrder] = useMutation(
UPDATE_CONTEST_PROBLEMS_ORDER
)
Expand Down
25 changes: 1 addition & 24 deletions apps/frontend/app/admin/contest/page.tsx
Original file line number Diff line number Diff line change
@@ -1,40 +1,17 @@
'use client'

import { gql } from '@generated'
import { DataTableAdmin } from '@/components/DataTableAdmin'
import { Button } from '@/components/ui/button'
import { ScrollArea } from '@/components/ui/scroll-area'
import { Skeleton } from '@/components/ui/skeleton'
import { GET_CONTESTS } from '@/graphql/contest/queries'
import { useQuery } from '@apollo/client'
import { PlusCircleIcon } from 'lucide-react'
import Link from 'next/link'
import { columns } from './_components/Columns'

export const dynamic = 'force-dynamic'

const GET_CONTESTS = gql(`
query getContests (
$groupId: Int!
$cursor: Int
$take: Int!
) {
getContests (
groupId: $groupId
cursor: $cursor
take: $take
) {
id
title
startTime
endTime
description
participants
isRankVisible
isVisible
}
}
`)

export default function Page() {
const { data, loading } = useQuery(GET_CONTESTS, {
variables: {
Expand Down
77 changes: 4 additions & 73 deletions apps/frontend/app/admin/problem/[id]/edit/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use client'

import { gql } from '@generated'
import CheckboxSelect from '@/components/CheckboxSelect'
import OptionSelect from '@/components/OptionSelect'
import TagsSelect from '@/components/TagsSelect'
Expand All @@ -16,6 +15,9 @@ import {
import { ScrollArea, ScrollBar } from '@/components/ui/scroll-area'
import { Switch } from '@/components/ui/switch'
import { Textarea } from '@/components/ui/textarea'
import { UPDATE_PROBLEM } from '@/graphql/problem/mutations'
import { GET_PROBLEM } from '@/graphql/problem/queries'
import { GET_TAGS } from '@/graphql/problem/queries'
import { languages, levels } from '@/lib/constants'
import { cn } from '@/lib/utils'
import { useMutation, useQuery } from '@apollo/client'
Expand All @@ -34,78 +36,7 @@ import { toast } from 'sonner'
import { z } from 'zod'
import ExampleTextarea from '../../_components/ExampleTextarea'
import Label from '../../_components/Label'
import { GET_TAGS, inputStyle } from '../../utils'

const GET_PROBLEM = gql(`
query GetProblem($groupId: Int!, $id: Int!) {
getProblem(groupId: $groupId, id: $id) {
title
isVisible
difficulty
languages
tag {
tag {
id
name
}
}
description
inputDescription
outputDescription
samples {
id
input
output
}
testcase {
id
input
output
}
timeLimit
memoryLimit
hint
source
template
}
}
`)

const UPDATE_PROBLEM = gql(`
mutation UpdateProblem($groupId: Int!, $input: UpdateProblemInput!) {
updateProblem(groupId: $groupId, input: $input) {
id
createdById
groupId
title
isVisible
difficulty
languages
problemTag {
tag {
id
name
}
}
description
inputDescription
outputDescription
samples {
input
output
}
problemTestcase {
input
output
}
timeLimit
memoryLimit
hint
source
template
}
}
`)
import { inputStyle } from '../../utils'

const schema = z.object({
id: z.number(),
Expand Down
Loading

0 comments on commit b994a02

Please sign in to comment.