Skip to content
Snippets Groups Projects
Select Git revision
  • 02f159d3fe233dac30b005696e6c7128cdd73b9c
  • main default protected
2 results

HomeParticipateForm.js

Blame
  • HomeParticipateForm.js 6.94 KiB
    import { useState, useEffect } from "react";
    import Input from "./Input";
    import Button from "./Button";
    import { useNavigate, useParams } from "react-router-dom";
    import axios from "axios";
    
    function HomeParticipateForm() {
      const [name, setName] = useState("");
      const [password, setPassword] = useState("");
      const [email, setEmail] = useState("");
      const navigate = useNavigate();
      const { id } = useParams();
    
      const handleName = (event) => {
        setName(event.target.value);
      };
      const handlePassword = (event) => {
        setPassword(event.target.value);
      };
      const handleEmail = (event) => {
        setEmail(event.target.value);
      };
      const checkParticipantExistence = async () => {
        try {
          const response = await axios.get(
            `/meetings/${id}/participants/?name=${name}`
          );
          return false;
        } catch (err) {
          return true;
        }
      };
    
      const handleSubmit = async (event) => {
        event.preventDefault();
        if (name === "") {
          alert("이름을 입력하세요");
        } else {
          let checkParticipant = await checkParticipantExistence();
          console.log(checkParticipant);
          if (checkParticipant) {
            // DB에 해당 이름이 존재하지 않으면
            try {
              const response = await axios.post(`/meetings/${id}/participants`, {
                name: name,
                password: password,
                email: email,
              });
              try {
                const response = await axios.post(
                  `/meetings/${id}/entry`,
                  {
                    name: name,
                    password: password,
                  },
                  {
                    withCredentials: true,
                  }
                );
    
                try {
                  const response = await axios.get(`/meetings/${id}/`);
                  console.log(response);
                  const startDate = response.data.startDate;
                  const endDate = response.data.endDate;
                  const startTime = response.data.availableVotingStartTime;
                  const endTime = response.data.availableVotingEndTime;
                  try {
                    const response = await axios.get(
                      `/meetings/${id}/my/schedules`