Skip to content

Commit

Permalink
[#46] feat: implement pet list and pet info screen
Browse files Browse the repository at this point in the history
  • Loading branch information
suzinxix committed Jan 31, 2023
1 parent c263628 commit 57a112d
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 4 deletions.
7 changes: 5 additions & 2 deletions bowwowcare/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useDispatch, useSelector } from "react-redux";

import "tailwindcss/tailwind.css";

import HomePage from './views/HomePage';
import HomePage from './views/HomePage/HomePage'
import PreviewPage from './views/PreviewPage/PreviewPage';
import ResultsPage from './views/ResultsPage/ResultsPage';
import CameraPage from './views/CameraPage/CameraPage';
Expand All @@ -14,6 +14,8 @@ import LoginPage from './views/LoginPage/LoginPage';
import SignupPage from './views/SignupPage/SignupPage';
import AdditionPage from "./views/AdditionPage/AdditionPage";
import PrivateRoute from "./PrivateRoute";
import PetInfo from "./views/HomePage/PetList/PetInfo/PetInfo";
import PetInfoPage from "./views/PetInfoPage/PetInfoPage";

import { logout } from "./slices/auth";

Expand Down Expand Up @@ -49,7 +51,8 @@ function App() {
<Route path="/solution" element={ <SolutionPage /> } />
<Route path="/login" element={ <LoginPage /> } />
<Route path="/signup" element={ <SignupPage /> } />
<Route path="addition" element={<AdditionPage/>} />
<Route path="/addition" element={<AdditionPage/>} />
<Route path="/petinfo/:id" element={<PetInfoPage/>} />
{/* <Route path="/addition" element={<PrivateRoute component={<AdditionPage />} authenticated={token}/>} /> */}
</Routes>
);
Expand Down
20 changes: 20 additions & 0 deletions bowwowcare/src/utils/Calculator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export const getAge = (date) => {
const now = new Date();
const nowYear = now.getFullYear();
const birthYear = date.getFullYear();

const age = nowYear - birthYear

return age;
};

export const getBwDate = (date) => {
const now = new Date();
const stDate = new Date(date.getFullYear(), date.getMonth() + 1, date.getDate());
const endDate = new Date(now.getFullYear(), now.getMonth() + 1, now.getDate());

const btMs = endDate.getTime() - stDate.getTime() ;
const btDay = btMs / (1000*60*60*24);

return btDay;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import React from "react";
import React, { useState, useEffect } from 'react'
import { useNavigate } from "react-router-dom";

import Header from "../components/Header";
import Header from "../../components/Header";
import PetList from "./PetList/PetList"

function HomePage() {
const [pet, setPet] = useState([]);
const navigate = useNavigate();
const fileInput = React.useRef(null);

Expand All @@ -22,6 +24,53 @@ function HomePage() {
}
};

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: ""
}]

useEffect(() => {
setPet(res);
console.log(pet);
}, [])

// 펫 데이터 불러오기
// 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 (
<div className="container mx-auto px-8 w-screen h-screen">
<Header />
Expand Down Expand Up @@ -50,6 +99,7 @@ function HomePage() {
onClick={() => navigate("/camera")}>사진 찍기</button>

</div>
{pet && <PetList pet={pet}/>}

<button className="w-full h-32 mt-6 text-center rounded-md border border-gray-300 hover:border-main-color text-gray-300 bg-transparent pl-4"
onClick={()=>navigate("/addition")}>
Expand Down
35 changes: 35 additions & 0 deletions bowwowcare/src/views/HomePage/PetList/PetInfo/PetInfo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React from 'react'

import { getAge } from '../../../../utils/Calculator';
import { getBwDate } 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 age = getAge(birthDate);
const day = getBwDate(adoptDate);

return (
<div className='flex shadow-lg mt-6 p-8 rounded-lg'>
<div className="rounded-full border w-14 h-14">
{props.pet.fileImg && (
<img
src={URL.createObjectURL(props.pet.fileImg)}
alt="프로필 이미지"
className="rounded-full w-14 h-14"
></img>
)}
</div>
<div className='ml-5'>
<span className='text-xl'>{props.pet.petname}</span>
<span className='text-sm'> {age}</span>
<div>함께한지 {day}일째</div>
</div>
</div>
)

}

export default PetInfo
20 changes: 20 additions & 0 deletions bowwowcare/src/views/HomePage/PetList/PetList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from 'react'
import { Link } from 'react-router-dom';

import PetInfo from './PetInfo/PetInfo'

function PetList(props) {
return (
<div>
{ props.pet.map((a, i)=>{
return(
<Link to={`/petinfo/${a.id}`} state={{ pet: a }}>
<PetInfo pet={a} key={i}/>
</Link>
)
})}
</div>
)
}

export default PetList
70 changes: 70 additions & 0 deletions bowwowcare/src/views/PetInfoPage/PetInfoPage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import React from 'react'
import { useLocation, useNavigate, useParams } from "react-router-dom";

import Header from '../../components/Header';
import Button from '../../components/Button';

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 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;

return (
<div className="px-8">
<Header />
<div className='flex justify-center mt-30'>
<div className='flex flex-col w-full'>
<div className='text-center'>
<div className='flex justify-center'>
<div className="rounded-full border w-14 h-14 mb-5">
{fileImg && (
<img
src={URL.createObjectURL(fileImg)}
alt="프로필 이미지"
className="rounded-full w-14 h-14"
></img>
)}
</div>
</div>


<div className='text-xl mb-5'>{petname}</div>
<div className='flex justify-center'>
<div className='text-sm mb-10 border w-28 p-2 rounded-lg'>{gender}</div>
</div>

</div>
<div className='mb-10'>
<div className='flex justify-center'>
<table className='border-separate border-spacing-3'>
<tr>
<td>태어난 날</td>
<td>{birthDate}</td>
</tr>
<tr>
<td>가족이 된 날</td>
<td>{adoptDate}</td>
</tr>
</table>
</div>
</div>

<div className='shadow-lg p-10 h-60 mb-10'>
<span>기록</span>
</div>
<Button children="편집" onClick={()=>navigate(`/edit/${params.id}`, { state: pet })}></Button>
</div>
</div>
</div>
)
}

export default PetInfoPage

0 comments on commit 57a112d

Please sign in to comment.