Skip to content
Snippets Groups Projects
HomeParticipateForm.js 6.99 KiB
Newer Older
  • Learn to ignore specific revisions
  • Min Dong Hyeun's avatar
    Min Dong Hyeun committed
    import { useState, useEffect } from "react";
    
    import Input from "./Input";
    import Button from "./Button";
    
    Min Dong Hyeun's avatar
    Min Dong Hyeun committed
    import { useNavigate, useParams } from "react-router-dom";
    
    Min Dong Hyeun's avatar
    Min Dong Hyeun committed
    import axios from "axios";
    
    
    function HomeParticipateForm() {
    
    Min Dong Hyeun's avatar
    Min Dong Hyeun committed
        const [name, setName] = useState("");
        const [password, setPassword] = useState("");
        const [email, setEmail] = useState("");
    
        const navigate = useNavigate();
    
    Min Dong Hyeun's avatar
    Min Dong Hyeun committed
        const { id } = useParams();
    
    Min Dong Hyeun's avatar
    Min Dong Hyeun committed
        const handleName = (event) => {
    
            setName(event.target.value);
        }
    
    Min Dong Hyeun's avatar
    Min Dong Hyeun committed
        const handlePassword = (event) => {
    
            setPassword(event.target.value);
        }
    
    Min Dong Hyeun's avatar
    Min Dong Hyeun committed
        const handleEmail = (event) => {
    
            setEmail(event.target.value);
        }
    
    Min Dong Hyeun's avatar
    Min Dong Hyeun committed
        const checkParticipantExistence = async()=>{
    
    Min Dong Hyeun's avatar
    Min Dong Hyeun committed
            try{
    
                const response = await axios.get(`http://localhost:3000/meetings/${id}/participants/?name=${name}`);
    
    Min Dong Hyeun's avatar
    Min Dong Hyeun committed
                return false;
            }
            catch(err){
                return true;
    
    Min Dong Hyeun's avatar
    Min Dong Hyeun committed
            }
        }
    
    Min Dong Hyeun's avatar
    Min Dong Hyeun committed
        const newHandleSubmit = async (event) => {
            event.preventDefault();
            if (name === "") {
                alert('이름을 입력하세요')
            }
            else {
    
    Min Dong Hyeun's avatar
    Min Dong Hyeun committed
                let checkParticipant =  await checkParticipantExistence();
                if(checkParticipant){
                    try {
    
                        const response = await axios.post(`http://localhost:3000/meetings/${id}/participants`, {
    
    Min Dong Hyeun's avatar
    Min Dong Hyeun committed
                            name: name,
                            password: password,
                            email: email
                        });
    
                        try {
                            const response = await axios.post(`http://localhost:3000/meetings/${id}/entry`,{
                                name : name,
                                password : password
                            }, {
                                withCredentials: true 
                            });
    
                            try{
                                const response = await axios.get(`http://localhost:3000/meetings/${id}/`);
    
                                const startDate = response.data.startDate;
                                const endDate = response.data.endDate;
                                try{
                                    const response = await axios.get(`http://localhost:3000/meetings/${id}/my/schedules`);
                                    // console.log(startDate, endDate);
                                    navigate('UserTimeInfo', { state: { id:id, startDate: startDate, endDate: endDate, schedules:response.data.schedules }});
                                }
                                catch(e){
                                    console.log(e);
                                }
    
                            }
                            catch(e){
                                console.log(e);
                            }
                        }
                        catch (error) {
                            if (error.response) {
                                if (error.response.status === 401) {
                                    alert('Password를 잘못 입력하였습니다');
                                } else if (error.response.status === 404) {
                                    alert('해당하는 이름이 존재하지 않습니다');
                                } 
                                else if(error.response.status === 400){
                                    alert("비밀번호를 설정하셨습니다. 비밀번호를 입력해주세요")
                                }
                                else {
                                    alert(`Unexpected status code: ${error.response.status}`);
                                }
                            } else {
                                console.error(error);
                            }
                        }
    
    Min Dong Hyeun's avatar
    Min Dong Hyeun committed
                    }
                    catch (error) {
                        console.error(error);
                    }
    
    Min Dong Hyeun's avatar
    Min Dong Hyeun committed
                }
    
    Min Dong Hyeun's avatar
    Min Dong Hyeun committed
                else{
                    alert('이미 존재하는 이름입니다.');
    
    Min Dong Hyeun's avatar
    Min Dong Hyeun committed
                }
            }
        };
        const oldHandleSubmit = async (event) => {
    
            event.preventDefault();
    
    Min Dong Hyeun's avatar
    Min Dong Hyeun committed
            if (name === "") {
                alert('이름을 입력하세요')
            }
            else {
                try {
    
                    const response = await axios.post(`http://localhost:3000/meetings/${id}/entry`,{
    
    Min Dong Hyeun's avatar
    Min Dong Hyeun committed
                        name : name,
                        password : password
    
                    }, {
                        withCredentials: true 
    
    Min Dong Hyeun's avatar
    Min Dong Hyeun committed
                    });
    
                    try{
                        const response = await axios.get(`http://localhost:3000/meetings/${id}/`);
    
                        const startDate = response.data.startDate;
                        const endDate = response.data.endDate;
                        try{
                            const response = await axios.get(`http://localhost:3000/meetings/${id}/my/schedules`);
                            // console.log(startDate, endDate);
                            navigate('UserTimeInfo', { state: { id:id, startDate: startDate, endDate: endDate, schedules:response.data.schedules }});
                        }
                        catch(e){
                            console.log(e);
                        }
    
                    }
                    catch(e){
                        console.log(e);
                    }
    
    Min Dong Hyeun's avatar
    Min Dong Hyeun committed
                }
                catch (error) {
                    if (error.response) {
                        if (error.response.status === 401) {
    
    Min Dong Hyeun's avatar
    Min Dong Hyeun committed
                            alert('Password를 잘못 입력하였습니다');
    
    Min Dong Hyeun's avatar
    Min Dong Hyeun committed
                        } else if (error.response.status === 404) {
    
    Min Dong Hyeun's avatar
    Min Dong Hyeun committed
                            alert('해당하는 이름이 존재하지 않습니다');
                        } 
                        else if(error.response.status === 400){
                            alert("비밀번호를 설정하셨습니다. 비밀번호를 입력해주세요")
                        }
                        else {
    
    Min Dong Hyeun's avatar
    Min Dong Hyeun committed
                            alert(`Unexpected status code: ${error.response.status}`);
                        }
                    } else {
                        console.error(error);
                    }
                    
                }
            }
    
    Min Dong Hyeun's avatar
    Min Dong Hyeun committed
                    <h1>투표에 참여하기</h1>
                    {console.log({id})}
                    <Input
                        type="text"
                        value={name}
                        onChange={handleName}
                        placeholder="이름"
    
    Min Dong Hyeun's avatar
    Min Dong Hyeun committed
                    <Input
                        type="password"
                        value={password}
                        onChange={handlePassword}
    
    Min Dong Hyeun's avatar
    Min Dong Hyeun committed
                        placeholder="Password(선택)"
    
    Min Dong Hyeun's avatar
    Min Dong Hyeun committed
                    <Input
                        type="text"
                        value={email}
                        onChange={handleEmail}
                        placeholder="이메일(선택)"
                    />
                    <Button
                        type='submit'
                        text='신규참여'
                        onClick={newHandleSubmit}
    
    Min Dong Hyeun's avatar
    Min Dong Hyeun committed
                        type='submit'
                        text='재참여'
                        onClick={oldHandleSubmit}
    
                    />
                </div>
            </form>
        );
    }
    
    export default HomeParticipateForm;