Skip to content

Commit

Permalink
fix(fe): unpack contest config (#1614)
Browse files Browse the repository at this point in the history
* fix(fe): unpack contest config

* fix(deps): revert cmdk to v0.2.1

* fix(fe): add default value
  • Loading branch information
jimin9038 authored Mar 26, 2024
1 parent 926367c commit 5d9c5a7
Show file tree
Hide file tree
Showing 6 changed files with 238 additions and 50 deletions.
19 changes: 8 additions & 11 deletions apps/frontend/app/admin/contest/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ const UPDATE_CONTEST = gql(`
mutation UpdateContest($groupId: Int!, $input: UpdateContestInput!) {
updateContest(groupId: $groupId, input: $input) {
id
config
isRankVisible
isVisible
description
endTime
startTime
Expand Down Expand Up @@ -121,10 +122,8 @@ const inputStyle =
const schema = z.object({
id: z.number(),
title: z.string().min(1).max(100),
config: z.object({
isRankVisible: z.boolean(),
isVisible: z.boolean()
}),
isRankVisible: z.boolean(),
isVisible: z.boolean(),
description: z.string().min(1),
startTime: z.date(),
endTime: z.date()
Expand Down Expand Up @@ -155,10 +154,8 @@ export default function Page({ params }: { params: { id: string } }) {
} = useForm<UpdateContestInput>({
resolver: zodResolver(schema),
defaultValues: {
config: {
isRankVisible: true,
isVisible: true
}
isRankVisible: true,
isVisible: true
}
})

Expand Down Expand Up @@ -349,7 +346,7 @@ export default function Page({ params }: { params: { id: string } }) {
{errors.title && (
<div className="flex items-center gap-1 text-xs text-red-500">
<PiWarningBold />
{getValues('title').length === 0
{getValues('title')?.length === 0
? 'required'
: errors.title?.message}
</div>
Expand Down Expand Up @@ -405,7 +402,7 @@ export default function Page({ params }: { params: { id: string } }) {
<TextEditor
placeholder="Enter a description..."
onChange={field.onChange}
defaultValue={field.value}
defaultValue={field.value ?? ''}
/>
)}
name="description"
Expand Down
33 changes: 12 additions & 21 deletions apps/frontend/app/admin/contest/_components/Columns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,16 @@ interface DataTableContest {
endTime: string
description: string
participants: number
config: {
isVisible: boolean
isRankVisible: boolean
}
isVisible: boolean
isRankVisible: boolean
}

const EDIT_VISIBLE = gql(`
mutation UpdateContestVisible($groupId: Int!, $input: UpdateContestInput!) {
updateContest(groupId: $groupId, input: $input) {
id
config
isVisible
isRankVisible
}
}
`)
Expand All @@ -46,7 +45,7 @@ function VisibleCell({ row }: { row: Row<DataTableContest> }) {
<div className="flex space-x-2">
<Switch
id="hidden-mode"
checked={row.original.config.isVisible}
checked={row.original.isVisible}
onCheckedChange={() => {
const currentTime = dateFormatter(new Date(), 'YYYY-MM-DD HH:mm:ss')
const startTime = dateFormatter(
Expand All @@ -61,10 +60,6 @@ function VisibleCell({ row }: { row: Row<DataTableContest> }) {
toast.error('Cannot change visibility of ongoing contest')
return
}
row.original.config = {
...row.original.config,
isVisible: !row.original.config.isVisible
}
// TODO: contest update API 수정되면 고치기
updateVisible({
variables: {
Expand All @@ -75,10 +70,8 @@ function VisibleCell({ row }: { row: Row<DataTableContest> }) {
startTime: row.original.startTime,
endTime: row.original.endTime,
description: row.original.description,
config: {
isVisible: row.original.config.isVisible,
isRankVisible: row.original.config.isRankVisible
}
isVisible: !row.original.isVisible,
isRankVisible: row.original.isRankVisible
}
}
})
Expand All @@ -90,7 +83,7 @@ function VisibleCell({ row }: { row: Row<DataTableContest> }) {
<Tooltip>
<TooltipTrigger asChild>
<button>
{row.original.config.isVisible ? (
{row.original.isVisible ? (
<FiEye className="text-primary h-[14px] w-[14px]" />
) : (
<FiEyeOff className="h-[14px] w-[14px] text-gray-400" />
Expand All @@ -99,22 +92,20 @@ function VisibleCell({ row }: { row: Row<DataTableContest> }) {
</TooltipTrigger>
<TooltipContent
className={cn(
row.original.config.isVisible ? 'bg-primary' : 'bg-black'
row.original.isVisible ? 'bg-primary' : 'bg-black'
)}
align="end"
alignOffset={-20}
>
{row.original.config.isVisible ? (
{row.original.isVisible ? (
<p>This contest is visible</p>
) : (
<p>This contest is not visible to users</p>
)}
<TooltipPrimitive.Arrow
className={cn(
'fill-current',
row.original.config.isVisible
? 'text-primary'
: 'text-black'
row.original.isVisible ? 'text-primary' : 'text-black'
)}
/>
</TooltipContent>
Expand Down Expand Up @@ -189,7 +180,7 @@ export const columns: ColumnDef<DataTableContest>[] = [
)
},
{
accessorKey: 'config.isVisible',
accessorKey: 'isVisible',
header: ({ column }) => (
<DataTableColumnHeader column={column} title="Visible" />
),
Expand Down
15 changes: 6 additions & 9 deletions apps/frontend/app/admin/contest/create/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ const CREATE_CONTEST = gql(`
mutation CreateContest($groupId: Int!, $input: CreateContestInput!) {
createContest(groupId: $groupId, input: $input) {
id
config
isVisible
isRankVisible
description
endTime
startTime
Expand Down Expand Up @@ -84,10 +85,8 @@ const inputStyle =

const schema = z.object({
title: z.string().min(1).max(100),
config: z.object({
isRankVisible: z.boolean().optional(),
isVisible: z.boolean().optional()
}),
isRankVisible: z.boolean().optional(),
isVisible: z.boolean().optional(),
description: z.string().min(1),
startTime: z.date(),
endTime: z.date()
Expand Down Expand Up @@ -116,10 +115,8 @@ export default function Page() {
} = useForm<CreateContestInput>({
resolver: zodResolver(schema),
defaultValues: {
config: {
isRankVisible: true,
isVisible: true
}
isRankVisible: true,
isVisible: true
}
})

Expand Down
3 changes: 2 additions & 1 deletion apps/frontend/app/admin/contest/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ const GET_CONTESTS = gql(`
endTime
description
participants
config
isRankVisible
isVisible
}
}
`)
Expand Down
2 changes: 1 addition & 1 deletion apps/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"@uiw/codemirror-themes": "^4.21.25",
"@uiw/react-codemirror": "^4.21.25",
"apollo-upload-client": "^18.0.1",
"cmdk": "^1.0.0",
"cmdk": "^0.2.1",
"date-fns": "^3.6.0",
"dayjs": "^1.11.10",
"dotenv": "^16.4.5",
Expand Down
Loading

0 comments on commit 5d9c5a7

Please sign in to comment.