11import { Check , Plus } from "lucide-react" ;
2- import { useState } from "react" ;
2+ import { useEffect , useState } from "react" ;
33
44import { AddEvent } from "@/components/ui/add-event-menu" ;
55import { AddHealthEvent } from "@/components/ui/add-health-event-menu" ;
@@ -15,24 +15,62 @@ import { TaskCarousel } from "@/components/ui/pet-task-carousel";
1515import { TextTitle } from "@/components/ui/text-styles" ;
1616import { dm_sans } from "@/lib/fonts" ;
1717
18+ interface petInfoResponse {
19+ name : string ;
20+ dob : string ;
21+ type : string ;
22+ img : string ;
23+ }
24+
1825export default function Default ( ) {
1926 const [ taskMenu , setTaskMenu ] = useState ( false ) ;
2027 const [ settingsMenu , setSettingsMenu ] = useState ( false ) ;
2128 const [ eventMenu , setEventMenu ] = useState ( false ) ;
2229 const [ healthMenu , setHealthMenu ] = useState ( false ) ;
2330
31+ const [ petInfo , setPetInfo ] = useState < petInfoResponse > ( ) ;
32+
33+ useEffect ( ( ) => {
34+ const fetchData = async ( ) => {
35+ const data = await fetch ( "http://localhost:8000/pet/" ) ;
36+ const petInfo = await data . json ( ) ;
37+ setPetInfo ( petInfo ) ;
38+ } ;
39+
40+ fetchData ( ) ;
41+ } , [ ] ) ;
42+
43+ const formatPetAge = ( ) => {
44+ if ( ! petInfo ) return null ;
45+ const dob = new Date ( "2020-12-01" ) ;
46+ const now = Date . now ( ) ;
47+ const age = now - dob . getTime ( ) ;
48+ const yearsAndMonths = age / 1000 / 60 / 60 / 24 / 365.25 ;
49+ const years = Math . floor ( yearsAndMonths ) ;
50+ const months = Math . floor ( ( yearsAndMonths % 1 ) * 12 ) ;
51+ if ( years == 1 && months == 1 ) {
52+ return `${ years } year ${ months } month` ;
53+ } else if ( years > 1 && months == 1 ) {
54+ return `${ years } years ${ months } month` ;
55+ } else if ( years == 1 && months > 1 ) {
56+ return `${ years } year ${ months } months` ;
57+ } else {
58+ return `${ years } years ${ months } months` ;
59+ }
60+ } ;
61+
2462 return (
2563 < div className = { dm_sans . className } >
2664 < h1 className = "flex justify-center p-10" > Navbar </ h1 >
2765 < PetHeader
2866 onClickTask = { ( ) => setTaskMenu ( true ) }
2967 onClickSettings = { ( ) => setSettingsMenu ( true ) }
30- petImg = ""
68+ petImg = { petInfo ?. img || "" }
3169 petAlt = "Spaghetti's Profile Image"
3270 petInit = "S"
33- petName = "Spaghetti (example pet)"
34- petType = "Cat"
35- petAge = "5 yrs 6 months"
71+ petName = { petInfo ?. name || "Loading..." }
72+ petType = { petInfo ?. type || "Loading..." }
73+ petAge = { formatPetAge ( ) || "Loading..." }
3674 />
3775
3876 < div className = "mx-[7vw]" >
@@ -42,9 +80,9 @@ export default function Default() {
4280
4381 { settingsMenu === true && (
4482 < PetSettings
45- petName = "Spaghetti"
46- petType = "Cat"
47- petDOB = "01/06/2020"
83+ petName = { petInfo ?. name || "Loading..." }
84+ petType = { petInfo ?. type || "Loading..." }
85+ petDOB = { petInfo ?. dob || "Loading..." }
4886 onClickClose = { ( ) => setSettingsMenu ( false ) }
4987 />
5088 ) }
0 commit comments