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

refactor: join form #22

Merged
merged 1 commit into from
Mar 11, 2024
Merged
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
53 changes: 15 additions & 38 deletions app/components/JoinForm.tsx
Original file line number Diff line number Diff line change
@@ -1,58 +1,30 @@
'use client'
import { ArrowRightIcon, RefreshIcon, Svg100MsLogoIcon } from '@100mslive/react-icons'
import { useHMSActions } from '@100mslive/react-sdk'
import Image from 'next/image'
import { useState, ChangeEvent, FormEvent, useEffect } from 'react'
import { Inter } from 'next/font/google'
import { useState, FormEvent } from 'react'
import { useSearchParams } from 'next/navigation'
import { ROLES } from './constants'

const inter = Inter({ subsets: ['latin'] })

// Define a type for your state
type InputValues = {
name: string
roomCode: string // Assuming roomCode is optional
}

const JoinForm = () => {
const [activeTabRole, setActiveTabRole] = useState('teacher')
const [activeTabRole, setActiveTabRole] = useState(ROLES.TEACHER)
const hmsActions = useHMSActions()

const searchParams = useSearchParams()
const roomCodeParam = searchParams.get('room') || ''
const [loading, setLoading] = useState(false)

const handleTabClick = (tab) => {
setActiveTabRole(tab)
}

const [inputValues, setInputValues] = useState<InputValues>({
name: '',
roomCode: '',
})

// Type the event parameter
const handleInputChange = (e: ChangeEvent<HTMLInputElement>) => {
setInputValues((prevValues) => ({
...prevValues,
[e.target.name]: e.target.value,
}))
}
const [name, setName] = useState<string>('')

// Type the event parameter
const handleSubmit = async (e: FormEvent<HTMLFormElement>) => {
e.preventDefault()
setLoading(true)

const { name: userName = '', roomCode = '' } = inputValues

if (roomCodeParam) {
try {
localStorage.setItem('roomCode', roomCodeParam)
let authToken = ''
authToken = await hmsActions.getAuthTokenByRoomCode({ roomCode: roomCodeParam })
await hmsActions.join({ userName, authToken })
const authToken = await hmsActions.getAuthTokenByRoomCode({ roomCode: roomCodeParam })
await hmsActions.join({ userName: name, authToken })
} catch (e) {
alert('Room Code might be invalid or expired. Please try again.')
}
Expand Down Expand Up @@ -83,20 +55,18 @@ const JoinForm = () => {
const roomCodeForTeacher = data[1].code

let authToken = ''

if (activeTabRole === 'teacher') {
if (activeTabRole === ROLES.TEACHER) {
authToken = await hmsActions.getAuthTokenByRoomCode({
roomCode: roomCodeForTeacher,
})

localStorage.setItem('roomCode', roomCodeForTeacher)
} else {
authToken = await hmsActions.getAuthTokenByRoomCode({
roomCode: roomCodeForStudent,
})
}

await hmsActions.join({ userName, authToken })
await hmsActions.join({ userName: name, authToken })
}
} else {
setLoading(false)
Expand Down Expand Up @@ -132,7 +102,14 @@ const JoinForm = () => {
<form onSubmit={handleSubmit}>
<div className="input-container">
<div className="input-label">Your Name</div>
<input required id="name" type="text" name="name" placeholder="Name" />
<input
required
id="name"
type="text"
name="name"
placeholder="Name"
onChange={(e) => setName(e.target.value)}
/>
</div>
{roomCodeParam ? null : (
<div className="input-container">
Expand Down