diff --git a/app/join/[id]/page.tsx b/app/join/[id]/page.tsx index 5567e63..41dbf3a 100644 --- a/app/join/[id]/page.tsx +++ b/app/join/[id]/page.tsx @@ -1,23 +1,54 @@ 'use client'; -import { useRouter } from 'next/navigation'; +import { useRouter, useParams } from 'next/navigation'; import { useState } from 'react'; +import { useParticipantEnter } from '@/hooks/api/useParticipant'; +import { useToast } from '@/hooks/useToast'; +import Toast from '@/components/ui/toast'; export default function Page() { + const params = useParams(); + const meetingId = params?.id as string; const [name, setName] = useState(''); const [password, setPassword] = useState(''); const [isRemembered, setIsRemembered] = useState(true); + const [errorMessage, setErrorMessage] = useState(''); const router = useRouter(); + const participantEnter = useParticipantEnter(); + const { isVisible, show } = useToast(); // 이름/비번 유효성 검사 (입력값이 있을 때만 버튼 활성화) const isFormValid = name.length > 0 && password.length === 4; - const handleSubmit = (e: React.FormEvent) => { + const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); - if (!isFormValid) return; + if (!isFormValid || !meetingId) return; - console.log('참여 요청:', { name, password, isRemembered }); - router.push('/meeting'); + try { + const result = await participantEnter.mutateAsync({ + meetingId, + data: { + userId: name, + password, + }, + }); + + if (result.success) { + if (isRemembered) { + localStorage.setItem('userId', name); + } else { + sessionStorage.setItem('userId', name); + } + + router.push(`/meeting/${meetingId}`); + } else { + setErrorMessage('모임 참여에 실패했습니다. 다시 시도해주세요.'); + show(); + } + } catch { + setErrorMessage('모임 참여에 실패했습니다. 이름과 비밀번호를 확인해주세요.'); + show(); + } }; return ( @@ -101,17 +132,20 @@ export default function Page() { {/* 하단 버튼 */} - +