From 2bbb5598d1ef35731abef73d58d05ed1e9d3d5dc Mon Sep 17 00:00:00 2001 From: suzinxix Date: Thu, 23 Feb 2023 13:36:28 +0900 Subject: [PATCH] [#46] feat: implement pet list api --- bowwowcare/src/views/HomePage/HomePage.js | 105 +++++++++--------- .../HomePage/PetList/Sections/PetInfo.js | 4 +- .../src/views/PetInfoPage/PetInfoPage.js | 9 +- 3 files changed, 57 insertions(+), 61 deletions(-) diff --git a/bowwowcare/src/views/HomePage/HomePage.js b/bowwowcare/src/views/HomePage/HomePage.js index a76d5de..cfee6fb 100644 --- a/bowwowcare/src/views/HomePage/HomePage.js +++ b/bowwowcare/src/views/HomePage/HomePage.js @@ -1,5 +1,8 @@ import React, { useState, useEffect } from "react"; import { useNavigate } from "react-router-dom"; +import axios from "axios"; +import { API_URL } from "../../Config"; +import authHeader from "../../services/auth-header"; import Header from "../../components/Header"; import PetList from "./PetList/PetList"; @@ -8,6 +11,7 @@ import anxiety from "../../assets/images/anxiety.png"; import aggression from "../../assets/images/aggression.png"; function HomePage() { + const [petlist, setPetlist] = useState([]); const [pet, setPet] = useState([]); const navigate = useNavigate(); const fileInput = React.useRef(null); @@ -27,78 +31,71 @@ function HomePage() { } }; + const handleNavigation = (type) => { + if (type === "aggression") { + navigate("/behavior-selection", { + state: { + type: type, + }, + }); + } else if (type === "anxiety") { + navigate("/examination", { + state: { + type: type, + }, + }); + } + }; + const handleBehaviorSelection = (type) => { navigate("/behavior-selection", { state: { - type: type - } - }) + type: type, + }, + }); + }; - } + const getPetList = async () => { + axios({ + method: "GET", + url: `${API_URL}/pets`, + headers: authHeader(), + }).then((res) => { + if (res.status === 200) { + console.log(res); + setPetlist(res.data); + } + }).catch((e) => { + console.log(e.message); + }) - const res = [ - { - id: 0, - petname: "강쥐", - gender: "남", - birthDate: "2020-01-30T08:13:57.980Z", - adoptDate: "2020-01-30T08:13:57.980Z", - fileImg: "", - }, - { - id: 1, - petname: "강쥐2", - gender: "여", - birthDate: "2019-01-30T08:13:57.980Z", - adoptDate: "2019-01-30T08:13:57.980Z", - fileImg: "", - }, - { - id: 2, - petname: "강쥐3", - gender: "남", - birthDate: "2010-01-30T08:13:57.980Z", - adoptDate: "2010-01-30T08:13:57.980Z", - fileImg: "", - }, - ]; + + }; + const getPetDetail = async () => { + const promise = petlist.map(async (v) => { + const res = await axios.get(`${API_URL}/pets/${v.petId}`); + return res.data; + }); + return Promise.all(promise).then((result) => setPet(result)); + }; useEffect(() => { - setPet(res); - // console.log(pet); + getPetList(); + getPetDetail(); }, []); - // TODO: 펫 데이터 불러오기 - // useEffect(async() => { - // try{ - // const res = await axios.get('/api/v1/pets'); - // const input = await res.map((x) => ({ - // id: x.idx, - // petname: x.name, - // gender: x.gender, - // birthDate: x.birthDate, - // adopDate: x.adopday, - // fileImg: x.fileImg - // }) - // ) - // setPet(pet.concat(input)) - // } catch(e){ - // console.error(e.message) - // } - // },[]) - return (
{pet && }
- +

우리 아이 이상행동 확인해보기

- - diff --git a/bowwowcare/src/views/HomePage/PetList/Sections/PetInfo.js b/bowwowcare/src/views/HomePage/PetList/Sections/PetInfo.js index f294871..a83cf55 100644 --- a/bowwowcare/src/views/HomePage/PetList/Sections/PetInfo.js +++ b/bowwowcare/src/views/HomePage/PetList/Sections/PetInfo.js @@ -5,7 +5,7 @@ import { getDurationDate } from "../../../../utils/Calculator"; function PetInfo(props) { const birthDate = new Date(Date.parse(props.pet.birthDate)); - const adoptDate = new Date(Date.parse(props.pet.adoptDate)); + const adoptDate = new Date(Date.parse(props.pet.adoptionDate)); const age = getAge(birthDate); const day = getDurationDate(adoptDate); @@ -22,7 +22,7 @@ function PetInfo(props) { )}
- {props.pet.petname} + {props.pet.name} {age}살
함께한지 {day}일째
diff --git a/bowwowcare/src/views/PetInfoPage/PetInfoPage.js b/bowwowcare/src/views/PetInfoPage/PetInfoPage.js index ceb5ff2..6ef568f 100644 --- a/bowwowcare/src/views/PetInfoPage/PetInfoPage.js +++ b/bowwowcare/src/views/PetInfoPage/PetInfoPage.js @@ -9,13 +9,12 @@ function PetInfoPage() { const navigate = useNavigate(); const location = useLocation(); const params = useParams(); - //console.log(location.state.pet) const pet = location.state.pet; - const petname = location.state.pet.petname; + const petname = location.state.pet.name; const gender = location.state.pet.gender; const birthDate = location.state.pet.birthDate.substring(0,10).split('-').join('.'); - const adoptDate = location.state.pet.adoptDate.substring(0,10).split('-').join('.'); - const fileImg = location.state.pet.fileImg; + const adoptDate = location.state.pet.adoptionDate.substring(0,10).split('-').join('.'); + const fileImg = location.state.pet.petImg; return (
@@ -27,7 +26,7 @@ function PetInfoPage() {
{fileImg && ( 프로필 이미지