Skip to content

Commit

Permalink
refactor(fe): replace data table admin component to new components (2) (
Browse files Browse the repository at this point in the history
#2177)

* refactor(fe): refactor participant table

* refactor(fe): refactor contest submission table

* refactor(fe): refactor user score table and user submission table

* chore(fe): fix width style

* chore(fe): fix fallback ui

* chore(fe): remove unnecessary components
  • Loading branch information
eunnbi authored Nov 3, 2024
1 parent a8a928a commit 5dcbbc4
Show file tree
Hide file tree
Showing 21 changed files with 279 additions and 1,366 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ export default function DataTableFallback<TData>({
...props
}: DataTableFallbackProps<TData>) {
return (
<>
<div className="space-y-4">
{withSearchBar && <Skeleton className="h-10 w-[250px]" />}
<TableFallback {...props} />
</>
</div>
)
}

export function TableFallback<TData>({
function TableFallback<TData>({
columns,
headerStyle = {},
rowCount = 10
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
'use client'

import DataTableColumnHeader from '@/app/admin/_components/table/DataTableColumnHeader'
import type { ScoreSummary, ProblemData } from '@/app/admin/contest/utils'
import { DataTableColumnHeader } from '@/components/DataTableColumnHeader'
import type { ColumnDef, Row } from '@tanstack/react-table'

export const columns = (
interface DataTableScoreSummary extends ScoreSummary {
id: number
}

export const createColumns = (
problemData: ProblemData[]
): ColumnDef<ScoreSummary>[] => {
): ColumnDef<DataTableScoreSummary>[] => {
return [
{
accessorKey: 'studentId',
Expand Down Expand Up @@ -79,7 +83,7 @@ export const columns = (
{String.fromCharCode(Number(65 + i))}
</p>
),
cell: ({ row }: { row: Row<ScoreSummary> }) => {
cell: ({ row }: { row: Row<DataTableScoreSummary> }) => {
const problemScore = row.original.problemScores.find(
(ps) => ps.problemId === problem.problemId
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'use client'

import DataTable from '@/app/admin/_components/table/DataTable'
import DataTableFallback from '@/app/admin/_components/table/DataTableFallback'
import DataTablePagination from '@/app/admin/_components/table/DataTablePagination'
import DataTableRoot from '@/app/admin/_components/table/DataTableRoot'
import DataTableSearchBar from '@/app/admin/_components/table/DataTableSearchBar'
import { Skeleton } from '@/components/ui/skeleton'
import { GET_CONTEST_SCORE_SUMMARIES } from '@/graphql/contest/queries'
import { GET_CONTEST_PROBLEMS } from '@/graphql/problem/queries'
import { useSuspenseQuery } from '@apollo/client'
import { createColumns } from './Columns'

export function ParticipantTable({ contestId }: { contestId: number }) {
const summaries = useSuspenseQuery(GET_CONTEST_SCORE_SUMMARIES, {
variables: { contestId, take: 300 }
})
const summariesData = summaries.data.getContestScoreSummaries.map((item) => ({
...item,
id: item.userId
}))

const problems = useSuspenseQuery(GET_CONTEST_PROBLEMS, {
variables: { groupId: 1, contestId }
})

const problemData = problems.data.getContestProblems
.slice()
.sort((a, b) => a.order - b.order)

return (
<div>
<p className="mb-3 font-medium">
<span className="text-primary font-bold">{summariesData.length}</span>{' '}
Participants
</p>
<DataTableRoot data={summariesData} columns={createColumns(problemData)}>
<DataTableSearchBar columndId="realName" placeholder="Search Name" />
<DataTable
getHref={(data) =>
`/admin/contest/${contestId}/participant/${data.id}`
}
/>
<DataTablePagination />
</DataTableRoot>
</div>
)
}

export function ParticipantTableFallback() {
return (
<div>
<Skeleton className="mb-3 h-[24px] w-2/12" />
<DataTableFallback columns={createColumns([])} />
</div>
)
}
72 changes: 9 additions & 63 deletions apps/frontend/app/admin/contest/[id]/(overall)/@tabs/page.tsx
Original file line number Diff line number Diff line change
@@ -1,67 +1,13 @@
'use client'

import { DataTableAdmin } from '@/components/DataTableAdmin'
import { ScrollArea, ScrollBar } from '@/components/ui/scroll-area'
import { Skeleton } from '@/components/ui/skeleton'
import { GET_CONTEST_SCORE_SUMMARIES } from '@/graphql/contest/queries'
import { GET_CONTEST_PROBLEMS } from '@/graphql/problem/queries'
import { useQuery } from '@apollo/client'
import { useParams } from 'next/navigation'
import type { ScoreSummary, ProblemData } from '../../../utils'
import { columns } from './_components/Columns'

export default function Submission() {
const { id } = useParams()

const summaries = useQuery(GET_CONTEST_SCORE_SUMMARIES, {
variables: { contestId: Number(id), take: 300 }
})
const summariesData = summaries.data?.getContestScoreSummaries
const summariesLoading = summaries.loading

const problems =
useQuery(GET_CONTEST_PROBLEMS, {
variables: { groupId: 1, contestId: Number(id) }
}) || []
const problemData = problems.data?.getContestProblems
.slice()
.sort((a, b) => a.order - b.order)
const problemLoading = problems.loading
import { Suspense } from 'react'
import {
ParticipantTable,
ParticipantTableFallback
} from './_components/ParticipantTable'

export default function Submission({ params }: { params: { id: string } }) {
return (
<div>
{summariesLoading || problemLoading ? (
<>
<div className="mb-16 flex gap-4">
<span className="w-2/12">
<Skeleton className="h-10 w-full" />
</span>
</div>
{[...Array(8)].map((_, i) => (
<Skeleton key={i} className="my-2 flex h-12 w-full rounded-xl" />
))}
</>
) : (
<>
<p className="mb-3 font-medium">
<span className="text-primary font-bold">
{summariesData?.length}
</span>{' '}
Participants
</p>
<ScrollArea>
<DataTableAdmin
columns={columns(problemData as ProblemData[])}
data={summariesData as ScoreSummary[]}
enableSearch={true}
searchColumn="realName"
enablePagination={true}
/>
<div className="mt-6" />
<ScrollBar orientation="horizontal" />
</ScrollArea>
</>
)}
</div>
<Suspense fallback={<ParticipantTableFallback />}>
<ParticipantTable contestId={Number(params.id)} />
</Suspense>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import dayjs from 'dayjs'
export const columns: ColumnDef<OverallSubmission>[] = [
{
accessorKey: 'title',
id: 'problemTitle',
header: () => (
<div className="border-r py-1 font-mono text-sm">Problem Title</div>
),
cell: ({ row }) => (
<div className="whitespace-nowrap border-r py-1 text-center text-xs">
{String.fromCharCode(65 + row.original.order)}. {row.getValue('title')}
{String.fromCharCode(65 + (row.original.order ?? 0))}.{' '}
{row.getValue('problemTitle')}
</div>
)
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
'use client'

import DataTable from '@/app/admin/_components/table/DataTable'
import DataTableFallback from '@/app/admin/_components/table/DataTableFallback'
import DataTablePagination from '@/app/admin/_components/table/DataTablePagination'
import DataTableProblemFilter from '@/app/admin/_components/table/DataTableProblemFilter'
import DataTableRoot from '@/app/admin/_components/table/DataTableRoot'
import DataTableSearchBar from '@/app/admin/_components/table/DataTableSearchBar'
import { Dialog, DialogContent } from '@/components/ui/dialog'
import { GET_CONTEST_SUBMISSIONS } from '@/graphql/submission/queries'
import { useSuspenseQuery } from '@apollo/client'
import { useState } from 'react'
import { columns } from './Columns'
import SubmissionDetailAdmin from './SubmissionDetailAdmin'

export function SubmissionTable({ contestId }: { contestId: number }) {
const { data } = useSuspenseQuery(GET_CONTEST_SUBMISSIONS, {
variables: {
input: {
contestId
},
take: 1000
}
})

const [isSubmissionDialogOpen, setIsSubmissionDialogOpen] = useState(false)
const [submissionId, setSubmissionId] = useState(0)

return (
<>
<DataTableRoot
data={data.getContestSubmissions}
columns={columns}
defaultSortState={[{ id: 'submissionTime', desc: true }]}
>
<div className="flex gap-4">
<DataTableSearchBar columndId="realname" />
<DataTableProblemFilter contestId={contestId} />
</div>
<DataTable
onRowClick={(_, row) => {
setSubmissionId(row.original.id)
setIsSubmissionDialogOpen(true)
}}
/>
<DataTablePagination />
</DataTableRoot>
<Dialog
open={isSubmissionDialogOpen}
onOpenChange={setIsSubmissionDialogOpen}
>
<DialogContent className="max-h-[620px] max-w-[800px] justify-center">
<SubmissionDetailAdmin submissionId={submissionId} />
</DialogContent>
</Dialog>
</>
)
}

export function SubmissionTableFallback() {
return <DataTableFallback columns={columns} />
}
Original file line number Diff line number Diff line change
@@ -1,49 +1,13 @@
'use client'
import { Suspense } from 'react'
import {
SubmissionTable,
SubmissionTableFallback
} from './_components/SubmissionTable'

import type { OverallSubmission } from '@/app/admin/contest/utils'
import { DataTableAdmin } from '@/components/DataTableAdmin'
import { Skeleton } from '@/components/ui/skeleton'
import { GET_CONTEST_SUBMISSIONS } from '@/graphql/submission/queries'
import { useQuery } from '@apollo/client'
import { useParams } from 'next/navigation'
import { columns } from './_components/Columns'

export default function Submission() {
const { id } = useParams()
const { data, loading } = useQuery(GET_CONTEST_SUBMISSIONS, {
variables: {
input: {
contestId: Number(id)
},
take: 5000
}
})
const submissions = data?.getContestSubmissions || []
export default function Submission({ params }: { params: { id: string } }) {
return (
<div>
{loading ? (
<>
<div className="mb-16 flex gap-4">
<span className="w-2/12">
<Skeleton className="h-10 w-full" />
</span>
</div>
{[...Array(8)].map((_, i) => (
<Skeleton key={i} className="my-2 flex h-12 w-full rounded-xl" />
))}
</>
) : (
<DataTableAdmin
columns={columns}
data={submissions as OverallSubmission[]}
enableSearch={true}
searchColumn="realname"
enablePagination={true}
enableFilter={true}
enableProblemFilter={true}
defaultSortColumn={{ id: 'submissionTime', desc: true }}
/>
)}
</div>
<Suspense fallback={<SubmissionTableFallback />}>
<SubmissionTable contestId={Number(params.id)} />
</Suspense>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,24 @@
import type { ProblemData, ScoreSummary } from '@/app/admin/contest/utils'
import type { ColumnDef, Row } from '@tanstack/react-table'

export const scoreColumns = (
interface DataTableScoreSummary
extends Pick<
ScoreSummary,
| 'contestPerfectScore'
| 'submittedProblemCount'
| 'userContestScore'
| 'totalProblemCount'
> {
id: number
problemScores: {
problemId: number
score: number
}[]
}

export const createColumns = (
problemData: ProblemData[]
): ColumnDef<ScoreSummary>[] => {
): ColumnDef<DataTableScoreSummary>[] => {
return [
{
accessorKey: 'submittedProblemCount',
Expand All @@ -32,7 +47,7 @@ export const scoreColumns = (
{String.fromCharCode(Number(65 + i))}
</p>
),
cell: ({ row }: { row: Row<ScoreSummary> }) => {
cell: ({ row }: { row: Row<DataTableScoreSummary> }) => {
const problemScore = row.original.problemScores.find(
(ps) => ps.problemId === problem.problemId
)
Expand Down
Loading

0 comments on commit 5dcbbc4

Please sign in to comment.