Skip to content

Commit

Permalink
feat(fe): add contest register deregister button (#1545)
Browse files Browse the repository at this point in the history
* feat(fe): add register and deregister button

* feat(fe): register button for upgoing and ongoing

* fix(fe): register for ongoing button

* fix(fe): change deregister button color

* fix(fe): fix toast message

* fix: get rid of comments

* fix(fe): change parameter name
  • Loading branch information
jihorobert authored Mar 11, 2024
1 parent 5fb21d1 commit a76bce0
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 29 deletions.

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

0 comments on commit a76bce0

Please sign in to comment.