'use client'; import { useState } from 'react'; import Link from 'next/link'; import { useRouter } from 'next/navigation'; const imgRiCheckboxCircleLine = "http://localhost:3845/assets/e4c498605e2559d2764a3112ae9a9019e6ad798e.svg"; const imgFormkitRadio = "http://localhost:3845/assets/ea30a9a80d95ced4bfb1174d3a8475a4a1dbbabb.svg"; const imgAkarIconsRadio = "http://localhost:3845/assets/d772bd292f6dfddfcbd42cc1f22aa796ed671b11.svg"; const imgLsiconDownFilled = "http://localhost:3845/assets/1c65a7143b6e9a0eee4b0878c33198a22da091cf.svg"; export default function RegisterPage() { const router = useRouter(); const [formData, setFormData] = useState({ name: '', phone: '', email: '', password: '', passwordConfirm: '', gender: 'male', birthYear: '', birthMonth: '', birthDay: '', }); const [agreements, setAgreements] = useState({ all: false, age14: false, terms: false, privacy: false, }); const [errors, setErrors] = useState({ name: '', phone: '', email: '', password: '', passwordConfirm: '', }); const [verificationCode, setVerificationCode] = useState(''); const [isVerificationSent, setIsVerificationSent] = useState(false); const [isVerificationComplete, setIsVerificationComplete] = useState(false); const [verificationError, setVerificationError] = useState(''); const handleChange = (e: React.ChangeEvent) => { const { name, value } = e.target; const prevValue = formData[name as keyof typeof formData]; setFormData((prev) => ({ ...prev, [name]: value, })); if (errors[name as keyof typeof errors]) { setErrors((prev) => ({ ...prev, [name]: '', })); } // 이메일이 실제로 변경되었을 때만 인증 상태 초기화 if (name === 'email' && prevValue !== value && isVerificationSent) { setIsVerificationSent(false); setVerificationCode(''); setIsVerificationComplete(false); } }; const handlePhoneChange = (e: React.ChangeEvent) => { let value = e.target.value.replace(/[^\d]/g, ''); if (value.length > 11) value = value.slice(0, 11); setFormData((prev) => ({ ...prev, phone: value, })); if (errors.phone) { setErrors((prev) => ({ ...prev, phone: '', })); } }; const handleAgreementChange = (key: keyof typeof agreements) => { if (key === 'all') { const newValue = !agreements.all; setAgreements({ all: newValue, age14: newValue, terms: newValue, privacy: newValue, }); } else { const newAgreements = { ...agreements, [key]: !agreements[key], }; setAgreements({ ...newAgreements, all: newAgreements.age14 && newAgreements.terms && newAgreements.privacy, }); } }; const handleGenderChange = (gender: 'male' | 'female') => { setFormData((prev) => ({ ...prev, gender, })); }; const validateEmail = (email: string) => { const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; return emailRegex.test(email); }; const validatePassword = (password: string) => { return password.length >= 8 && password.length <= 16 && /[a-zA-Z]/.test(password) && /[0-9]/.test(password); }; const validatePhone = (phone: string) => { if (phone === '') return true; const phoneRegex = /^010\d{8}$/; return phoneRegex.test(phone.replace(/[^\d]/g, '')); }; const handleSendVerificationCode = () => { if (!formData.email || !validateEmail(formData.email)) { setErrors((prev) => ({ ...prev, email: '올바른 이메일을 입력해주세요.', })); return; } // TODO: 인증번호 전송 API 호출 setIsVerificationSent(true); setVerificationCode(''); setIsVerificationComplete(false); setVerificationError(''); }; const handleVerificationCodeChange = (e: React.ChangeEvent) => { setVerificationCode(e.target.value); // 인증번호 입력 시 에러 초기화 if (verificationError) { setVerificationError(''); } }; const handleVerifyCode = () => { if (!verificationCode.trim()) { return; } // TODO: 인증번호 확인 API 호출 // API 연동 전까지 맞는 인증번호는 123456 if (verificationCode.trim() === '123456') { setIsVerificationComplete(true); setVerificationError(''); } else { // 인증번호가 틀렸을 때 에러 메시지 표시 setVerificationError('인증번호가 잘못되었습니다.'); } }; const validateForm = () => { const newErrors = { name: '', phone: '', email: '', password: '', passwordConfirm: '', }; let isValid = true; if (formData.name.trim() === '') { newErrors.name = '이름을 입력해주세요.'; isValid = false; } if (formData.phone && !validatePhone(formData.phone)) { newErrors.phone = '올바른 전화번호 형식이 아닙니다.'; isValid = false; } if (formData.email.trim() === '') { newErrors.email = '이메일을 입력해주세요.'; isValid = false; } else if (!validateEmail(formData.email)) { newErrors.email = '올바른 이메일 형식이 아닙니다.'; isValid = false; } if (formData.password === '') { newErrors.password = '비밀번호를 입력해주세요.'; isValid = false; } else if (!validatePassword(formData.password)) { newErrors.password = '비밀번호는 8~16자의 영문/숫자를 포함해야 합니다.'; isValid = false; } if (formData.passwordConfirm === '') { newErrors.passwordConfirm = '비밀번호 확인을 입력해주세요.'; isValid = false; } else if (formData.password !== formData.passwordConfirm) { newErrors.passwordConfirm = '비밀번호가 일치하지 않습니다.'; isValid = false; } setErrors(newErrors); return isValid; }; const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); if (!agreements.age14 || !agreements.terms || !agreements.privacy) { alert('필수 약관에 동의해주세요.'); return; } if (validateForm()) { router.push('/registercomplete'); } }; const canProceed = formData.name.trim() !== '' && formData.email.trim() !== '' && formData.password !== '' && formData.passwordConfirm !== '' && agreements.age14 && agreements.terms && agreements.privacy; return (
{/* 제목 */}

회원가입

{/* 회원정보 입력 폼 */}
{/* 이름 */}
이름
{errors.name && (

{errors.name}

)}
{/* 휴대폰 */}
휴대폰
{errors.phone && (

{errors.phone}

)}
{/* 이메일(아이디) + 인증번호 발송 */}
이메일(아이디)
{errors.email && (

{errors.email}

)}
{/* 인증번호 입력 폼 */} {isVerificationSent && (
{verificationError && (

{verificationError}

)} {!isVerificationComplete && !verificationError && (

인증 확인을 위해 작성한 이메일로 인증번호를 발송했어요.
이메일을 확인해 주세요.

)} {isVerificationComplete && (

이메일 인증을 완료했어요.

)}
)} {/* 비밀번호 */}
비밀번호
{errors.password && (

{errors.password}

)}
{/* 비밀번호 확인 */}
비밀번호 확인
{errors.passwordConfirm && (

{errors.passwordConfirm}

)}
{/* 성별 */}
성별
{/* 생년월일 */}
생년월일
{/* 약관 동의 */}
{/* 전체동의 */}
handleAgreementChange('all')} > 모든 항목에 동의합니다.
{/* 개별 동의들 */}
handleAgreementChange('age14')}> 만 14세 이상입니다. (필수)
handleAgreementChange('terms')}> 이용 약관 동의 (필수)
handleAgreementChange('privacy')}> 개인정보 수집 및 이용 동의 (필수)
{/* 버튼 영역 */}
돌아가기
{/* 카피라이트 */}
); }