Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(fe): add contest register deregister button #1545

Merged
merged 7 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
'use client'

import { Button } from '@/components/ui/button'
import { fetcherWithAuth } from '@/lib/utils'
import { useState } from 'react'
import { toast } from 'sonner'

const clickRegister = async (contestId: string) => {
await fetcherWithAuth
.post(`contest/${contestId}/participation`, {
searchParams: { groupId: 1 }
})
.then((res) => res.json())
.catch((err) => console.log(err))
}

const clickDeregister = async (contestId: string) => {
await fetcherWithAuth
.delete(`contest/${contestId}/participation`, {
searchParams: { groupId: 1 }
})
.then((res) => res.json())
.catch((err) => console.log(err))
}

export default function RegisterButton({
id,
registered,
state
}: {
id: string
registered: boolean
state: string
}) {
const [isRegistered, setIsRegistered] = useState(registered)
const buttonColor = isRegistered ? 'bg-secondary' : 'bg-primary'
return (
<>
{state === 'Upcoming' ? (
<Button
className={`px-12 py-6 text-lg font-light ${buttonColor} hover:${buttonColor}`}
onClick={() => {
if (isRegistered) {
clickDeregister(id)
setIsRegistered(false)
toast.success('Deregistered Upcoming test successfully')
} else {
clickRegister(id)
setIsRegistered(true)
toast.success('Registered Upcoming test successfully')
}
}}
>
{isRegistered ? 'Deregister' : 'Register'}
</Button>
) : (
<>
{!isRegistered && (
<Button
className={`px-12 py-6 text-lg font-light ${buttonColor} hover:${buttonColor}`}
onClick={() => {
clickRegister(id)
setIsRegistered(true)
toast.success('Registered Ongoing test successfully')
}}
>
Register
</Button>
)}
</>
)}
</>
)
}
27 changes: 22 additions & 5 deletions apps/frontend/app/(main)/contest/[contestId]/@tabs/page.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { auth } from '@/lib/auth'
import { fetcher } from '@/lib/utils'
import { fetcherWithAuth } from '@/lib/utils'
import { sanitize } from 'isomorphic-dompurify'
import ParticipateButton from './_components/ParticipateButton'
import RegisterButton from './_components/RegisterButton'

interface ContestTop {
description: string
startTime: string
endTime: string
isRegistered: boolean
}

interface ContestTopProps {
Expand All @@ -17,19 +19,34 @@ interface ContestTopProps {
export default async function ContestTop({ params }: ContestTopProps) {
const session = await auth()
const { contestId } = params
const data: ContestTop = await fetcher.get(`contest/${contestId}`).json()
const data: ContestTop = await fetcherWithAuth
.get(`contest/${contestId}`)
.json()

const startTime = new Date(data.startTime)
const endTime = new Date(data.endTime)
const currentTime = new Date()
const state =
currentTime >= endTime
? 'Finished'
: currentTime < startTime
? 'Upcoming'
: 'Ongoing'

return (
<>
<main
className="prose w-full max-w-full border-b-2 border-b-gray-300 p-5 py-12"
dangerouslySetInnerHTML={{ __html: sanitize(data.description) }}
/>
{session && currentTime < startTime && (

{session && state !== 'Finished' && (
<div className="mt-10 flex justify-center">
<ParticipateButton id={contestId} />
<RegisterButton
id={contestId}
registered={data.isRegistered}
state={state}
/>
</div>
)}
</>
Expand Down