diff --git a/src/pages/Registration.jsx b/src/pages/Registration.jsx index 476c3fb..52310ad 100644 --- a/src/pages/Registration.jsx +++ b/src/pages/Registration.jsx @@ -1,10 +1,12 @@ // 3.0 내가 찾은 유기동물 등록하기 페이지 -import React, { useState, useRef } from "react"; +import React, { useState, useRef, useEffect } from "react"; import { Header, MHeader } from "../components"; import { bg2, RegistDropPet } from "../images"; import { useNavigate } from "react-router-dom"; import styled from "styled-components"; +import { useLocation } from "react-router-dom"; +import axios from "axios"; const Registration = () => { const isMobile = window.innerWidth <= 393; @@ -13,11 +15,17 @@ const Registration = () => { const [imgBase64, setImgBase64] = useState(""); const inputRef = useRef(); - // 업로드 api 로직 추가 해야함 + const location = useLocation(); + const queryString = location.search.slice(1); - const handleCompleteClick = () => { - navigate("/research"); - }; + // 입력 필드들 상태 + const [registrarName, setRegistrarName] = useState(""); + const [connectNumber, setConnectNumber] = useState(""); + const [breed, setBreed] = useState(""); + const [gender, setGender] = useState(""); + const [isNeutered, setIsNeutered] = useState(""); + const [findlocation, setFindlocation] = useState(""); + const [significantReport, setSignificantReport] = useState(""); // 이미지 변경 핸들러 부분 const imageChange = (e) => { @@ -34,6 +42,43 @@ const Registration = () => { } }; + // api 연동 부분 + const handleCompleteClick = async () => { + const formData = new FormData(); + + formData.append("photo", selectedImage); + formData.append("is_dog", queryString === "dog" ? true : false); + formData.append("breed", breed); + formData.append("gender", gender); + formData.append("is_neutered", isNeutered === "yes" ? true : false); + formData.append("name", registrarName); + formData.append("shelter_location", "Not protected yet"); + formData.append("shelter_contact", connectNumber); + formData.append("location", findlocation); + formData.append("notes", significantReport); + formData.append("is_adopted", false); + formData.append("password", "animal"); + + try { + const response = await axios.post( + "https://34.64.68.236.nip.io/animals", + formData, + { + headers: { + "Content-Type": "multipart/form-data", + }, + } + ); + + console.log("Image uploaded successfully:", response.data); + navigate("/research"); + } catch (error) { + console.log("FormData to be sent:", Array.from(formData.entries())); + + console.error("Error during the fetch operation:", error); + } + }; + return ( <> {isMobile ? :
} @@ -75,6 +120,8 @@ const Registration = () => { as="input" type="text" placeholder="Please enter it." + value={registrarName} + onChange={(e) => setRegistrarName(e.target.value)} /> @@ -85,43 +132,72 @@ const Registration = () => { as="input" type="text" placeholder="Please enter it." + value={connectNumber} + onChange={(e) => setConnectNumber(e.target.value)} /> - Breed + Where You found setFindlocation(e.target.value)} /> - Where You found + Breed + as="select" + value={breed} + onChange={(e) => setBreed(e.target.value)} + > + + {/* 강아지 */} + + + + + + + {/* 고양이 */} + + + + + + Neutered - + setIsNeutered(e.target.value)} + > - Gender - + setGender(e.target.value)} + > - - - + + @@ -130,6 +206,8 @@ const Registration = () => { as="textarea" type="text" placeholder="Please enter it." + value={significantReport} + onChange={(e) => setSignificantReport(e.target.value)} /> diff --git a/src/pages/Select.jsx b/src/pages/Select.jsx index cd3d57d..6578e4b 100644 --- a/src/pages/Select.jsx +++ b/src/pages/Select.jsx @@ -15,7 +15,7 @@ import { Link, useLocation } from "react-router-dom"; const Select = () => { const location = useLocation(); const queryString = location.search.slice(1); - + console.log(queryString); return (